简体   繁体   中英

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++.

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());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM