【问题标题】:Create thumbnail of PDF or PPT file using php使用 php 创建 PDF 或 PPT 文件的缩略图
【发布时间】:2014-03-23 00:28:29
【问题描述】:

我想制作用户上传的 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;
}

但是这需要在服务器上安装 imagic 并且我的主机不允许我,因为我在共享主机上是否有任何其他方法可以做到这一点

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

【问题讨论】:

  • 这里需要在哪里转义单引号?
  • 抱歉标题不正确

标签: php pdf thumbnails powerpoint imagick


【解决方案1】:

还有使用 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 函数,然后根据图像结果调整大小。

【讨论】:

    【解决方案2】:

    我知道我回答这个问题为时已晚,但这可能对其他人有所帮助。

    除了使用imagick或ghost脚本(命令行工具)之外没有其他方法可以完成上述任务,所以最后我找到了一种方法,也可以在另一台专用服务器上启用imagick扩展并在那里生成图像然后下载它们在托管我的网站的服务器上 file_get_content()

    【讨论】:

    • 你能告诉我wordpress用什么来为不同的文件创建缩略图吗?
    【解决方案3】:

    这个问题又迟到了,但您可以考虑使用其中一种在线转换服务。他们有 API,您可以在其中上传 PPTX 并下载每张幻灯片的 png。大部分都不是免费的,但它可能比你自己进行转换更容易。

    在我对这个问题的研究中,我遇到了http://www.zamzar.com/https://cloudconvert.com/。我也没有使用过,所以我无法报告转换的质量。

    【讨论】:

    • 关于此主题的更新...我已经使用 cloudconvert 实现了它,并且效果很好...在请求转换和完成转换之间大约有一两分钟的延迟。我查看了zamzar,但他们在完成选项时没有回调。这意味着您的代码将阻塞等待响应,或者您需要一个轮询循环来检查它是否已完成。实施 cloudconvert 并不难,它需要一些反复试验。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-10-03
    • 2011-10-27
    • 1970-01-01
    • 2018-09-16
    • 2014-12-10
    • 1970-01-01
    • 2018-03-30
    相关资源
    最近更新 更多