英文:
PHP imagick, remove profiles and transform color space to RGB
问题
以下是翻译好的部分:
以下脚本上传图像文件,调整大小至定义的最大宽度,压缩并保存缩略图到不同位置。
到目前为止,这个脚本运行得很好,但我遇到了一些问题:
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_SRGB);// 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();
}
?>
英文:
The following script uploads image files, resizes to defined max width, compresses and saves a thumbnail image to a different location.
This works great so far but I'm strugglin with a few issues:
stripImage() should remove the color profile, but when I check the uploaded files, they still include the profile.(?)
transformImageColorspace should transform the colorspace to RGB. But when I upload a grey scale image, it does not transform to RGB.
Any ideas on how to fix these issues?
(PHP Version 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_SRGB);// 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();
}
?>
答案1
得分: 0
直接在读取图像后添加 $img->setType(Imagick::IMGTYPE_TRUECOLOR);
解决了该问题。
英文:
Adding $img->setType(Imagick::IMGTYPE_TRUECOLOR);
directly after reading the image solved the issue.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论