在 PHP 中使用 Imagick 从 .tif 文件创建图像

Creating images from .tif file using Imagick in PHP

我无法在 PHP 中使用 Imagick 创建图像。我已经创建了一个日志文件来调试正在使用的函数,但我没有从中得到太多。 我检查了 $sourcePath、$destPath、$fileExtension,一切正常。

转换版本:

Version: ImageMagick 6.9.10-23 Q16 x86_64 20190101 https://imagemagick.org
Copyright: © 1999-2019 ImageMagick Studio LLC
License: https://imagemagick.org/script/license.php
Features: Cipher DPC Modules OpenMP 
Delegates (built-in): bzlib djvu fftw fontconfig freetype jbig jng jpeg lcms lqr ltdl lzma openexr pangocairo png tiff webp wmf x xml zlib

// 用于创建缩略图的函数

    function CreateThumnail($sourcePath, $destPath, $fileExtension)
    {
        $maxCoreNum = 4; // set maximum number of cores
        $tempPath = ""; 
        
        if (strtoupper($fileExtension) == "TIF" || strtoupper($fileExtension) == "TIFF") 
        {
            $tempPath = str_replace("thumb.jpg","temp.tif",$destPath);

            //flatten image
            $command = 'gdal_translate -co "TILED=YES" -co "COMPRESS=LZW" -co "BIGTIFF=YES" -ot Byte -scale "'.$sourcePath.'" "'.$tempPath.'"  2>&1';
            exec($command, $output);
            
            $image = new Imagick($tempPath);
            
            $image->setImageColorspace(255); 
            $image->setCompression(Imagick::COMPRESSION_JPEG); 
            $image->setCompressionQuality(60); 
            $image->setResourceLimit (6, $maxCoreNum); //Set maximum number of cores to use with ImageMagick
            $image->setImageBackgroundColor('white');
            $image = $image->flattenImages(); // Use this instead.
            
            $image->setImageFormat('jpeg'); 

            $image->resizeImage(300, 0, imagick::FILTER_UNDEFINED, 1);  
            $image->writeImage($destPath);
    
            $command = 'rm "'.$tempPath.'"';
            exec($command);     
        }
    }

Seven years ago, I pointed out a bug to the PHP.net team,显示writeImage()坏了,其实需要用andwriteImageFile()。我怀疑 readImage()readImageFile().

也一定存在这个问题

交换您的代码,$image->writeImage($destPath);,然后尝试使用此代码...

$image = new Imagick();     
$image_filehandle = fopen($sourcePath, 'a+');
$image->readImageFile($image_filehandle);

$image_filehandle = fopen($destPath, 'w+');
$image->writeImageFile($image_filehandle);
fclose($image_filehandle);

这应该会告诉您 fopen() 是否失败,以防 Imagick 根本不是问题,并且具有权限和 user/file 权利。

我有一个使用 imagick 函数的工作项目,看看 icon-resizing 的源代码,你会发现我几乎遵循了上面描述的过程:https://github.com/HoldOffHunger/GreenGluonCMS/blob/master/traits/scripts/SimpleImages.php

我一直是 Imagick 的粉丝,一直觉得它未被充分认识和使用。让我知道这个答案是否有帮助!