PHP imagick,移除配置文件并将颜色空间转换为RGB。

huangapple go评论79阅读模式
英文:

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)

&lt;?PHP
  $maxWidth = 3000;
  $path = &quot;../files/&quot;;
  if (move_uploaded_file($_FILES[&#39;file&#39;][&#39;tmp_name&#39;], $path)) {
    $img = new imagick();
    $thumbsPath = str_replace(&#39;files&#39;, &#39;thumbs&#39;, $path);
    $img-&gt;readImage($path);
    $img-&gt;transformImageColorspace(Imagick::COLORSPACE_SRGB);// does not work
    if ($img-&gt;getImageWidth() &gt; $maxWidth) $img-&gt;resizeImage($maxWidth, null, Imagick::FILTER_LANCZOS, 0.9);
    if ($img-&gt;getImageFormat() == &#39;jpg&#39; || &#39;jpeg&#39;) {
      $img-&gt;setImageCompression(Imagick::COMPRESSION_JPEG);
      $img-&gt;setImageCompressionQuality(82);
    }
    $img-&gt;stripImage();// does not work
    $img-&gt;writeImage($path);
    $img-&gt;thumbnailImage(300, null);
    $img-&gt;writeImage($thumbsPath);
    $img-&gt;destroy();
  }
?&gt;

答案1

得分: 0

直接在读取图像后添加 $img->setType(Imagick::IMGTYPE_TRUECOLOR); 解决了该问题。

英文:

Adding $img-&gt;setType(Imagick::IMGTYPE_TRUECOLOR); directly after reading the image solved the issue.

huangapple
  • 本文由 发表于 2023年7月31日 18:54:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/76802925.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定