March 3, 2024 5:01 am

Make PDF to JPEG Converter in PHP

We have published an article on How to Convert HTML to PDF in PHP with fpdf and received many emails to write something on conversion from PDF to JPEG so today I am going to show you how we can convert PDF to JPEG and its a 4 lines script and very easy and simple to understand. We use PHP imagick extension which is mostly built-in in PHP installation so no need to include any thing.

How to convert PDF to JPEG in PHP

[wpdm_file id=69]DEMO

Simple PHP Code:

<?php
$imagick = new Imagick();
$imagick->readImage('mytest.pdf');
$imagick->writeImage('output.jpg');
?>

In this code we have to give a PDF file and in output it will produce JPEG files for each page of your given PDF file.

If you want to convert first page of your PDF file only then define PDF file name like this mytest.pdf[0] and run the script it will show convert only first page of your PDF file.

If you are using shared hosting and there is most of the time imagick extension not compiled with PHP only binaries available so here is to code to convert PDF to JPEG with imagick binaries.

<?php
$location   = "/usr/local/bin/convert"; // Binaries Location
$name       = "myfile.pdf"; //Source PDF File
$nameto     = "myfile.jpg"; // Output File
$convert    = $location . " " . $name . " ".$nameto; // Command creating
exec ($convert); // Execution of complete command.

echo "PDF converted to JPEG!!";
?>

You have to change binaries location ($location   = “/usr/local/bin/convert”;) to your server location which you can get from your hosting admin.

I have used binaries to perform this task in demo and download code if you have a PHP with compiled imageick then use first code and edit it as per your requirement.

[wpdm_file id=69]DEMO

This is a very simple and easy tutorial mostly for PHP Beginners I hope you like it please write down your reviews in comments.

Author Huzoor Bux

I am Huzoor Bux from Karachi (Pakistan). I have been working as a PHP Developer from last 5+ years, and its my passion to learn new things and implement them as a practice. Basically I am a PHP developer but now days exploring more in HTML5, CSS and jQuery libraries.


Tutorial Categories:

60 responses to “Make PDF to JPEG Converter in PHP”

  1. Shahbaz Ahmed Bhatti says:

    Php Gang Team, Please Can u Post a Tutorial Email Attatchment with (pdf, or .doc or .jpg FIle) it nice to see pdf to jpg
    but the link is not working?
    http://demo.phpgang.com/convert-pdf-to-jpeg-in-php/

  2. binil billu says:

    when checking in my localhost with pdf file. the sample code not generating the images

  3. binil billu says:

    Hi,

    How to convert word files to jpeg ? .is that possible?

  4. ZeeStyl says:

    Can you please explain which binary will be used and should point to run the “exec” command?

  5. Edgarito says:

    hi, sorry bbut the code do not work, I have Imagick extension installed and I try many times your code but no responde…. only upload pdf file but no convert nothing… thanks..

  6. k balu says:

    Hi ,

    I am balu… I really liked your article. I installed imagick and path is C:imagick … But i getting this error shown below. Will you please help me to solve this issue….

  7. neetu says:

    in my localhost it is not working..please provide me library so i can implement

  8. Tran Huy says:

    Hello, I can not download the source code, I subscribed, but when I download the site show that I haven’t subscribed. Please check again! Thanks

  9. thanks, I am being found it!!

  10. Jai Agarwal says:

    I installed both demo script and imagick extension on my server but after uploading it is showing blank images. Please help me how to fix it, i noticed that same problem asked below but there is answer for it.

    • huzoorbux says:

      ini_set("display_errors",1);
      Add this line on top of the pages and run it again u must get some errors then share those errors here.

      • Jai Agarwal says:

        It is showing PDF converted to JPEG sucessfully!! but getting blank images and i’m also not getting them on my server folder location.

        Please help me.

  11. delarosa says:

    I can’t download the source code,i already subscribed but when i download the site show that i haven’t subscribed. Please check again..Thanks

  12. lanner says:

    Hello
    How to use this script directly without upload ? just with the name of the pdf file
    Thanks

  13. Gul Panra says:

    i have checked your tutorial i have configured my localhost xamp everything goes fine for image to image conversion but if i put .pdf file then its not working for this

    $imagick = new Imagick();
    $imagick->readImage(‘C:/xampp/htdocs/ican/images/ss.pdf[0]’);
    $imagick->writeImage(‘C:/xampp/htdocs/ican/images/sss.jpg’);

    also i need to put file path like in my code because images/ss.pdf etc its not working for images but if i put the path like above in my code then images are working but not working for pdf files thanks!

    i can see you are not replying lots of people commenting in this post may be you think we are fool to comment. If you are not going to reply back to your visitors or subscribers comment then shut this off from ur posts that please comment or comment box thanks!

  14. Caff says:

    i NEED HELP WITH THIS CODE
    the problem is if my PDF as 2 page I have 2 jpg I just 1 jpg how do i make 1 jpg
    right now like this

    123-0.jpg and 123-1.jpg
    and i just need

    123.jpg no the rest I’m not a very god programmer but I try to understand what I’m doing but I lost my self so easy can any one help me with this problem
    Thank to every one.

    $pid = mysqli_insert_id($con);
    // Place image in the folder
    mkdir (“uploads/$pid”, 0755);
    $output_dir = “uploads/$pid/”;

    $ImageName = preg_replace(“/.[^.s]{3,4}$/”, “”, $ImageName);
    $NewImageName = $pid.’.’.$ImageExt;

    move_uploaded_file($_FILES[“myfile”][“tmp_name”],$output_dir. $NewImageName);

    $location = “/usr/local/bin/convert”;
    $name = $output_dir. $NewImageName;
    $nameto = $output_dir.$pid.”.jpg”;
    $convert = $location . ” ” . $name . ” “.$nameto;
    exec($convert);
    {

    }
    $message = “PDF converted to JPEG sucessfully!!”;
    }

  15. TTom911 says:

    Your demo is NOT working ! Getting “success” message on your demo site bit no actual image is converted. Also in our local installation, with iMagick installed, same result: PDF uploaded but there are no converted images in the “uploads” folder. Any solution? Thanks !

  16. banana says:

    HELP PHP GANG! the source code for the demo is working on the localhost. but when i uploaded it on my site all i see is blank images. What to do? Pls pls pls pls help 🙁

  17. Guadalupe Lip Curo ☮ says:

    Hi, I downloaded your code and when I upload a pdf convert it into an another pdf, I don’t know if is maybe the route of the “convert.exe” or what? Please help me

  18. RC says:

    After using your code….
    image not display or upload folder only pdf upload image not upload
    ple help i am truble

  19. Karthick says:

    same problem as image not display in front of view

  20. Karthick says:

    example.

  21. Karthick says:

    Hai rc i can solve this pbm example at screen shots above

  22. skyfuckingwalker says:

    Can someone show how to solve this fucking problem?
    I got stuck in this tutorial and I can’t solve it.
    Please make a video explaining >:(

    I tried to install imagick but it doesn’t work.

  23. skyfuckingwalker says:

    Come on man, video tutorial please. There is nothing on youtube.

  24. Karthick says:

    are you using windows or linux

  25. thasneem077 says:

    Hi nice stuff. Thank you… But i need to set resolution of the jpg image…
    Please help me

    • Karthick says:

      Easily set resolution of the converted image using exec(convert -density 150 -quality 100 example.pdf example.jpg) function,but -density 150 -quality 100 is optional,which resolution u will be set.

  26. shafeeq says:

    this code is for convert pdf to image, is okey. but anyone could you help me to get php code for convert docs file to pdf file………..thanks

  27. nandar says:

    The code can’t generate images on my localhost with codeigniter imagick library,how to do, pls help to get images.

  28. Amith Dandin says:

    Bro, i have added your code in my website. its working fine.

    But in some case -> the converted image will look black. so unable to read that.

    so please do something.

  29. Sunny Patial says:

    Thankyou so much brother.. its working fine for me.. excellent tutorial. great job

  30. kishore Dharany says:

    i just tried this sample code in localhost…but the image was not display in the result page during upload…and only pdf file is upload in described directory but no jpg files…

  31. Buyi Wen says:

    I’m not a developer, i always use this free online pdf to jpg converter

  32. Buyi Wen says:

    I’m not a developer, i always use this free online pdf to jpg converter

  33. Rk Sinha says:

    can we generate png image from fpdf object

  34. Vikas Patil says:

    images are getting converted to separate pdf file not image files

  35. Mohd Farhan Rizwan says:

    Huzoor sir i’m a big fan of your. Please can you tell me how to convert Pdf to Doc using php

    Thanks in Advance.

  36. Abubakar Siddique says:

    Hi
    It’s not working fine even in demo

  37. chintan vadi says:

    can you give me your demo project of convert pdf to Image in php.

  38. Athira Ravindranathan says:

    Could someone please attach the code of demo url, while i click on upload button i couldnt see from view source code

Leave a Reply

Your email address will not be published. Required fields are marked *