Faryal Khan
Faryal Khan

Reputation: 869

Image creation from pdf

I am creating a cron job in which I copy files from one directroy to another. The cron job works fine it copies the files to the import directory. The structure of import directory is like this.

import
      folder1
          pdf_file1
      folder2
          pdf_file2

I am trying to create a thumbnail image for each pdf file that is copied to the folders, I installed ImageMagik and my php code to create a thumnail image is

 if ($preview != "") {
    $copy_res = ftp_get("files/upload/" . $ftp_upload["path"] . "/" . $preview, "files/import/" . $ftp_upload["preview"] . "/" . $preview);
    $md5_checksum = md5("files/import/" . $ftp_upload["path"] . "/" . $preview);
 } else {
         //$pdf_file = 'files/import/folder1/pdf_file1';
         $pdf_file = 'files/import/' . $ftp_upload["path"]."/".$filename_new;
         if (file_exists($pdf_file)){
             echo 'I am here';
             exec("convert -density 300 " . $pdf_file . "[0]" . $filename_new . ".jpg");
         }
 }

When I run the code it comes to the else statement and echo the line but its not executing the exec command. I checked the convert command from Command Prompt for that I navigated to

 C:\wamp\www\project\files\import\folder1 

and then I run this command

 exec("convert -density 300  test.pdf[0]  test.jpg");

From the command prompt it worked but in my code it s not working . Is there any issue with the path ? because the file is alreday copied when I try to create the thumbnail for it.

The ImageMagik is installed in

 C:\Program Files(x86)\ImageMagik

Any ideas what I am doing wron ? and is there any other faster way to create thumbail image for pdf ?

Thanks in advance

Upvotes: 0

Views: 1201

Answers (2)

Bonzo
Bonzo

Reputation: 5299

I now write my code seperating the code form the exec() so that you can display the information you are sending to Imagemagick. I would also try setting the filenames outside the exec()

$input = $pdf_file . "[0]";
$output = $filename_new . ".jpg"
$cmd = " -density 300 $input -thumbnail 200x300 $output ";
// echo $cmd."<br>";
exec("convert $cmd");

Added a resize but it will keep the image proportions - if you need an exact size you could crop the image or pad it with a colour.

Upvotes: 1

Fad
Fad

Reputation: 9848

There seems to be a missing space that separates the path to the pdf file and the new name for the image file.

exec("convert -density 300 " . $pdf_file . "[0]" . $filename_new . ".jpg");

Should be:

exec("convert -density 300 " . $pdf_file . "[0] " . $filename_new . ".jpg");
-----------------------------------------------^

Upvotes: 0

Related Questions