Search code examples
phpgdjcrop

Saving a cropped image using php


I used JCrop to crop an image using the code I got from this website : http://deepliquid.com/content/Jcrop_Implementation_Theory.html

php code:

$targ_w = $targ_h = 150;
$jpeg_quality = 90;

$img_r = imagecreatefromjpeg($_FILES['afile']['tmp_name']);
$dst_r = ImageCreateTrueColor( $targ_w, $targ_h );

imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
$targ_w,$targ_h,$_POST['w'],$_POST['h']);

header('Content-type: image/jpeg');
imagejpeg($dst_r, null, $jpeg_quality);

Essentially what the php does is print the cropped image onto the screen.

However I would like to store this image in my uploads file on my server. Normally I would use move_uploaded_file(), but in this particular instance, I'm not sure what parameter value to give to move_uploaded_file(). Can someone point me in the right direction?


Solution

  • You can then change:

    header('Content-type: image/jpeg');
    imagejpeg($dst_r, null, $jpeg_quality);
    

    into:

    imagejpeg($dst_r, 'uploads/sample-file.jpg', $jpeg_quality);
    

    The second parameter of imagejpeg is location where you want to save the file. Of course 'uploads/sample-file.jpg' is only sample, you need to change it to make it unique to not override other files