1

I have been stumbling over this for a couple of days. I have an imagick object $img that reads a pdf of two pages, in a webservice. I am trying to save each page as jpg image.

for($i = 0;$i < $num_pages; $i++){
   file_put_contents(logfile,"index = ".$i." \r\n\r\n",FILE_APPEND);
   $img->setIteratorIndex($i);
   $img->writeImage('workorders/'.$jobSFID.$i.".jpg");
}

file_put_contents(logfile,"test 7 \r\n\r\n",FILE_APPEND);

When I remove the loop and save one page only... all goes well. But with the loop, saving the two pages, it breaks down, never reaches test 7 logging statement, and gives 503 server temporarily unavailable some times, and other times 502 proxy error reading from remote server.

Alexandre Neukirchen
  • 2,713
  • 7
  • 26
  • 36
Ali H
  • 9
  • 2
  • What is your maximum execution time for php scripts? How long does it take to render one jpg image? The script could reach this timeout and would then be terminated, which - depending on your configuration - could result in the error message that you describe – sotn0r Jan 16 '17 at 12:30
  • Did you enable PHP errors? `ini_set('display_errors', 1);`. Maybe you are then able to see the error and fix it. – Red Jan 16 '17 at 12:31
  • try adding usleap(10), or more imagick will take time create image for($i = 0;$i < $num_pages; $i++){ usleep(10); file_put_contents(logfile,"index = ".$i." \r\n\r\n",FILE_APPEND); $img->setIteratorIndex($i); $img->writeImage('workorders/'.$jobSFID.$i.".jpg"); } – Vivek Chaudhari Jan 16 '17 at 12:33
  • How about some server logs? – degr Jan 16 '17 at 12:38
  • @sotn0r I have set the maximum execution time in my webservice for the sake of testing: ini_set('max_execution_time', 120000); ini_set("memory_limit", "6400M"); No luck – Ali H Jan 16 '17 at 13:19
  • I'd recommend getting errors to display, http://stackoverflow.com/questions/1053424/how-do-i-get-php-errors-to-display Figuring this out without the error is hard. – Danack Jan 16 '17 at 15:27

1 Answers1

0

There is likely to be another problem with your code, that turning on error reporting, or looking in the appropriate log file will reveal, however your code probably doesn't do quite what you want it to do.

Altough setting the "iteratorIndex" affects which image is current in a sequence of images, it doesn't actually separate that particular image out.

It should be much more reliable to separate a single image with Imagick::getImage to ensure that only a single page is written.

for($i = 0; $i < $num_pages; $i++){
   $img->setIteratorIndex($i);
   $pageImage = $img->getImage();
   $pageImage->writeImage('workorders/'.$jobSFID.$i.".jpg");
}
Danack
  • 24,939
  • 16
  • 90
  • 122
  • Thanks for your suggestions... but it didn't work.. I checked php.log file on the server after enabling errors display... no trace for an error... – Ali H Jan 17 '17 at 09:01
  • I have also granted write permission on the server for "Workorders" directory... no luck neither – Ali H Jan 17 '17 at 09:03