4

I'm generating a barcode with PEAR::Image_Barcode which uses GD, but I need to write that barcode onto a section of a PDF and PHP/Imagick seems to be the easiest way to do that, is there any way to convert a GD image object into something Imagick can work with?

JKirchartz
  • 17,612
  • 7
  • 60
  • 88
  • is this for a heavy usage and looking to scale? you could try just save the GD image and reload it into Imagick, not the elegant solution I know. – Declan Cook Jun 09 '11 at 19:24
  • This is for 40k+ items, that would destroy the server... – JKirchartz Jun 09 '11 at 19:42
  • Yeah hence my question, all that saving and reloading would not be a good solution. As for moving the GD Image object into Imagick I've never tried it and don't know of a method sorry. – Declan Cook Jun 09 '11 at 19:46
  • I haven't used it, but I note that there is "readImageBlob" with pitiful documentation. I ( http://www.php.net/manual/en/function.imagick-readimageblob.php ) and This ( http://eclecticdjs.com/mike/tutorials/php/imagemagick/examples_07/readimageblob.php ) link shows a possible usage. It reads from a file, but I suppose one might try to capture the output of a GD function such as imagejpeg – horatio Jun 09 '11 at 20:28

2 Answers2

7

here we go:

ob_start();
 Image_Barcode::draw($barcode, 'upca', 'gif');
 $couponBarcode = ob_get_contents();
ob_end_clean();

$second = new Imagick(); 
$second->readImageBlob($couponBarcode)

it's writing the image to an output buffer with GD then reading that variable into an Imagick object

JKirchartz
  • 17,612
  • 7
  • 60
  • 88
2

ImageMagick has internal image creation functions. Using a barcode font you can directly create barcodes in ImageMagick. I guess ImageMagick will be more efficient in building images than PHP. Not sure about this (imageimagick's PDF functions are some times unexpected) but, as an additional bonus you may end up with vector output instead of bitmap, which will take more space and will not scale well.

Cem Kalyoncu
  • 14,120
  • 4
  • 40
  • 62
  • +1 That would be better... but then I'd have to figure out more stuff and I don't have time... next version I'm definitely using this. – JKirchartz Jun 10 '11 at 12:57