Click here to Skip to main content
15,896,201 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
I have some pdfs in a folder called downloads and i want to generate thumbnails in jpg format using imagick in php but i am getting error as unable to create temporary file. error i am getting is this:


Fatal error: Uncaught exception 'ImagickException' with message 'unable to create temporary file `/home/vrwebqbj/public_html/virtuereal.com/KyoritsuElectric/admin/images/downloads/demoPDF.pdf': No such file or directory @ error/pdf.c/ReadPDFImage/397' in /home/vrwebqbj/public_html/virtuereal.com/KyoritsuElectric/admin/imagickTest2.php:14 Stack trace: #0 /home/vrwebqbj/public_html/virtuereal.com/KyoritsuElectric/admin/imagickTest2.php(14): Imagick->__construct('/home/vrwebqbj/...') #1 {main} thrown in /home/vrwebqbj/public_html/virtuereal.com/KyoritsuElectric/admin/imagickTest2.php on line 14


This is so far I tried :

<?php
error_reporting(1);
$source       = __DIR__."/images/downloads/demoPDF.pdf"; //Source PDF File
$target     = "myfile.jpg"; // Output File



/*function genPdfThumbnail($source, $target)
    {*/
        //$source = realpath($source);
        $target = dirname($source).DIRECTORY_SEPARATOR.$target;
        //echo $target;
        //echo __DIR__;
        $im     = new Imagick($source."[0]"); // 0-first page, 1-second page
        $im->setImageColorspace(255); // prevent image colors from inverting
        $im->setimageformat("jpeg");
        $im->thumbnailimage(160, 120); // width and height
        $im->writeimage($target);
        $im->clear();
        $im->destroy();
   // }
    //genPdfThumbnail('images/downloads/demoPDF.pdf','myfile.jpg');
?>
<img src="KyoritsuElectric/admin/images/downloads/myfile.jpg" />
Posted

1 solution

You are building the target path using the complete source path appended to your home directory. But imagemagick will not create non-existing directories but expects them to exist already (background: it just tries to create the file using a file open function that fails when the directory does not exist).

So you have two options:

  • Create the directory dirname($source) below your home directory before executing imagemagick, or
  • Use another existing directory to store the converted file.
 
Share this answer
 
Comments
[no name] 19-Jan-16 1:01am    
I changed my code as:
$source = "images/downloads/demoPDF.pdf"; //Source PDF File
$target = "images/downloads2/myfile.jpg"; // Output File

both downloads and downloads2 folders exist when seen through filezilla
Jochen Arndt 19-Jan-16 2:57am    
Where do they exist?
Both are relative pathes to the current working directory. If that is the valid upper path, the conversion should work.
[no name] 19-Jan-16 1:02am    
I didn't understand by the line: "Create the directory dirname($source) below your home directory before executing imagemagick"
Jochen Arndt 19-Jan-16 2:51am    
For your example, imagemagick fails at creating the file

'/home/vrwebqbj/public_html/virtuereal.com/KyoritsuElectric/admin/images/downloads/demoPDF.pdf'.

So you must create the directory '/public_html/virtuereal.com/KyoritsuElectric/admin/images/downloads' (which is returned by dirname($source)) below your home directory '/home/vrwebqbj'.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900