使用php创建PDF或PPT文件的缩略图

使用php创建PDF或PPT文件的缩略图,php,pdf,thumbnails,powerpoint,imagick,Php,Pdf,Thumbnails,Powerpoint,Imagick,我想让用户上传的pdf或ppt的缩略图 我试过了 $file = 'Digital Signage.pptx'; $cache = $_SERVER['DOCUMENT_ROOT'].'/cache/'; $ext = "jpg";//just the extension $dest = $cache.$file.'.'.$ext; if (file_exists($dest)){ $img = new imagick(); $img->re

我想让用户上传的pdf或ppt的缩略图 我试过了

$file = 'Digital Signage.pptx';    
$cache = $_SERVER['DOCUMENT_ROOT'].'/cache/';    
$ext = "jpg";//just the extension    
$dest = $cache.$file.'.'.$ext;    
if (file_exists($dest)){
    $img = new imagick();
    $img->readImage($dest);
    header( "Content-Type: image/jpg" );
    echo $img;
    exit;    
 } else {    
    $img = new imagick($_SERVER['DOCUMENT_ROOT'].'/'.$file.'[0]');
    $img->setImageFormat($ext);    
    $width = $img->getImageheight();
    //$img->cropImage($width, $width, 0, 0);
    $img->scaleImage(105, 149, true);    
    $img->writeImage($dest);    
    header( "Content-Type: image/jpg" );
    echo $img;
    exit;
}
Bu这需要在服务器上安装imagic,我的主机不允许我,因为我在共享主机上。是否有其他方法可以做到这一点


我也可以使用exec命令,因为它不安全,而且我一定不会使用它。

使用imagemagick还有其他选择。也许可以使用PHPGD来创建您的缩略图。该函数称为imagecopyresized

调整大小工作原理的示例代码:

<?php 
 // The file you are resizing 
 $file = 'your.jpg'; 

 //This will set our output to 45% of the original size 
 $size = 0.45; 

 // This sets it to a .jpg, but you can change this to png or gif 
 header('Content-type: image/jpeg'); 

 // Setting the resize parameters
 list($width, $height) = getimagesize($file); 
 $modwidth = $width * $size; 
 $modheight = $height * $size; 

 // Creating the Canvas 
 $tn= imagecreatetruecolor($modwidth, $modheight); 
 $source = imagecreatefromjpeg($file); 

 // Resizing our image to fit the canvas 
 imagecopyresized($tn, $source, 0, 0, 0, 0, $modwidth, $modheight, $width, $height); 

 // Outputs a jpg image, you could change this to gif or png if needed 
 imagejpeg($tn); 
 ?> 

更新:另外,如果要从PDF创建图像,请尝试imagecreatefromstring函数,然后根据该函数的图像结果调整大小。

我知道我回答这个问题太晚了,但这可能对其他人有所帮助


除了使用imagick或ghost scripta命令行工具外,没有其他方法可以完成上述任务,因此,最后我找到了一种方法,在另一台专用服务器上启用imagick扩展,并在那里生成图像,然后将它们下载到我的站点所在的服务器上,再次推迟到这个问题,但是您可以考虑使用一个在线转换服务。他们有API,您可以上传PPTX并下载每张幻灯片的png。大多数情况下不是免费的,但这可能比你自己做的转换更容易


在我对这个问题的研究中,我遇到了很多问题。我也没有用过,所以我不能报告转换的质量

您需要在此处转义单引号的位置?很抱歉,此主题的标题更新不正确。。。我已经使用cloudconvert实现了,它运行得非常好。。。在请求转换和完成转换之间大约有一到两分钟的延迟。我查看了zamzar,但他们没有在完成时回调选项。这意味着您的代码将阻止等待响应,或者您将需要一个轮询循环来检查是否完成。实现cloudconvert并不难,它需要一些尝试和错误。你能告诉我wordpress使用什么为不同的文件创建缩略图吗?