Is there a simple way to convert this Indexed Color file to an RGB Color mode using Imagick in PHP?
I'm the process of converting an image from an RGB PDF, to an RGB PNG. In the majority of cases when I open the file the image mode is RGB color, but in a couple of instances the image mode is "Indexed Color".
Does anyone know the difinitive approach to convert Indexed Color to RGB? I assume this is controlled by the Imagick::IMGTYPE rather than Imagick::COLORSPACE?
I've included these 2 examples of a file which is RGB and one which is Indexed. Interestingly when you run the following functions:
function getTitleImageType ($imagetype) {
$imagetype_title = "";
switch($imagetype) {
case \Imagick::IMGTYPE_UNDEFINED: $imagetype_title = "Undefined"; break;
case \Imagick::IMGTYPE_BILEVEL: $imagetype_title = "Bilevel"; break;
case \Imagick::IMGTYPE_GRAYSCALE: $imagetype_title = "Grayscale";break;
case \Imagick::IMGTYPE_GRAYSCALEMATTE: $imagetype_title = "Grayscale Matte"; break;
case \Imagick::IMGTYPE_PALETTE: $imagetype_title = "Palette"; break;
case \Imagick::IMGTYPE_PALETTEMATTE: $imagetype_title = "Palette Matte"; break;
case \Imagick::IMGTYPE_TRUECOLOR: $imagetype_title = "Truecolor"; break;
case \Imagick::IMGTYPE_TRUECOLORMATTE: $imagetype_title = "Truecolor Matte"; break;
case \Imagick::IMGTYPE_COLORSEPARATION: $imagetype_title = "Color Separation"; break;
case \Imagick::IMGTYPE_COLORSEPARATIONMATTE: $imagetype_title = "Color Separation Matte"; break;
case \Imagick::IMGTYPE_OPTIMIZE: $imagetype_title = "Optimize"; break;
}
return $imagetype_title;
}
This returns 7 (Truecolor Matte) for both files.
function getTitleImageColorspace ($imagecolor) {
$imagecolor_title = "";
switch($imagecolor) {
case \Imagick::COLORSPACE_UNDEFINED: $imagecolor_title = "Undefined"; break;
case \Imagick::COLORSPACE_RGB: $imagecolor_title = "RGB"; break;
case \Imagick::COLORSPACE_GRAY: $imagecolor_title = "GRAY"; break;
case \Imagick::COLORSPACE_TRANSPARENT: $imagecolor_title = "Transparent"; break;
case \Imagick::COLORSPACE_OHTA: $imagecolor_title = "OHTA"; break;
//case \Imagick::COLORSPACE_LAB: $imagecolor_title = "LAB"; break;
case \Imagick::COLORSPACE_XYZ: $imagecolor_title = "XYZ"; break;
case \Imagick::COLORSPACE_YCBCR: $imagecolor_title = "YCBCR"; break;
case \Imagick::COLORSPACE_YCC: $imagecolor_title = "YCC"; break;
case \Imagick::COLORSPACE_YIQ: $imagecolor_title = "YIQ"; break;
case \Imagick::COLORSPACE_YPBPR: $imagecolor_title = "YPBPR"; break;
case \Imagick::COLORSPACE_YUV: $imagecolor_title = "YUV"; break;
case \Imagick::COLORSPACE_CMYK: $imagecolor_title = "CMYK"; break;
case \Imagick::COLORSPACE_SRGB: $imagecolor_title = "SRGB"; break;
case \Imagick::COLORSPACE_HSB: $imagecolor_title = "HSB"; break;
case \Imagick::COLORSPACE_HSL: $imagecolor_title = "HSL"; break;
case \Imagick::COLORSPACE_HWB: $imagecolor_title = "HWB"; break;
case \Imagick::COLORSPACE_REC601LUMA: $imagecolor_title = "REC601LUMA"; break;
case \Imagick::COLORSPACE_REC709LUMA: $imagecolor_title = "REC709LUMA"; break;
case \Imagick::COLORSPACE_LOG: $imagecolor_title = "LOG"; break;
case \Imagick::COLORSPACE_CMY: $imagecolor_title = "CMY"; break;
}
return $imagecolor_title;
}
This returns 13 (SRGB) for both files?
Am I misunderstanding how these elements work?
UPDATE
I've written this function to compare some files. I've created a transparent png (CLEARBG) and a red png (REDBG) and opened them in Imagick, saved them and renamed the file (BACKGROUND/BACKGROUND_RED).
This process alone is changing the file properties as shown below:
REDBG / COLOURSPACE = 13 / IMAGETYPE = 6
BRACKGROUND_RED / COLOURSPACE = 13 / IMAGETYPE = 4
CLEARBG / COLOURSPACE = 13 / IMAGETYPE = 7
BACKGROUND_CLEAR / COLOURSPACE = 2 / IMAGETYPE = 7
The code is below with the files - would anyone be able to give me a sanity check to see if you get the same result?
public function compareFiles($sku){
dump($sku);
$folder = "TEST/";
$base_path = $folder.'CLEARBG.png';
$base_path_red = $folder.'REDBG.png';
// Open the base image - transparent
$imgbase = new \Imagick();
$imgbase->readImage($base_path);
$imgbase->writeImage($folder."BACKGROUND_CLEAR.png");
// Open the base image - red
$imgbase = new \Imagick();
$imgbase->readImage($base_path_red);
$imgbase->writeImage($folder."BACKGROUND_RED.png");
$files = [
"REDBG.png",
"CLEARBG.png",
"BACKGROUND_RED.png",
"BACKGROUND_CLEAR.png",
];
foreach($files AS $file){
$printfile = new \Imagick();
$printfile->setResolution(300, 300);
$printfile->readImage($folder.$file);
$properties = $printfile->getImageProperties();
$colour_space = $printfile->getImageColorspace();
$image_type = $printfile->getImageType();
echo "File: ".$file."\n";
echo "Colour Space: ".$colour_space."\n";
echo "Image Type: ".$image_type."\n";
echo "<pre>".print_r($properties)."</pre>\n";
echo "######################################\n";
}
}


