I've been trying to figure out why my calls to image_rotate() have been failing with the following message:
ImageMagick reported an error: convert: unrecognized color `000000' @ warning/color.c/GetColorCompliance/948. .
I started inserting drupal_set_messages() here and there to test things out and I eventually found that _image_imagemagick_convert() is stripping the hash off the color codes:
function _image_imagemagick_convert($source, $dest, $args) {
$command = implode(' ', array(
preg_replace("/[^A-Za-z0-9\!\.\-\+\_\/\040]/", '', implode(' ', $args)),
escapeshellarg($source),
escapeshellarg($dest),
));
drupal_set_message("MHEINZ: " . print_r($command,1));
if (0 != _image_imagemagick_convert_exec($command, $output, $errors)) {
return FALSE;
}
return file_exists($dest);
}
Generates
MHEINZ: -rotate 270 -background 000000 -quality 75 -density 72 -units PixelsPerInch 'sites/mwheinz.no-ip.info/files/images/DreamNeedles.JPG' '/private/tmp/acidfreeqP3ngB.jpg'
This problem only affects rotate because it appears to be the only function that passes a color to ImageMagick.
Unfortunately, simply adding '#' to the preg_replace() also causes problems - because it gets interpreted as the beginning of a comment when the command is executed, so the complete patch has to be slightly more complex. The final patch looks like this:
--- image.imagemagick.orig 2011-02-23 12:27:06.000000000 -0500
+++ image.imagemagick.inc 2011-02-23 12:30:37.000000000 -0500
@@ -105,7 +105,7 @@
function image_imagemagick_rotate($source, $dest, $degrees, $bg_color = 0x000000) {
$args = array(
'rotate' => '-rotate ' . (float) $degrees,
- 'background' => '-background #' . str_pad(dechex($bg_color), 6, 0),
+ 'background' => '-background "#' . str_pad(dechex($bg_color), 6, 0) . '"',
);
$args = _image_imagemagick_alter_invoke('rotate', $source, $args);
return _image_imagemagick_convert($source, $dest, $args);
@@ -125,7 +125,7 @@
*/
function _image_imagemagick_convert($source, $dest, $args) {
$command = implode(' ', array(
- preg_replace("/[^A-Za-z0-9\!\.\-\+\_\/\040]/", '', implode(' ', $args)),
+ preg_replace('/[^#"A-Za-z0-9\!\.\-\+\_\/\040]/', '', implode(' ', $args)),
escapeshellarg($source),
escapeshellarg($dest),
));
Comment | File | Size | Author |
---|---|---|---|
#2 | imagemagick.patch | 934 bytes | mwheinz |
Comments
Comment #1
joachim CreditAttribution: joachim commentedThe imagemagic component is unsupported, but if you upload this as a patch file and get a few people to test it and report back, I'll commit it.
Comment #2
mwheinz CreditAttribution: mwheinz commentedHow's this?
Comment #3
joachim CreditAttribution: joachim commentedSetting to 'needs review' so other users will test the patch :)
Comment #5
joachim CreditAttribution: joachim commentedPatch is incorrectly formed -- roll it from the base package folder.