Jump to content

Recommended Posts

Basically, I want to have my script call Imagemagick montage to combine multiple images and store the result image as a blob in the database. Imagemagick, as far as I can see, just wants input images and an output image. Is there any way I can somehow manipulate my exec (or similar) function to store the result in a variable instead of somewhere on the filesystem?

Link to comment
https://forums.phpfreaks.com/topic/240060-store-imagemagick-result-in-database/
Share on other sites

exec allows you to supply an "output" array as the second argument. You also need to have imagemagick write to stdout (standard output) in order to actually return the data in the first place. We'll need to see the command you're using to help with that though...

exec allows you to supply an "output" array as the second argument. You also need to have imagemagick write to stdout (standard output) in order to actually return the data in the first place. We'll need to see the command you're using to help with that though...

 

Hmm I've been playing around with this and I think I'm almost to a solution. I figured out how to make imagemagick output to stdout, and I found the passthru() function which is designed for binary data like the images I'm dealing with. If I do something like this (using CodeIgniter), I get my image rendered correctly:

 

$this->output->set_content_type('image/png');
passthru($imagemagick_command);

 

passthru() doesn't have an option to return output, so that's not going to work in my case. Going back to exec, it can return output but it automatically separates lines and returns each line as an element in an array.

 

I figure I can just implode the array to get it back as one contiguous file and store it in the database, but I cannot seem to display it again.

 

My exec/recombine line looks like this:

exec($magick_command, $output);
$output = implode("\n", $output);

 

The binary data is being stored in the database, but when I pull it out of the DB and try to send it to the browser (after setting my MIME type in the header, of course), it just gets output as a huge string of random characters (binary data being interpreted as text).

 

What am I doing wrong?

What does a var_dump of $output return? Being binary, you won't be able to just have line-feeds in the middle.. although I'm not sure why it would be split in the first place. Perhaps there's other stuff being returned with the binary..? Just a guess though. I'll try to recreate the issue here and see what I can find.

Okay had a play around and got a similar case working using an output buffer:

 

header('Content-Type: image/gif');

ob_start();

passthru('convert test.jpg gif:-');

$output = ob_get_contents();

ob_end_clean();

echo $output;

 

I'm not convinced it's the best way to do it, and someone may have a better suggestion, but it's all I could get working at the moment.

Okay had a play around and got a similar case working using an output buffer:

 

header('Content-Type: image/gif');

ob_start();

passthru('convert test.jpg gif:-');

$output = ob_get_contents();

ob_end_clean();

echo $output;

 

I'm not convinced it's the best way to do it, and someone may have a better suggestion, but it's all I could get working at the moment.

 

That seems like a hack-ish way of doing it but I can't find any other way to do it so it looks like that's what I'm doing to do, thanks  :D

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.