trying to use imagemagick for the first time

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
stupidquestion
Posts: 4
Joined: 2012-07-31T03:32:29-07:00
Authentication code: 15

trying to use imagemagick for the first time

Post by stupidquestion »

Hi all, first time user here, need a bit of help. I installed imagemagick in Linux using "sudo apt-get install imagemagick". I'm trying to follow the basic examples listed at http://www.php.net/manual/en/imagick.examples-1.php

The first sample script is:

Code: Select all

<?php

header('Content-type: image/jpeg');

$image = new Imagick('image.jpg');

// If 0 is provided as a width or height parameter,
// aspect ratio is maintained
$image->thumbnailImage(100, 0);

echo $image;

?>
So I save this script in my web folder, and put a test image called "image.jpg" there (image.jpg is visible when accessed in the browser). But when I open the script in the browser, I get a broken image icon.

Then I tried to follow the second example script:

Code: Select all

<?php

$images = new Imagick(glob('images/*.JPG'));

foreach($images as $image) {

    // Providing 0 forces thumbnailImage to maintain aspect ratio
    $image->thumbnailImage(1024,0);

}

$images->writeImages();

?>
I changed the path accordingly (from glob('images/*.JPG') to glob('*.jpg') but when I open the script, I get a server error (HTTP error 500). I restarted the apache2 service but it didn't help. What am I missing?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: trying to use imagemagick for the first time

Post by Bonzo »

You have installed Imagemagick and your code is for Imagick - Imagick needs to be installing seperatly to Imagemagick or it might just need enabaling in your php.ini file.

When you view the contents of the php.ini file is Imagick listed ( Imagemagick will probably not be listed ).
stupidquestion
Posts: 4
Joined: 2012-07-31T03:32:29-07:00
Authentication code: 15

Re: trying to use imagemagick for the first time

Post by stupidquestion »

Bonzo wrote:You have installed Imagemagick and your code is for Imagick - Imagick needs to be installing seperatly to Imagemagick or it might just need enabaling in your php.ini file.

When you view the contents of the php.ini file is Imagick listed ( Imagemagick will probably not be listed ).
Thanks! I had to also install php-pear, libmagickwand-dev, libmagickcore-dev, then add extension=imagick.so to php.ini. Now the 1st script works!

By the way, the 2nd example script above is supposed to "Make a thumbnail of all JPG files in a directory", but when I run it, nothing seems to happen to the files, and no thumbnail file is created. Is this expected behavior from that script as it's written, or did I do something wrong?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: trying to use imagemagick for the first time

Post by Bonzo »

File must be .JPG .jpg will not be read; you can modify the glob to read .JPG and .jpg but you will need to look it up as I can not remember how offhand.
stupidquestion
Posts: 4
Joined: 2012-07-31T03:32:29-07:00
Authentication code: 15

Re: trying to use imagemagick for the first time

Post by stupidquestion »

Bonzo wrote:File must be .JPG .jpg will not be read; you can modify the glob to read .JPG and .jpg but you will need to look it up as I can not remember how offhand.
I changed the pictures' extension to JPG, but nothing happens. What's wrong with the lower-case .jpg anyway?

Well, maybe the manual is not very clear: the example calls writeImages without parameters, but documentation says writeImages takes 2 parameters, which don't seem to be optional, and no official explanation is given for the parameters.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: trying to use imagemagick for the first time

Post by Bonzo »

glob is case sensative so .jpg is not the same as .JPG

You will have a LOT of headaches with Imagick.

Try this:

Code: Select all

<?php
$images = (glob("images/{*.jpg,*.JPG}", GLOB_BRACE));
foreach($images as $image) {
$im = new Imagick($image);
$im->thumbnailImage(100,0);
$newname = str_replace( 'images/', '', $image);
//echo $newname;
$im->writeImage($newname);
}
?>
stupidquestion
Posts: 4
Joined: 2012-07-31T03:32:29-07:00
Authentication code: 15

Re: trying to use imagemagick for the first time

Post by stupidquestion »

Bonzo wrote:glob is case sensative so .jpg is not the same as .JPG
Haha I thought you meant the program only takes upper-case (if that was the case then the first script shouldn't have worked). Nah, the problem was file permission: enabling write permission on the folder for the program solved the problem. Thanks!
Post Reply