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 UE5#if WITH_EDITOR导致打包的功能不可用
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真
  • ¥15 mars2d在vue3中的引入问题
  • ¥50 h5唤醒支付宝并跳转至向小荷包转账界面
  • ¥15 算法题:数的划分,用记忆化DFS做WA求调
  • ¥15 chatglm-6b应用到django项目中,模型加载失败
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket