简体   繁体   English

使用ImageMagick有效地从PDF创建图像

[英]Using ImageMagick to create an image from a PDF…efficiently

I'm using ImageMagick to create a tiny JPG thumbnail image of an already-uploaded PDF. 我正在使用ImageMagick创建一个已经上传的PDF的小JPG缩略图。 The code works fine. 该代码工作正常。 It's a WordPress widget, though this isn't necessarily WordPress specific. 这是一个WordPress小部件,尽管不一定是WordPress特定的。

I'm unfamiliar with ImageMagick, so I was hoping somebody could tell me if this looks terrible or isn't following some best practices of some sort, or if I'm risking crashing the server. 我不熟悉ImageMagick,所以我希望有人可以告诉我这是否看起来很糟糕,或者没有遵循某些最佳实践,或者是否有使服务器崩溃的风险。

My questions, specifically, are: 我的问题具体是:

  • Is that image cached, or does the server have to re-generate the image every time somebody views the page? 该图像是否已缓存,还是服务器每次有人查看该页面时都必须重新生成该图像? If it isn't cached, what's the best way to make sure the server doesn't have to regenerate the thumbnail? 如果缓存,确保服务器不必重新生成缩略图的最佳方法什么?
  • I tried to create a separate folder (/thumbs) for ImageMagick to put all the images in, instead of cluttering up the WP upload folders with images of PDFs. 我试图为ImageMagick创建一个单独的文件夹(/ thumbs)以放入所有图像,而不是用PDF的图像弄乱WP上传文件夹。 It kept throwing a permission error, despite 777 permissions on the folder in my testing environment. 尽管在我的测试环境中对该文件夹有777个权限,但它始终引发权限错误。 Why? 为什么? Do the source/destination directories have to be the same? 源/目标目录必须相同吗?
  • Am I doing anything incorrectly/inefficiently here that needs to be improved? 我在这里做的任何错误/无效的事情都需要改进吗?

The whole widget is on Pastebin: http://pastebin.com/WnSTEDm7 整个小部件位于Pastebin上: http : //pastebin.com/WnSTEDm7

Relevant code: 相关代码:

<?php

if ( $url ) {       
    $pdf = $url;
    $info = pathinfo($pdf);
    $filename =  basename($pdf,'.'.$info['extension']);

    $uploads = wp_upload_dir();
    $file_path = str_replace( $uploads['baseurl'], $uploads['basedir'], $url );
    $dest_path = str_replace( '.pdf', '.jpg', $file_path );
    $dest_url = str_replace( '.pdf', '.jpg', $pdf );

    exec("convert \"{$file_path}[0]\" -colorspace RGB -geometry 60 $dest_path"); ?>
    <div class="entry">
        <div class="widgetImg">
            <p><a href="<?php echo $url; ?>" title="<?php echo $filename; ?>"><?php echo "<img src='".$dest_url."' alt='".$filename."' class='blueBorder' />"; ?></a></p>
        </div>

        <div class="widgetText">
            <?php echo wpautop( $desc ); ?>

            <p><a class="downloadLink" href="<?php echo $url; ?>" title="<?php echo $filename; ?>">Download</a></p>
        </div>
    </div>
    <?php }
?>

As you can see, the widget grabs whatever PDF is attached to the current page being viewed, creates an image of the first page of the PDF, stores it, then links to it in HTML. 如您所见,该小部件将捕获附加到当前正在查看的当前页面的所有PDF,创建该PDF第一页的图像,进行存储,然后以HTML链接到该页面。

Thanks for any and all help! 感谢您提供的所有帮助!

As you are saving as a jpg try adding -define to your code: 当您另存为jpg时,请尝试在代码中添加-define:

exec("convert -define jpeg:size=60x60 \"{$file_path}[0]\" -colorspace RGB -geometry 60 $dest_path"); ?> 

60x60 is the finished size of your image - all it does is read in enough data to create the image so speeding up the read process. 60x60是图像的最终尺寸-它所做的全部都是读取足够的数据以创建图像,从而加快了读取过程。

Resize keeping aspect then crop to 60x60 调整保持宽高比的大小,然后裁剪为60x60

exec("convert -define jpeg:size=60x60 \"{$file_path}[0]\" -colorspace RGB -thumbnail 60x60 -gravity center -crop 60x60+0+0 +repage $dest_path"); ?> 

So I think ImageMagick was re-generating the thumbnail on every page view. 因此,我认为ImageMagick 正在重新生成每个页面视图上的缩略图。 Pages with this widget would take an additional couple of seconds to load. 带有此小部件的页面将花费额外的几秒钟时间。

So, it now does a simple check to see if the thumbnail is already there: 因此,它现在进行简单检查以查看缩略图是否已经存在:

if ( !file_exists( $dest_path ) ) {
    exec("convert \"{$file_path}[0]\" -colorspace RGB -geometry 60 $dest_path");
};

Pages that took ~5 seconds to load now take 2-3. 现在,需要大约5秒钟才能加载的页面需要2-3。

Regardless, I'm still interested to know if any PHP people think this could be done better. 无论如何,我仍然想知道是否有任何PHP人员认为可以做得更好。

Hope this code helps somebody out. 希望这段代码可以帮助别人。

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

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