douyi2798 2012-06-11 10:16
浏览 32
已采纳

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

I'm using ImageMagick to create a tiny JPG thumbnail image of an already-uploaded PDF. The code works fine. It's a WordPress widget, though this isn't necessarily WordPress specific.

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.

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. It kept throwing a permission error, despite 777 permissions on the folder in my testing environment. 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

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.

Thanks for any and all help!

  • 写回答

2条回答 默认 最新

  • dtz46697 2012-06-11 14:39
    关注

    As you are saving as a jpg try adding -define to your code:

    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.

    Resize keeping aspect then crop to 60x60

    exec("convert -define jpeg:size=60x60 \"{$file_path}[0]\" -colorspace RGB -thumbnail 60x60 -gravity center -crop 60x60+0+0 +repage $dest_path"); ?> 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择