DWilliams Posted June 22, 2011 Share Posted June 22, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/240060-store-imagemagick-result-in-database/ Share on other sites More sharing options...
Adam Posted June 22, 2011 Share Posted June 22, 2011 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... Quote Link to comment https://forums.phpfreaks.com/topic/240060-store-imagemagick-result-in-database/#findComment-1233219 Share on other sites More sharing options...
DWilliams Posted June 24, 2011 Author Share Posted June 24, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/240060-store-imagemagick-result-in-database/#findComment-1234154 Share on other sites More sharing options...
Adam Posted June 24, 2011 Share Posted June 24, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/240060-store-imagemagick-result-in-database/#findComment-1234276 Share on other sites More sharing options...
Adam Posted June 24, 2011 Share Posted June 24, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/240060-store-imagemagick-result-in-database/#findComment-1234291 Share on other sites More sharing options...
DWilliams Posted June 27, 2011 Author Share Posted June 27, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/240060-store-imagemagick-result-in-database/#findComment-1235561 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.