0

I have the following code, thanks to Michael K here: php - Best way to blur images

I've tried numerous attempts with canvas.toDataURL() to save the blurred image from Canvas to a folder named "saved-image", with no luck. The closest I've gotten was a zero byte size file. Can someone suggest please?

header('Content-Type: image/jpeg');
$file = '1.jpg';
$image = imagecreatefromjpeg($file);

    /* Get original image size */
    list($w, $h) = getimagesize($file);

    /* Create array with width and height of down sized images */
    $size = array('sm'=>array('w'=>intval($w/4), 'h'=>intval($h/4)),
                   'md'=>array('w'=>intval($w/2), 'h'=>intval($h/2))
                  );                       

    /* Scale by 25% and apply Gaussian blur */
    $sm = imagecreatetruecolor($size['sm']['w'],$size['sm']['h']);
    imagecopyresampled($sm, $image, 0, 0, 0, 0, $size['sm']['w'], $size['sm']['h'], $w, $h);

    for ($x=1; $x <=40; $x++){
        imagefilter($sm, IMG_FILTER_GAUSSIAN_BLUR, 999);
    } 

    imagefilter($sm, IMG_FILTER_SMOOTH,99);
    imagefilter($sm, IMG_FILTER_BRIGHTNESS, 10);        

    /* Scale result by 200% and blur again */
    $md = imagecreatetruecolor($size['md']['w'], $size['md']['h']);
    imagecopyresampled($md, $sm, 0, 0, 0, 0, $size['md']['w'], $size['md']['h'], $size['sm']['w'], $size['sm']['h']);
    imagedestroy($sm);

        for ($x=1; $x <=25; $x++){
            imagefilter($md, IMG_FILTER_GAUSSIAN_BLUR, 999);
        } 

    imagefilter($md, IMG_FILTER_SMOOTH,99);
    imagefilter($md, IMG_FILTER_BRIGHTNESS, 10);        

/* Scale result back to original size */
imagecopyresampled($image, $md, 0, 0, 0, 0, $w, $h, $size['md']['w'], $size['md']['h']);
imagedestroy($md);  

// Apply filters of upsized image if you wish, but probably not needed
//imagefilter($image, IMG_FILTER_GAUSSIAN_BLUR); 
//imagefilter($image, IMG_FILTER_SMOOTH,99);
//imagefilter($image, IMG_FILTER_BRIGHTNESS, 10);       

imagejpeg($image);

imagedestroy($image);
BG1
  • 1
  • Did you see this: https://stackoverflow.com/questions/13198131/how-to-save-an-html5-canvas-as-an-image-on-a-server – Mark Jul 23 '20 at 13:00
  • @Mark Yes I did look at that and more. The link you sent me the end result resulted in the person having to do Ajax calls, and that solution assumes the image file is an UPLOAD.. that would actually convoluted what is working perfectly for me so far. I was a dev here could see how maybe I could maybe do a base64_decode() at the end of what I have and save file. Or even a click on a button to save the image would also be fine. – BG1 Jul 23 '20 at 13:34
  • OK, I think your question may be confusing then. What does any of it have to do with canvas? This PHP script returns the image to the browser. Are you saying you just want to save the image and not return it? If so then see https://www.php.net/manual/en/function.imagejpeg.php The function takes as argument 2 the location and name to save file as. If you want to return it as well then use imagejpeg twice, once to save and once to output. – Mark Jul 23 '20 at 13:47
  • @Mark Thank you Thank you Thank you for pointing me in the right direction works like a charm now! – BG1 Jul 23 '20 at 19:24

0 Answers0