0

i am trying to get images of a pdf file. but the problem is that my code produce error for some pdf files. probably file size problem

this is my code

$saved_file_location = "images.pdf";
$destination_dir = "t";
$Name = "123";

exec('convert -verbose -density 150 '.$saved_file_location.' -crop 1020x490+128+40 -quality 90 '.$destination_dir."/".$Name.'-%03d.jpg');
sleep(5);
//echo filesize($destination_dir."/".$Name.'-000.jpg'). "<br>";
if(filesize($destination_dir."/".$Name.'-000.jpg') < 10000)
{
    exec('convert -verbose -density 150 '.$saved_file_location.' -crop 1020x490+128+580 -quality 90 '.$destination_dir."/".$Name.'-%03d.jpg');
    sleep(5);
    //echo filesize($destination_dir."/".$Name.'-001.jpg')."<br>";
    if(filesize($destination_dir."/".$Name.'-001.jpg') < 10000)
    {
        exec('convert -verbose -density 150 '.$saved_file_location.' -crop 1020x490+128+1125 -quality 90 '.$destination_dir."/".$Name.'-%03d.jpg');
    }
}

and this error i get

PHP Warning:  filesize(): stat failed for t/123-000.jpg in /home3/domain/pdf_to_images.php on line 10

some time the code work fine and produce multiple images of pdf file but some time i get this error

3
  • No one is going to be able to help you. For some reason filesize is failing on your system, but only you can investigate what the cause of that is. Commented Nov 12, 2015 at 0:47
  • any suggestion how i can track this problem Commented Nov 12, 2015 at 2:54
  • Do filesize("ThisFileDoesntExist.txt"); - it gives the same error message. Either the file you're trying to read the size of doesn't exist or you don't have access to it. Commented Nov 12, 2015 at 4:31

1 Answer 1

1

First of all ImageMagick convert command internally uses ghostscript's gs command and gs is faster comparing to convert so it is always good to use ghostscript if you are not using ImageMagick specific features. and the other thing is rather than constructing the command by yourself I would recommend using library already written and tested to avoid waste of time trying to understand all the combinations of a commands.

you will find good libraries packagist.org

here a library I wrote which will be useful PdfLib who reads your question in the future

$pdflib = new ImalH\PDFLib\PDFLib();
$pdflib->setPdfPath($pdf_file_path);
$pdflib->setOutputPath($folder_path_for_images);
$pdflib->setImageFormat(\ImalH\PDFLib\PDFLib::$IMAGE_FORMAT_PNG);
$pdflib->setDPI(300);
$pdflib->setPageRange(1,$pdflib->getNumberOfPages());
$pdflib->convert();
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.