【发布时间】:2012-03-08 02:39:31
【问题描述】:
在搜索了 Google 和 SO 之后,我发现了一些使用 ImageMagick 创建 PDF 文档缩略图的代码。
对我来说,麻烦在于将它实施到我的 WordPress 主题中。我认为我陷入了脚本缓存临时文件所需的路径上。
我按照文章中的描述使用它:
<img src="http://localhost/multi/wp-content/themes/WPalchemy-theme/thumbPdf.php?pdf=http://localhost/multi/wp-content/uploads/2012/03/sample.pdf&size=200 />
这一定是正确的(也许......但我认为我使用完整 URL 指向实际文件是正确的),因为当我点击该 URL 时,我被带到一个页面,该页面显示以下错误:
Unable to read the file: tmp/http://localhost/multi/wp-content/uploads/2012/03/sample.pdf.png
现在 tmp 是在 thumbPdf.php 脚本中定义的,但我对它的值应该是什么感到困惑。它是网址还是路径?像 timthumb.php 一样,我可以让它与 thumbPdf.php 脚本相关吗? (我尝试了 ./cache ,这是 timthumb 中的设置 - 并且确定在我的主题根目录中有一个 /cache 文件夹,但无济于事)。另外,我在根目录下放了一个 /tmp 文件夹,但仍然出现同样的错误。
那么我该如何配置 tmp 来完成这项工作?
http://stormwarestudios.com/articles/leverage-php-imagemagick-create-pdf-thumbnails/
function thumbPdf($pdf, $width)
{
try
{
$tmp = 'tmp';
$format = "png";
$source = $pdf.'[0]';
$dest = "$tmp/$pdf.$format";
if (!file_exists($dest))
{
$exec = "convert -scale $width $source $dest";
exec($exec);
}
$im = new Imagick($dest);
header("Content-Type:".$im->getFormat());
echo $im;
}
catch(Exception $e)
{
echo $e->getMessage();
}
}
$file = $_GET['pdf'];
$size = $_GET['size'];
if ($file && $size)
{
thumbPdf($file, $size);
}
我看到了这个答案: How do I convert a PDF document to a preview image in PHP? 下次我也要去试试了
【问题讨论】:
标签: php imagemagick