简体   繁体   中英

How to crop image using GD image functions

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 (50x50)

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.

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.

Take a look at the parameters that should be passed to imagecopyresampled , as per the PHP manual:

imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )

From the third parameter on, you basically define how a rectangle on the source image maps to a rectangle on the destination image.

So the first thing you have to do is calculate the rectanle ( x , y , width and height ) which defines the visible area of your original image. These will be the 5th, 6th, 9th and 10th parameters to the function, respectively.

For the destination rectangle, use 0,0 for x,y , and $thumb_width,$thumb_height for w,h , just as you are currently doing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM