Page 1 of 1

Sharing between IMagick & GD without writing to disk?

Posted: 2010-10-22T11:03:20-07:00
by pwnedd
Hi all,

Background
I have an interesting problem where I would like to be able to work with an image in Imagick, do some processing in GD, and then pass the image back to IMagick before finally writing it to disk. I know this sounds a bit indirect, but there is a reason: GD supports direct palette manipulation, making it possible to very quickly apply a color table to an 8-bit grayscale image. ImageMagick does support applying a color table to an image (, but instead of applying the color table directly to the image's palette, the image is converting into a 24-bit RGB image first, which is slow (viewtopic.php?f=1&t=13327).

To complicate things further, GD cannot read any of the formats that the images can be made to being with (RAW, TIFF, BMP and PGM), so I first have to read the image into IMagick and convert it to a PNG image, which GD can read.

In a nut-shell the process looks something like :

Code: Select all

    PGM -> IMagick -> GD -> IMagick -> JPG
Goal

So what I would like to be able to do is to read the file in once and then just pass around something like a base-64 encoding string between GD and FFmpeg. So far though I haven't been able to find a way to do that. IMagick supports reading a binary string in (http://www.php.net/manual/en/function.i ... geblob.php), but how can I write one out? Is is possible use PHP output buffering to capture the image as a binary string?

Does anyone know if this is possible, or have any suggestions?

Any help would be appreciated :)

Thanks,
Keith

Re: Sharing between IMagick & GD without writing to disk?

Posted: 2010-10-24T02:45:05-07:00
by pwnedd
Okay, I found a way. In case anyone else is interested:

Code: Select all

// Read image into IM
$image = new IMagick($input);
$image->setImageFormat('PNG');

// Export as a binary string
$intermediateStr = $image->getimageblob();

// Import image string into GD
$gd = imagecreatefromstring($intermediateStr);

// do some processing...

// Then capture GD output as a string using PHP output buffering
ob_start();
imagepng($gd, NULL);
$gdImageString = ob_get_contents();
ob_end_clean();

// Read back in to IM
$image = new IMagick();
$image->readimageblob($gdImageString);
$image->writeImage("final.png");
I also tried base64 encoding the strings to see if that would speed things up, but that seemed to be just a little bit slower.