douzhuo2722 2015-04-22 13:40
浏览 56
已采纳

Imagemagick - PHP批量转换缩略图没有服务器压力?

I've been using WPDM to create a file system for a client of mine. Unfortunately WPDM doesn't have any preview functionality for filetypes like psd, ai, pdf, etc so I've been trying to create this functionality myself by using ImageMagick.

I'm probably approaching this the wrong way, as I have a major issue with using the code. It only creates the first thumbnail (or two, depending on the psds file size) before it crashes with an Internal Server Error and even one thumbnail makes the site incredibly slow.

// Function to create preview images for all files in WPDM package
function create_previews() {
    $file_list = uploaded_files_path();
    $preview_image_path_list=array();

    foreach ($file_list as $file) {
        $imagick = new Imagick();
        $img = wp_get_image_editor($file);
        $imagick->readImage($file);
        $filenamelres = $img->generate_filename('thumb', ABSPATH.'wp-content/uploads/thumbs/', 'png' );

        if ( ! is_wp_error( $imagick ) ) {
            if ( ! file_exists($filenamelres) ) {
                $imagick->setIteratorIndex(0);
                $imagick->thumbnailImage(200, 0);
                $imagick->writeImage($filenamelres);
                $preview_image_path_list[] = $filenamelres;
            } else {}
        } else {
            $preview_image_path_list[] = $filenamelres;
        }

        $imagick->destroy(); 
    }

    return $preview_image_path_list;
}

Is there a different route to take here. Could I do the same without stressing the server as much as I'm doing?

  • 写回答

1条回答 默认 最新

  • doujiao9574 2015-04-23 12:59
    关注

    As per my comment: on a system of any non-trivial size, thumbnails should be generated in the background, not within the web server process. Switch to cron or a job queue, and use a placeholder thumbnail in the meantime.

    It's worth finding out what triggered the "Internal Server Error" though - you might need to turn up your error logging to ensure it is recorded.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用