Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6543181
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:18:57+00:00 2026-05-25T11:18:57+00:00

everything in my code is working great for creating a thumbnail image of an

  • 0

everything in my code is working great for creating a thumbnail image of an uploaded picture.

now all i need to do is crop the $thumb from the center of the image into a square shape (50×50)

heres my function so far

    $ext = end(explode('.', $_FILES['profile_photo']['name']));

    if ($ext == 'jpg' || $ext == 'jpeg' || $ext == 'png' || $ext == 'gif')
    {
        $tmp = $_FILES['profile_photo']['tmp_name'];

        if ($ext=='jpg' || $ext=='jpeg')
            $src = imagecreatefromjpeg($tmp);
        else if ($ext=='png')
            $src = imagecreatefrompng($tmp);
        else 
            $src = imagecreatefromgif($tmp);

        list($width,$height) = getimagesize($tmp);

        $thumb_width = 50;
        $thumb_height = ($height/$width) * $thumb_width;
        $thumb_tmp = imagecreatetruecolor($thumb_width, $thumb_height);

        $full_width = 200;
        $full_height = ($height/$width) * $full_width;
        $full_tmp = imagecreatetruecolor($full_width, $full_height);

        imagecopyresampled($thumb_tmp, $src, 0, 0, 0, 0, $thumb_width, $thumb_height, $width, $height);         
        imagecopyresampled($full_tmp, $src, 0, 0, 0, 0, $full_width, $full_height, $width, $height);        

        imagejpeg($thumb_tmp, 'images/profile/'.$user['id'].'_'.time().'_thumb.'.$ext, 100);
        imagejpeg($full_tmp, 'images/profile/'.$user['id'].'_'.time().'_full.'.$ext, 100);

        imagedestroy($src);
        imagedestroy($thumb_tmp);
        imagedestroy($full_tmp);

        // delete old image from server if it is not none.png
    }

any help would be greatly appreciated! i know that it has something to do with imagecopyresampled but i can’t figure out the math for the cropping from the center of the image. i want this to be my own function so please dont recommend me using other peoples classes.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:18:57+00:00Added an answer on May 25, 2026 at 11:18 am

    Right after $full_tmp = imagecreatetruecolor($full_width, $full_height);, add…

    if ($thumb_width > $thumb_height) {
        $thumb_offset = array('x' => ($thumb_width/2 - 25), 'y' => 0);
    } else {
        $thumb_offset = array('x' => 0, 'y' => ($thumb_height/2 - 25));
    }
    
    $square_tmp = imagecreatetruecolor($thumb_width, $thumb_height);
    
    imagecopyresampled($square_tmp, $src, 0, 0, $thumb_offset['x'], $thumb_offset['y'], 50, 50, $width, $height);
    

    Then save and destroy the temp like the other two images.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had everything working great in 1.4.0.2 but updated to 1.6.0.0 and now I
I have the following code and it's working (as usual) in everything but IE.
Here is my code, everything is working fine the only problem is with the
I can't get my dojo working. I've tried everything. Here is the code: <!DOCTYPE
When I want to implement ViewModel I should cut everything c# code from my
Created a phonegap application using jQuery Mobile. Everything is working great for the most
I have a struts2 application which uses the struts2-rest-plugin v.2.2.3. Everything is working great
I've just deployed an app to App Engine and everything is working great. I'm
So I'm wiring up my first MasterPage, and everything is working great except for
Everything was working great with the build. Then this started popping up after the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.