Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to convert PDF to JPG Using ImageMagick in PHP

    • 0
    • 0
    • 0
    • 0
    • 1
    • 0
    • 0
    • 0
    • 5.41k
    Comment on it

    Hello Reader's! Today in my blog I am going to explain how you can convert PDF to JPG image using ImageMagick in PHP. ImageMagick is a open-source software  for displaying, converting, and editing image files in PHP.

    First you need to configure and install ImageMagick on your system.

    Run this commend on your Ubuntu terminal

    sudo apt-get install php5-imagick
    sudo php5enmod imagick

    Get the PDf file and convert to JPG

    if($_FILES)
    {	
        $output_dir = "uploads/";	
        ini_set("display_errors",1);
        if(isset($_FILES["myfile"]))
        {
            $RandomNum   = time();
            
            $ImageName      = str_replace(' ','-',strtolower($_FILES['myfile']['name']));
            $ImageType      = $_FILES['myfile']['type']; //"image/png", image/jpeg etc.
         
            $ImageExt = substr($ImageName, strrpos($ImageName, '.'));
            $ImageExt       = str_replace('.','',$ImageExt);
            if($ImageExt != "pdf")
            {
                $message = "Invalid file format only <b>\"PDF\"</b> allowed.";
            }
            else
            {
                $ImageName      = preg_replace("/\.[^.\s]{3,4}$/", "", $ImageName);
                $NewImageName = $ImageName.'-'.$RandomNum.'.'.$ImageExt;         
                move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir. $NewImageName);        
    	    $location= __DIR__;
                $name       = $output_dir. $NewImageName;
                $num = count_pages($name);
                $RandomNum   = time();
                $nameto     = $output_dir.$RandomNum.".jpg";        
                $location . " " .  $convert    = $location . " " . $name . " ".$nameto;
    			exec("convert ".$convert);
                for($i = 0; $i<$num;$i++)
                {
                  $display .= "<img src='$output_dir$RandomNum-$i.jpg' title='Page-$i' /><br>";            
                }
                $message = "PDF converted to JPEG sucessfully!!";
            }
        }
    }

    Count all converted page

    function count_pages($pdfname) {
          $pdftext = file_get_contents($pdfname);
          $num = preg_match_all("/\/Page\W/", $pdftext, $dummy);
          return $num;
        }

    Put All code together

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
      "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
      <head>
        <title>Convert PDF to JPEG</title>
        <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
        
        <style>
    	body{ font-family:Tahoma, Geneva, sans-serif; color:#000; font-size:11px; background-color:#D3D3D3; margin:0; padding:0;}
    	</style>
        <style type="text/css">
    	#upload-form {
    		padding: 40px;
    		background: #D3D3D3;
    		border: 1px solid #000;
    		margin-left: auto;
    		margin-right: auto;
    		width: 400px;
    	}
    	#upload-form input[type=file] {
    		border: 1px solid #ddd;
    		padding: 4px;
    	}
    	#upload-form input[type=submit] {
    		height: 30px;
    	}
    </style>
        
        <style type="text/css">    
          img {border-width: 0}
          * {font-family:'Lucida Grande', sans-serif;}
        </style>
      </head>
      <body>
        
    <?php
    
    $message = "";
    $display = "";
    if($_FILES)
    {	
        $output_dir = "uploads/";	
        ini_set("display_errors",1);
        if(isset($_FILES["myfile"]))
        {
            $RandomNum   = time();
            
            $ImageName      = str_replace(' ','-',strtolower($_FILES['myfile']['name']));
            $ImageType      = $_FILES['myfile']['type']; //"image/png", image/jpeg etc.
         
            $ImageExt = substr($ImageName, strrpos($ImageName, '.'));
            $ImageExt       = str_replace('.','',$ImageExt);
            if($ImageExt != "pdf")
            {
                $message = "Invalid file format only <b>\"PDF\"</b> allowed.";
            }
            else
            {
                $ImageName      = preg_replace("/\.[^.\s]{3,4}$/", "", $ImageName);
                $NewImageName = $ImageName.'-'.$RandomNum.'.'.$ImageExt;         
                move_uploaded_file($_FILES["myfile"]["tmp_name"],$output_dir. $NewImageName);        
    			$location= __DIR__;
                $name       = $output_dir. $NewImageName;
                $num = count_pages($name);
                $RandomNum   = time();
                $nameto     = $output_dir.$RandomNum.".jpg";        
                $location . " " .  $convert    = $location . " " . $name . " ".$nameto;
    			exec("convert ".$convert);
                for($i = 0; $i<$num;$i++)
                {
                  $display .= "<img src='$output_dir$RandomNum-$i.jpg' title='Page-$i' /><br>";            
                }
                $message = "PDF converted to JPEG sucessfully!!";
            }
        }
    }
    	function count_pages($pdfname) {
    		  $pdftext = file_get_contents($pdfname);
    		  $num = preg_match_all("/\/Page\W/", $pdftext, $dummy);
    		  return $num;
    		}
        
        $content ='<br><form enctype="multipart/form-data"  id="upload-form"  action="" method="post">
    <h2>How to convert PDF to JPEG in PHP Example</h2><br />
     Select PDF File: <input name="myfile" type="file" /><br /><br />
     <input type="submit" value="Upload" />
     </form> <h2>'.$message.'</h2><br />'.$display.'';
       
    echo $content;
    ?>
    </body>
    </html>

     

    I hope you like this please feel free to comment.

 1 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: