简体   繁体   English

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

[英]Create thumbnail of PDF or PPT file using php

I want to make thumbnails of pdf or ppt being uploaded by the user I tried 我想使我尝试过的用户上传的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 this needs imagic to be intalled on server and my host does not allow me as I am on shared hosting is there any other method to do this 但是这需要在服务器上安装,而我的主机不允许我,因为我在共享主机上,是否有其他方法可以做到这一点?

I can use exec command also because it is not secure and I am bound not to use this 我也可以使用exec命令,因为它不安全,而且我一定不使用它

There are alternatives to using imagemagick. 有使用imagemagick的替代方法。 Perhaps use PHPGD instead to create your thumbnails. 也许改用PHPGD创建缩略图。 The function is called imagecopyresized 该函数称为imagecopyresize

Example code of how the resizing works: 调整大小的示例代码:

<?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); 
 ?> 

UPDATE: also, for creating the image from a PDF then maybe give imagecreatefromstring function a try, and then do the resize based on the image result from that. 更新:同样,要从PDF创建图像,则可以尝试使用imagecreatefromstring函数,然后根据图像结果调整大小。

I know I am too late to answer this question but, this may help some one else. 我知道我回答这个问题为时已晚,但这可能会对其他人有所帮助。

There is no way to do the above mentioned task except to use imagick or ghost script(a command line tool), so finally I found a way too enable the imagick extension on another dedicated server and generated images over there and then downloaded them on the server where my site was being hosted file_get_content() 除了使用imagick或ghost脚本(命令行工具)外,没有其他方法可以执行上述任务,因此最终我找到了一种方法,也可以在另一台专用服务器上启用imagick扩展,并在该服务器上生成图像,然后将其下载到托管我的网站的服务器file_get_content()

Again late to this question, but you might consider using one of the online conversion services. 再提这个问题很晚,但是您可以考虑使用一种在线转换服务。 They have API's where you can upload a PPTX and download png's of each slide. 他们有API,您可以在其中上传PPTX并下载每张幻灯片的png。 Mostly not free, but it might be easier than rolling your own to do the conversions. 通常不是免费的,但比起自己进行转换要容易得多。

In my research on this question, I came across http://www.zamzar.com/ and https://cloudconvert.com/ . 在对此问题的研究中,我遇到了http://www.zamzar.com/https://cloudconvert.com/ I haven't used either, so I can't report on the quality of the conversion. 我也没有使用过,因此无法报告转换质量。

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

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