Create a thumbnail from uploaded pdf

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
eloyucu
Posts: 1
Joined: 2016-02-02T05:21:33-07:00
Authentication code: 1151

Create a thumbnail from uploaded pdf

Post by eloyucu » 2016-02-02T05:31:04-07:00

I am trying to make a preview from pdf that users must to upload. I am using 1and1 hosting server, so I don´t have total control about what to install, and I don´t know how to install ImageMagick. I followed these steps https://help.1and1.com/hosting-c37630/w ... 49013.html and I was using this code (that is working in a different project using a VPS):

Code: Select all

private function preViewPDF($filename)
{
    $img_path = './assets/uploads/previews';
    $file_name = explode(".", $filename)[0].".jpg";
    $dir = './assets/upload/files/';

    $img = new Imagick($dir."/".$filename.'[0]');
    $img->setImageFormat('jpg');
    $img->writeImage($img_path."/".$file_name);
    return "previews/".$id.$type."/".$file_name;
}
After try that and get Imagick Class not Found Exception, I am trying to convert using exec command:

Actual code

Code: Select all

private function preViewPDF($filename)
{
    $file_name = explode(".", $filename)[0].".jpg";
    $dir = getcwd().'/assets/uploads/files/';
    if(file_exists($dir."/".$filename))
    {
        exec("convert ".$dir."/".$filename.'[0]'." ".$dir."/".$file_name, $output, $return_var);
        var_dump($output);
        echo "<br>";
        var_dump($return_var);
    }
    else echo "no file";
    echo "<br>".$dir."/".$filename.'[0]'."<br>";
    echo "<br>".$file_name."<br>";
}
The var_dump($output); throws an empty array. And the $return_var is 1... general error :(
If I change the value between [] (the number of the page I want to convert) $output throws:

Code: Select all

array(3) {   
    [0]=> string(0) ""   
    [1]=> string(70) "Requested FirstPage is greater than the number of pages in the file: 1" 
    [2]=> string(53) " No pages will be processed (FirstPage > LastPage)."   
} 
So... any ideas what am I doing wrong?? Thank you.

Extra Data
Only two little things more (maybe obvious). The first, if I emulate the order on a SSL connection it works (I get a image from a pdf). And second, permissions are not the reason (I tried to create and write a file -with fopen and fwrite- and it works).

Oooopppss More data
First, an explanation about my actual code:

Code: Select all

$file_name = explode(".", $filename)[0].".jpg"; 

This line is because the extension of $filename is .pdf, so I need remove this part and concatenate the right extension .jpg (from hello.pdf I get hello.jpg).

Code: Select all

$dir = getcwd().'/assets/uploads/files/';  
This is the folder where the pdf is uploaded and the jpg preview must be saved.

Code: Select all

if(file_exists($dir."/".$filename))

I put this line, simply because I though that the uploading of the pdf wasn´t finished and this was the reason that doesn´t work.

Code: Select all

exec("convert ".$dir."/".$filename.'[0]'." ".$dir."/".$file_name, $output, $return_var); 

This is the line where the command convert is executed... but doesn´t work.

Second thing is a new simple code I just tried:

Code: Select all

if(file_exists("./DpRPJTmfSArPRuGZrOddLendfbhgHTrydwukMRvOMuSzVMDuBb.pdf"))
{
    exec("convert ./DpRPJTmfSArPRuGZrOddLendfbhgHTrydwukMRvOMuSzVMDuBb.pdf[0] ./DpRPJTmfSArPRuGZrOddLendfbhgHTrydwukMRvOMuSzVMDuBb.jpg", $output, $return_var);
    var_dump($output);
    echo "<br>";
    var_dump($return_var);
}
else echo "no hay fichero";
The $output is empty, and the $return_var is 1.

Post Reply