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 Android STD快速启动
  • ¥15 如何使用simulink建立一个永磁同步直线电机模型?
  • ¥30 天体光谱图的的绘制并得到星表
  • ¥15 PointNet++的onnx模型只能使用一次
  • ¥20 西南科技大学数字信号处理
  • ¥15 有两个非常“自以为是”烦人的问题急期待大家解决!
  • ¥30 STM32 INMP441无法读取数据
  • ¥15 R语言绘制密度图,一个密度曲线内fill不同颜色如何实现
  • ¥100 求汇川机器人IRCB300控制器和示教器同版本升级固件文件升级包
  • ¥15 用visualstudio2022创建vue项目后无法启动