PHP Imagick - PSD的“内存分配失败”

时间:2014-03-14 03:55:12

标签: php imagemagick imagick

除非我使用PSD,否则一切都很完美,然后我收到此错误:

Fatal error: Uncaught exception 'ImagickException' with message 'Memory allocation failed `/tmp/phptfUzsA' @ psd.c/ReadPSDImage/1035' in /var/www/html/lib/ext/s98lib/classes/image.php:74

我当前的内存限制为 128mb 。我试着把它增加到256mb而没有运气。

进一步的研究表明,当文件存在其他问题时,会出现此问题。看看JPG和PNG如何正常工作,PSD似乎特别错误。

这是我正在使用的代码:

/**
 * Resize to PNG
 *
 * @throws InvalidParametersException
 *
 * @param string $image_path
 * @param int $width
 * @param int $height
 * @return string
 */
public static function resize_to_png( $image_path, $width, $height ) {
    if ( !file_exists( $image_path ) )
        throw new InvalidParametersException( 'No file exists to resize' );

    $info = getimagesize( $image_path );

    if ( empty( $info ) )
        throw new InvalidParametersException( 'No image information exists in file' );

    // Get data
    $mime = $info['mime'];

    // What sort of image?
    $type = substr( strrchr( $mime, '/' ), 1 );

    // Start doing stuff
    $image = new Imagick();
    $image->setBackgroundColor(new ImagickPixel('transparent'));
    $image->readimage( $image_path );

    if ( 'psd' == $type )
        $image->setIteratorIndex(0);

    // Create thumbnail
    $image->thumbnailImage( $width, $height, false, true );

    $tmp_file = tempnam( sys_get_temp_dir(), 'gcitem_' );

    $image->writeImage( 'png:' . $tmp_file );

    return $tmp_file;
}

我该如何解决这个问题?

0 个答案:

没有答案