Gašper Božič
Gašper Božič

Reputation: 59

Converting a PDF file to JPG file obtained in html form - PHP/HTML

I'm trying to convert a pdf with a single page into jpg file so that I can upload it as a image to a database. I am having some problems with Imagemagick ( nothing happens when I read the file and write it, although I don't get any errors).

Here is the form in html:

<form enctype="multipart/form-data" class="generalForm" onsubmit="return validateForm()" action="../includes/connect.php" method="POST">
            <label class="headLabel">DODAJ</label>
            <br>
            <br>
            <input class="inputTextSub" id="naziv" name="naziv" placeholder="naziv" onfocus="this.placeholder = ''" onblur="this.placeholder = 'naziv'" type="text">
            <br>
            <br>
            <input class="inputTextSub" id="opis" name="opis" placeholder="opis" onfocus="this.placeholder = ''" onblur="this.placeholder = 'naziv'" type="text">
            <br>
            <br>
            <input class="inputTextSub" id="datumz" name="datumz" type="date">
            <br>
            <br>
            <input class="inputTextSub" id="datumk" name="datumk" type="date">
            <br>
            <br>
            <label class="inputTextSub">prioriteta</label>
            <select class="inputSelSub" id="pr" name="pr">
                <option value=1>1</option>
                <option value=2>2</option></select>
            <br>
            <br>
            <label class="inputTextSub">format</label>
            <select class="inputSelSub" id="format" name="format">
                <option value=A3>A3</option>
                <option value=A4>A4</option></select>
            <br>
            <br>
THIS --->   <input type="file" name="inputfile" id="file" class="inputfile" />
            <br>
            <line id="addWarn" style="font-family: 'Ubuntu'; font-size:1em; -webkit-text-fill-color: white; letter-spacing: 2px;"></line>
            <br>
            <br>
            <input class="button" style="margin-top:2em;" name="save" type="submit" value=" shrani ">
        </form>

and here is the code in php:

if(isset($_POST['save'])){
if(!$_POST['naziv']==""&&!$_POST['opis']==""&&!$_POST['datumz']==""
&&!$_POST['datumk']==""&&isset($_POST['pr'])
&&isset($_POST['format'])&&$_FILES['inputfile']['size']>0){
    $naziv = $_POST['naziv'];
    $opis = $_POST['opis'];
    $datumz = $_POST['datumz'];
    $datumk = $_POST['datumk'];
    $pr = $_POST['pr'];
    $format = $_POST['format'];
    $file = file_get_contents($_FILES['inputfile']['tmp_name']);
    $modId = $_SESSION['mod_id'];

    $img = new Imagick();
    $img->readImage($_FILES['inputfile']['tmp_name']);
    $img->writeImage('tempImg.jpg');


    $stmt = $GLOBALS['conn']->prepare(
    "INSERT INTO `deska`(`mod_id`, `naziv`, `opis`, `datumz`, `datumk`, 
    `prioriteta`, `slika`, `tip`, `datumSpremembe`) 
    VALUES (?,?,?,?,?,?,?,?,CURRENT_DATE)"
    );

    $stmt->bind_param('issssibs',$modId,$naziv,$opis,$datumz,$datumk,$pr,$null,$format);
    $stmt->send_long_data(6,$file);

    if(!$stmt->execute()){
        die(mysqli_error($GLOBALS['conn']));
    }
    $stmt->close();

    header("location: ../sub/dodaj.php");
    }else{
    header("location: ../sub/dodaj.php");

}

As I said earlier: I am triyng to get the PDF file uploaded via html form, then change it to a JPEG file. Concentrating on the Imagick object, I know the query is not correct for what I am trying to do, but that is not the problem now. I'm just trying to save the pdf as an image :). Thank you for your help!

Upvotes: 0

Views: 314

Answers (1)

Nijeesh Joshy
Nijeesh Joshy

Reputation: 1481

You can make use of ImageMagick, If you are using a Ubuntu server you can install image magic by

sudo apt-get install imagemagick

and run it by convert input.pdf output.jpg

you can run the command from PHP it self by using

$result = shell_exec('convert input.pdf output.jpg');

You can use Advanced Image Converter https://www.2jpeg.com/

after install call it from php

$result = shell_exec('2jpeg.exe -src “source” -dst “destination” -oper Rasterize res:300');

Upvotes: 0

Related Questions