1
<?php
   $im = new Imagick();
   if ($im->pingImage('1.jpg')) {
       $image_info = $im->identifyImage();
       print_r($image_info);
   }
   else {
       echo 'image doesn't exist';
   }
?>

If I pass to pingImage a real image name, then print_r shows an array with parameters. But if I pass to pingImage not a real image name, nothing happens, but I expect output of "image doesn't exist". At the same time if I add an echo output before the condition, this echo does work.

Generally code is working until pingImage return false.

I haven't find the answer in the Imagick documentation.

serg66
  • 1,148
  • 1
  • 17
  • 31

2 Answers2

2

You got syntax error in your code, in else clause. it should be:

echo "image doesn't exist";

or

echo 'image doesn\'t exist';

(event SO highlighting should tell you there's something wrong :). And make your code more clear and use file_exists() first to see if what you expect to be file name really points to a file you can access:

if( file_exists( $fileName ) ) {
   $im = new Imagick();
   if ($im->pingImage( $fileName )) {
       $image_info = $im->identifyImage();
       print_r($image_info);
   } else {
     printf("Can't get image attributes of %s", htmlspecialchars( $fileName ));

   }
} else {
   printf("Can't open file %s", htmlspecialchars( $fileName ));
}
Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • Oh, sorry, I typed 'image doesn't exist' just for example, in my real code it was '11111' ) – serg66 Sep 01 '12 at 02:42
  • Thank you for the answer! But if a file exists it fails again. For example if I have a broken or empty file "fake-jpg.jpg". Function `file_exists` return `true`, but `pingImage` stops execution of code. – serg66 Sep 01 '12 at 02:48
  • Well, one guess it that `pingImage` is simply buggy. It looks like elementary bug, but still. And you can check its sources to ensure. Or, depending on how problematic it is for you, simple workaround would be to check file size too prior calling `pingImage` or check output to see if it is valid. – Marcin Orlowski Sep 01 '12 at 08:19
  • As it turned out the case was in try - catch. Anyway thank you for help. – serg66 Sep 01 '12 at 11:36
  • Yep, PHP manual sucks sometimes, so please go to http://php.net/manual/en/imagick.pingimage.php and put a comment for reference (and to save others' time) – Marcin Orlowski Sep 01 '12 at 11:40
0

I found the answer in another question: https://stackoverflow.com/a/4613341/678026 All I needed to do is to apply try - catch construction:

$im = new Imagick();
try {
    $im->pingImage('3.jpg');
}
catch(ImagickException $e) {
    echo "image doesn't exist";
}
Community
  • 1
  • 1
serg66
  • 1,148
  • 1
  • 17
  • 31