1

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!

Robert S.
  • 131
  • 2
  • 12
  • 1
    Were any error messages displayed? First that caught my eye was a missing `)` in the line `if(!is_image($picture)) { die("not an image"; }`. – George Brighton Apr 15 '14 at 20:18
  • My bad, that's a typo. It isn't missing in the actual code. :-) No error message, the script just doesn't do anything. – Robert S. Apr 15 '14 at 20:24
  • I can spot several things to look at (e.g. `is_image()` isn't a function, and I'm not sure `Imagick`'s constructor can accept an instance of itself), but your life will be much easier if error reporting is enabled. Have a look at [this answer](http://stackoverflow.com/a/6575502/2765666). – George Brighton Apr 15 '14 at 20:28
  • `is_image()` is a custom function I made earlier. I enabled error reporting and it seems that you were right : `Can not process empty Imagick object` on line `$imgBlur->blurImage(5,3);` . How can I then process the same image to get two different versions? :-/ Thank you! – Robert S. Apr 15 '14 at 20:54
  • Given that `Imagick` will complain if you don't give it an image, I wouldn't bother checking beforehand. How about reading directly from `$inputIMG` again? – George Brighton Apr 15 '14 at 20:58
  • It certainly works! :-) I was hoping I could speed up the process a bit by not repeating the same editings. – Robert S. Apr 15 '14 at 21:14

1 Answers1

2

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.

George Brighton
  • 5,131
  • 9
  • 27
  • 36