Search code examples
phpimagick

PHP imagick, remove profiles and transform color space to RGB


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();
  }
?>

Solution

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