Php jquery jcrop and imagejpeg

This is my first approach to GD. I am trying to implement resizing and cropping using jcrop jquery plugin. I still cannot figure out how to save the image that I cropped. There is not much jcrop on the site. here is my code:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $targ_w = $targ_h = 150;
    $jpeg_quality = 90;

    $src = 'demo_files/flowers.jpg';
    $img_r = imagecreatefromjpeg($src);
    $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);

    exit;
}

What should I do with imagejpeg ($ dst_r, null, $ jpeg_quality) to actually write the image file and save its path in my db?

Thanks in advance.

Mauro

+3
source share
1 answer

If you want to save the file instead of its output, complete the following two tasks:

  • Delete line header('Content-type: image/jpeg');
  • Change the following line after that to imagejpeg($dst_r, 'path/to/output.jpg', $jpeg_quality);

See docs for function imagejpeg()in php.net/imagejpeg

+3
source

Source: https://habr.com/ru/post/1796530/


All Articles