RGB Bilder in CMYK Bilder umrechnen

DiDiJo

Erfahrenes Mitglied
Hi Leute,

ich versuche gerade ein Skript zu schreiben, welches mir ein RGB-Bild in ein CMYK-Bild umrechnet.

Dafür habe ich mir ein Basis-Skript genommen, welches mir ein RGB-Bild in ein SW-Bild umrechnet (****t wunderbar):

PHP:
function makeNewJpegFromFile_sw($src, $target, $quality)
{
    @list($width, $height) = getimagesize($src);
    if ($width != NULL && $width != 0)
    {               
        $sourceImage = @imagecreatefromjpeg($src);
               
        if ($sourceImage == '') return false;
       
        touch($target);
               
        /* GRAYSCALE */
        $img_height = $height;
        $img_width  = $width;
       
        for ($y = 0; $y <$img_height; $y++) {
            for ($x = 0; $x <$img_width; $x++) {
                $rgb = imagecolorat($sourceImage, $x, $y);
                $red   = ($rgb >> 16) & 0xFF;
                $green = ($rgb >> 8)  & 0xFF;
                $blue  = $rgb & 0xFF;
   
                $gray = round(.299*$red + .587*$green + .114*$blue);
               
                // shift gray level to the left
                $grayR = $gray << 16;   // R: red
                $grayG = $gray << 8;    // G: green
                $grayB = $gray;         // B: blue
               
                // OR operation to compute gray value
                $grayColor = $grayR | $grayG | $grayB;
   
                // set the pixel color
                imagesetpixel ($sourceImage, $x, $y, $grayColor);
                imagecolorallocate ($sourceImage, $gray, $gray, $gray);
            }
        }
            /* GRAYSCALE */
       
        $destinationImage = imagecreatetruecolor($width, $height);
        imagecopy($destinationImage, $sourceImage, 0, 0, 0, 0, $img_width, $img_height);
        imagejpeg($destinationImage, $target, $quality);
           
        imagedestroy($sourceImage);
        imagedestroy($destinationImage);
       
        return true;
    }
    return false;
}

das habe ich nun versucht umzuschreiben:
PHP:
function makeNewJpegFromFile_cmyk($src, $target, $quality)
{
    @list($width, $height) = getimagesize($src);
    if ($width != NULL && $width != 0)
    {               
        $sourceImage = @imagecreatefromjpeg($src);
               
        if ($sourceImage == '') return false;
       
        touch($target);
               
        /* GRAYSCALE */
        $img_height = $height;
        $img_width  = $width;
       
        for ($y = 0; $y <$img_height; $y++) {
            for ($x = 0; $x <$img_width; $x++) {
                $rgb = imagecolorat($sourceImage, $x, $y);
                $cmyk = RGB_to_CMYK($rgb);
		//$cmykColor = $cmyk[0] | $cmyk[1] | $cmyk[2] | $cmyk[3];
   
                // set the pixel color
                //imagesetpixel ($sourceImage, $x, $y, $cmykColor);
                //imagecolorallocate ($sourceImage, $gray, $gray, $gray);
            }
        }
       
        $destinationImage = imagecreatetruecolor($width, $height);
        imagecopy($destinationImage, $sourceImage, 0, 0, 0, 0, $img_width, $img_height);
        imagejpeg($destinationImage, $target, $quality);
           
        imagedestroy($sourceImage);
        imagedestroy($destinationImage);
       
        return true;
    }
    return false;
}

function RGB_to_CMYK($rgb)
{
    $cyan = 1 - ($rgb[0] / 255);
    $magenta = 1 - ($rgb[1] / 255);
    $yellow = 1 - ($rgb[2] / 255);

    $min = min($cyan, $magenta, $yellow);
    
    if ($min == 1)
        return array(0,0,0,1);

    $K = $min;
    $black = 1 - $K;

    return array
    (
        ($cyan - $K) / $black,
        ($magenta- $K) / $black,
        ($yellow - $K) / $black,
        $K
    );
}

Ab dem 'imagesetpixel ' komme ich nicht weiter, weil ich hier eigentlich nur ein Array mit 3 Werten (rgb) übergeben kann. Ich bräuchte aber 4. Weiß jemand wie ich nun mein Bild fertig erstellen kann ?

mfg DiDi
 
Zuletzt bearbeitet:
ja das ist die gleiche funktion die ich oben auch nutze ... aber ich habe keine ahung was ich mit diesem cmyk Array anfangen soll.

Diese 'imagesetpixel ' und die 'imagecolorallocate' erwarten ja normalerweise 3 Werte (r g und b). Aber bei nem CMYK Bild habe ich ja 4 werte und das wirft mich ein wenig aus meinem konzept.

mfg DiDi
 
ich habs nun mit der Imagick PHP Extension gelöst bekommen:
PHP:
function convertCMYK($filename,$target) {
	$image = new Imagick($filename);
	$image->setImageColorspace(Imagick::COLORSPACE_CMYK);
	$image->writeImage($target); // this will replace the old file!
}
 
Zurück