Search code examples
phpwordpresscropimage-uploadingjcrop

imagecopyresampled doesn't give real output


I am trying to crop and upload image, I have tried following code for crop image.

$img_r = imagecreatefromjpeg('/home/user/site.com/wp-content/themes/my-theme/uploads/test.jpeg');

$new_canvas = imagecreatetruecolor(350, 350);

imagecopyresampled($new_canvas, $img_r, 0, 0, 85, 13, 350, 350, 500, 500 );

imagejpeg($new_canvas, $src);
imagedestroy($new_canvas);

I have uploaded image 500*500 and want to crop it in size 350*350 image size top X and left Y coordinate from where need to start image cropping are 85px and 13px.

The problem is that when it saved to server mean I over write same image in server and check image is cropped but not in that particular area.

It make image small and add at left and right side black space in image.

Please let me know that what I am doing wrong.

Thanks in advance.


Solution

  • Found answer after too much work on it, it's a simple.

    just change:

    $img_r = imagecreatefromjpeg('/home/user/site.com/wp-content/themes/my-theme/uploads/test.jpeg');
    

    TO:

    $img_r = imagecreatefromjpeg('site.com/wp-content/themes/my-theme/uploads/test.jpeg');
    

    I am using directory path and there should be URI path.