1

I want to convert a PDF-Image to a .JPG-Image.

I made a convert.php which i can call with the filename of the PDF file and convert it to jpg. I call this function like this: http://www.example.ch/tools/img/cache/convert.php?f=MyPdf.pdf

this looks like this:

if ($f = @$_REQUEST['f']) {
    $f = htmlspecialchars(urldecode($f));
    $url  = 'http://www.example.ch/img/cache/'.$f;


    $file = strtolower($f);

    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US; rv:1.9.1.2) Gecko/20090729 Firefox/3.5.2 GTB5');

    $data = curl_exec($ch);

    $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

    curl_close($ch);
    if ($retcode == 200) {
        file_put_contents($file, $data);
        $converted_filename = str_replace('.pdf', '.jpg', $file );
        $cmd = 'convert '.$file.' '.$converted_filename;
        exec('convert '.$file.' '.$converted_filename);
        $fp = fopen($converted_filename, 'rb');

        header('Content-Type: image/jpeg');
        header("Content-Length: " . filesize($converted_filename));
        fpassthru($fp);
        exit;
    } else {
        header("HTTP/1.0 404 Not Found");
    }

}

After this is done i get an .jpg File with the size of 700KB in my folder and i can't open it because it is corrupted. And the HTML output is also like: The image can't be oppened because it contains errors..

KenS
  • 30,202
  • 3
  • 34
  • 51
MrTouch
  • 654
  • 2
  • 12
  • 28

1 Answers1

0

The best tool for above is ImageMagic.

Install Image magic in your system and refer this link

Convert PDF to JPEG with PHP and ImageMagick

It is very easy to do these kind of things Using ImageMagic. Also refer this link

http://php.net/manual/en/imagick.setimageformat.php

Hope this will solve your problem

Community
  • 1
  • 1
Anand G
  • 3,130
  • 1
  • 22
  • 28
  • To the best of my knowledge, convert is either part of, or uses, ImageMagick. Which uses Ghostscript to do the rendering of PDF files..... I can't really see that this is a Ghostscript question as it stands, it really ought to be an ImageMagick question. – KenS May 03 '13 at 08:07