PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, September 18, 2022

[FIXED] How to convert .pdf from post method to .jpg in PHP

 September 18, 2022     forms, imagick, php     No comments   

Issue

so while developing a webapp I encountered a problem I could not find an answer to. Let's say we have this html

<form method="post" enctype="multipart/form-data" action="/upload.php">
    <label> Upload .pdf here 
        <input name="menu" type="file" accept=".pdf"> 
    </label>  
    <button>Submit</button>
</form>

And now this PHP to handle it:

if ($_SERVER['REQUEST_METHOD'] == 'POST'){
        $menu = $_POST['menu'];

        //convert whatever type menu is to something readable for Imagick

         // create Imagick object
        $imagick = new Imagick();
        // Reads image from PDF
        $imagick->readImage($menu);
        // Writes an image or image sequence Example- converted-0.jpg, converted-1.jpg
        $imagick->writeImages('menu.jpg', false);
        echo $imagick;

I couldn't find any solution anywhere else so I thought I'd ask my first ever queston on stackoverflow. Thanks for helping me out!


Solution

If you upload a file with name "menu", the actual file data is contained in $_FILES['menu']["tmp_name"] on submission to a php script.

Hence, please change $_POST['menu'] in your code to $_FILES['menu']["tmp_name"] for further processing.

HTML

<form method="post" enctype="multipart/form-data" action="pdfconvert.php">
    <label> Upload .pdf here 
        <input name="menu" type="file"> 
    </label>  
    <button>Submit</button>
</form>

pdfconvert.php (I amended the codes so as to handle multiple pages in a pdf file)

<?php

$im = new Imagick($_FILES['menu']["tmp_name"]);


$pages = $im->getNumberImages(); 



for ($i = 0; $i < $pages; $i++) { 

$url = $_FILES['menu']["tmp_name"].'['.$i.']'; 

$image = new Imagick($url);
$image->setImageFormat("jpg"); 
$image->writeImage('menu_'. $i. '.jpg'); 

echo "writing page " . $i. "<br>"; 
}

?>

Last but not least:

  1. please make sure that the target folder is writable (otherwise the system cannot write the jpg files
  2. it is better to create a separate folder (e.g. ./upload/) for the processing so that the system will not "mix" the generated jpg files with your php scripts
  3. better add a random string to the generated file names so that even if you upload two files with the same name, the old files will not get overwritten.


Answered By - Ken Lee
Answer Checked By - Katrina (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing