Does anyone know why setting ImagickPixel with a cmyk value doesn't work? It always creates a white pixel object. The documentation alleges that new ImagickPixel("cmyk(int,int,int,int)") should work. I can convert the image to RGB, apply the ImagickPixel with rgb or hex and then convert it back to cmyk, which kind of works except that the entire image looks brighter afterwords.

    Well, it appears from the post here that I have to first convert the image to RGB, write the text using an RGB value, and then convert the image back to CMYK. This kind of works except that the image still appears brighter after I convert it back to CMYK. Maybe I'm not understanding how to use color profiles correctly:

    $image = new Imagick("path/to/image/image.jpg");
    
    $icc_rgb = file_get_contents("path/to/profile/AdobeRGB1998.icc");
    $image->profileImage('icc', $icc_rgb);
    
    $image->setImageColorSpace(Imagick::COLORSPACE_RGB);
    
    /* Font properties */
    $draw = new ImagickDraw();
    $draw->setFont("Arial.ttf");
    $draw->setFontSize( 16 );
    $pixel = new ImagickPixel("#E55499");
    $draw->setFillColor($pixel);
    
    
    /* Create text */
    $image->annotateImage($draw, 24, 26, 0, 'some text');
    
    $image->profileImage('icc', null);
    $icc_cmyk = file_get_contents("path/to/profile/USWebCoatedSWOP.icc");
    $image->profileImage('icc', $icc_cmyk);
    
    $image->setImageColorSpace(Imagick::COLORSPACE_CMYK);
    
    $image->setImageFormat('jpeg');
    $image->writeImage('images/test.jpg');

      I don't know why the conversion makes the image too bright, but could it be that you get a white pixel colour due to assigning out of bounds values for the CMYK colours. They aren't in the range 0-255, but rather 0-100.

      Also, according to photoshop, values of 0 correspond to a completely white colour, while values of 100 correspond to a completely black colour. However, if you are restricted to what they refer to as web colours photoshop will not let you set all 100s. In that case CMYK(75, 68, 67, 90) is completely black.

      Does it help at all? I won't have access to Imagick until tomorrow to see if I can figure out what goes wrong.

        Thanks, johanafm. I think I figured it out. I was adding a CMYK profile to an image that already had one so that was screwing with the color. My new problem is annotating text onto an image in a set width textbox so that the text wraps to the next line if need be. Do you have any suggestions for that?

          As far as I know, you have to deal with that yourself.

          array Imagick::queryFontMetrics(ImagickDraw $draw, string $text); will give you some information about the text. It contains 'textWidth' among other things. foreach over the array to see what it contains.

          Then the simples way is probably something along the lines of

          $im = new Imagick();
          $draw = new ImagickDraw();		// also obviously have to set font and font size
          $text = "some very long text";
          $words = explode(' ', $text);
          $maxWidth = 100;
          
          $line = '';
          $lines = array();
          for ($i  = 0; $i < $words; ++$i) {
          	$prop = $im->queryFontMetrics($draw, $line . $words[$i]);
          	if ($prop['textWidth'] <= $maxWidth) {
          		$line .= $words[$i];
          	}
          	else {
          		$lines[] = $line;
          		$line = '';
          	}
          }
          /*	Do note that the above won't deal with splitting words that are too long to
          	fit on one line. */
          
          
          /*	You can use $prop['fontHeight'] (or something like that) and iterate over the
          	$lines array yourself, adjusting the y offset for where to draw each line of
          	text, but Imagick can handle that if you just insert line feeds "\n" */
          $text = implode("\n", $lines);
          

          Also, you might want to have a look at Miko's blog for a decent amount of easy to follow Imagick tutorials.

            Write a Reply...