Question

I'm very new to PHP and this is the very first time I've tried to use Imagick, so there must be plenty wrong with the script I wrote in order to achieve my goal. :-)

What I'd like to do:

  1. Convert an image that was uploaded to JPEG and resize it;
  2. Make a blurred version of it;
  3. Move the two images to a certain directory.

I wrote a function that's supposed to do it, but, as you guessed, it doesn't do anything at all:

function convertAndBlur($inputIMG, $dst, $width, $quality) {
   $img = new Imagick($inputIMG);
   $img->scaleImage($width, 0, true); // keep aspect ratio
   $img->setImageFormat('jpeg');
   $img->setImageCompressionQuality($quality);
   $img->writeImage($dst . ".jpg");

   $imgBlur = new Imagick($img);
   $imgBlur->blurImage(5,3);

   $imgBlur->writeImage($dst . "_blur.jpg");

   $img->clear(); $img->destroy();
   $imgBlur->clear(); $imgBlur->destroy(); 
}

The function is called this way:

$picture = $_FILES["picture"]["tmp_name"];
$pictureName = "foo";

if(!is_image($picture)) { die("not an image"); }

$picturePath = "uploads/" . $pictureName;

convertAndBlur($picture, $picturePath, 1000, 90);

This will certainly make some of you roll their eyes, but again, that's completely uncharted territory to me. Could anyone help me out? Thanks!

Was it helpful?

Solution

Imagick's constructor cannot take an instance of Imagick as an argument. To create another object, try $imgBlur = clone $img; in place of $imgBlur = new Imagick($img);, which will create a carbon copy of $img. Read more about cloning Imagick objects here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top