以下脚本上传图像文件,调整大小到定义的最大宽度,压缩缩略图并将其保存到不同位置。 到目前为止效果很好,但我遇到了一些问题:
stripImage() 应该删除颜色配置文件,但是当我检查上传的文件时,它们仍然包含配置文件。(?)
transformImageColorspace 应该将颜色空间转换为 RGB。但是当我上传灰度图像时,它不会转换为 RGB。
关于如何解决这些问题有什么想法吗? (PHP 版本 8.1/Imagick 3.7)
<?PHP
$maxWidth = 3000;
$path = "../files/";
if (move_uploaded_file($_FILES['file']['tmp_name'], $path)) {
$img = new imagick();
$thumbsPath = str_replace('files', 'thumbs', $path);
$img->readImage($path);
$img->transformImageColorspace(Imagick::COLORSPACE_RGB);// does not work
if ($img->getImageWidth() > $maxWidth) $img->resizeImage($maxWidth, null, Imagick::FILTER_LANCZOS, 0.9);
if ($img->getImageFormat() == 'jpg' || 'jpeg') {
$img->setImageCompression(Imagick::COMPRESSION_JPEG);
$img->setImageCompressionQuality(82);
}
$img->stripImage();// does not work
$img->writeImage($path);
$img->thumbnailImage(300, null);
$img->writeImage($thumbsPath);
$img->destroy();
}
?>