PHP PDF 转 JPG 图片 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.

<?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.

 

原文:http://www.phpgang.com/how-to-convert-pdf-to-jpeg-in-php_498.html

下载:convert-pdf-to-jpeg-in-php

转自:PHP PDF 转 JPG 图片 Make PDF to JPEG Converter in PHP

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值