简体   繁体   English

将HTML格式的PDF文件转换为JPG文件-PHP / HTML

[英]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. 我正在尝试将单页pdf转换为jpg文件,以便可以将其作为图像上传到数据库。 I am having some problems with Imagemagick ( nothing happens when I read the file and write it, although I don't get any errors). 我在使用Imagemagick时遇到了一些问题(虽然我没有看到任何错误,但在读取和写入文件时没有任何反应)。

Here is the form in html: 这是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: 这是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. 就像我之前说的:我正在尝试通过html表单上传PDF文件,然后将其更改为JPEG文件。 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. 专注于Imagick对象,我知道查询对于我要执行的操作是不正确的,但是现在这不是问题。 I'm just trying to save the pdf as an image :). 我只是想将pdf保存为图片:)。 Thank you for your help! 谢谢您的帮助!

You can make use of ImageMagick, If you are using a Ubuntu server you can install image magic by 您可以使用ImageMagick,如果您使用的是Ubuntu服务器,则可以通过以下方式安装image magic

sudo apt-get install imagemagick

and run it by convert input.pdf output.jpg 并通过convert input.pdf output.jpg运行它

you can run the command from PHP it self by using 您可以使用以下命令从PHP自身运行命令

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

You can use Advanced Image Converter https://www.2jpeg.com/ 您可以使用高级图像转换器https://www.2jpeg.com/

after install call it from php 安装后从php调用它

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM