Jonathan Basile
Jonathan Basile

Reputation: 679

Magick++ to display an image with CGI

I recently began using Magick++ (C++ API for ImageMagick) with the goal of creating a website that could display randomly generated images. I am trying to write a CGI script which would create a JPEG Image, set the color of its pixels, and then return the image information as Content-type: image/jpg.

Reading through the documentation, I only find functions for writing image files to disk. I do not see one which would do what I am hoping to do, along the lines of std::cout << Image or std::cout << Blob

My goal is to be able to display the image generated by the script in a webpage, without needing to write the image to disk.

I know that PerlMagick has a display function which does what I am trying to do - I am wondering if I can do the same with Magick++.

Upvotes: 1

Views: 1004

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207345

I think you are looking for blobs - Binary Large Objects. Basically, you create an image of type Image and a blob of type Blob. You populate your image either by reading from a file or generating your random data, then you write your image to the blob (to encode it) and you can then write that to the user's browser.

Please excuse my useless C/C++ skills...

Image  image;
Blob   blob;

// set type to JPEG
image.magick("JPEG");   

// generate/read/fill image here
image.read("image.jpg");

// encode image as JPEG
image.write(&blob);   

// now send MIME type to browser
std::cout << "Content-type: image/jpeg" << CRLF;

// ... followed by blob
write(1,(char*)blob.data(),blob.length());

Upvotes: 1

Related Questions