How do I store ImageMagick output in an array / sql-db?

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Michael_M

How do I store ImageMagick output in an array / sql-db?

Post by Michael_M »

Hi all,

I'm new here and very much interested in ImageMagick. I've been working for the last few days on converting a pdf file to jpg's. I've come a long way, however I can't seem to control the output of imagemagick.

This is the php-script I use to convert the files:

exec("convert -density 200x200 -quality 100 -resize 800x1024 $filepath $convertpath_large");

The files are stored in the right directory. The file name are stores in a mysql-db, however I can't seem to count the number of output-files. Any help would be very welcome.

Before posting, I took my time on this forum and reading myself up on the subject with the help of books, but I feel like everyone already knows how to manage the output, except for me. (I can't find any topic about handling the output). As you can see, I'm kinda stuck, I hope to find some help here or tips in the right direction.

Thanks for reading :D
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How do I store ImageMagick output in an array / sql-db?

Post by fmw42 »

what is $filepath? Is that one file or a direcotry? what is $convertpath_large? If they are just a directories, you have the wrong command. you need to use mogrify. If they are the actual filenames with suffix, then it should work.

http://www.imagemagick.org/Usage/basics/#mogrify


also your command does not have the path to convert. usually it is /usr/local/bin/convert

Can you have PHP confirm your version of IM?

try one of these:

<?php
system("/usr/local/bin/convert -version");
?>


<?php
$IM_version=shell_exec("/usr/local/bin/convert -version");
echo $IM_version
?>

<?php
exec("/usr/local/bin/convert -version",$out,$returnval);
print_r($out[0]);
?>
Michael_M

Re: How do I store ImageMagick output in an array / sql-db?

Post by Michael_M »

Hi fmw42,

Thanks for your reply! I think I'm using the right filename and suffix. I feel I'm stuck when it comes to returning the created filenames to php. Getting a number of results would suffice as well.

When it's just one file being converted, I have no trouble getting the filename, since I can set a variable in PHP. When it comes down to creating multiple filenames, I have no control anymore. What should I do to make it work?

Sorry for leaving some info out. This is the code defining the variables:

$file = $fileName;
$expDot = explode(".",$file);
$filepath = $newDir . $file;
$filepath_large = $filepath . "/large";
$newName = time() . ".jpg";
$convertpath = $newDir . $newName;
$convertpath_large = $convertpath . "/large";

I checked the current version:

* Version: ImageMagick 6.2.4 02/10/07 Q16 http://www.imagemagick.org
* Copyright: Copyright (C) 1999-2005 ImageMagick Studio LLC

I used this code to check the version:

<html> <head> <title>Test for ImageMagick</title> </head>
<body> <?
function alist ($array) { //This function prints a text array as an html list.
$alist = "<ul>";
for ($i = 0; $i < sizeof($array); $i++) {
$alist .= "<li>$array[$i]";
}
$alist .= "</ul>";
return $alist;
}
exec("convert -version", $out, $rcode); //Try to get ImageMagick "convert" program version number.
echo "Version return code is $rcode <br>"; //Print the return code: 0 if OK, nonzero if error.
echo alist($out); //Print the output of "convert -version"
//Additional code discussed below goes here.
?> </body> </html>
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How do I store ImageMagick output in an array / sql-db?

Post by fmw42 »

You cannot use convert to resize more than one image at a time (without using -write and parenthesis processing). If more than one input image in the command line, then you really need to use mogrify. Some commands will let you convert one image into multiple channels, but you cannot process multiple inputs to an equal number of outputs as you are trying to do. You have to issue one convert per input/output pair.

see

http://www.imagemagick.org/Usage/basics/#mogrify
http://www.imagemagick.org/Usage/basics/#image_seq

With regard to IM using PHP, I have no real experience. Perhaps one of the PHP experts can help you.
Michael_M

Re: How do I store ImageMagick output in an array / sql-db?

Post by Michael_M »

Hi fwm42,

Thanks again for your reply! This is forum will save me :)

There's one pdf coming into IM. That pdf is converted into multiple jpg's. (I'm not trying to put more files in at once.)

I've tried again yesterday, and I'll try again today, but I can't seem to count the number of jpg's created by IM. Because of that I can not control the output of IM and tell my database how many pages there are. :(

I'll try again to fix it today. I really hope someone can help me out.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How do I store ImageMagick output in an array / sql-db?

Post by fmw42 »

If you have one file with many pages/frames, then you can create multiple output as follows, either:

convert image.pdf image.jpg

this will add -0, -1, -2 etc before the .jpg
or

convert image.pdf image_%d.jpg

this will add _0, _1, _2 etc before the .jpg


You can also add leading zeros.

To find out how many images you are going to create, you can use identify and count the lines. For example

create a multipage pdf for testing:
convert rose: rose: rose: rose3.pdf

identify rose3.pdf
rose3.pdf[0] PDF 70x46 70x46+0+0 16-bit Bilevel DirectClass 1.39kb
rose3.pdf[1] PDF 70x46 70x46+0+0 16-bit Bilevel DirectClass 1.39kb
rose3.pdf[2] PDF 70x46 70x46+0+0 16-bit Bilevel DirectClass 1.39kb

Thus looking at the last line, you find [2] as the last page, so you have 2+1 pages.

One of the PHP experts can tell you how to extract that number from a similar PHP command
Last edited by fmw42 on 2009-05-15T09:01:43-07:00, edited 1 time in total.
Michael_M

Re: How do I store ImageMagick output in an array / sql-db?

Post by Michael_M »

Hi fmw42,

Thanks a lot! :D So that's how it can be done. I haven't seen that option yet in IM. I'll google around a bit for the identify option and see how to extract the number.

If a PHP-guy knows how to do it. Your help is very welcome!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How do I store ImageMagick output in an array / sql-db?

Post by fmw42 »

There is an easier way that I had forgotten about using string formats.

convert rose: rose: rose: rose.pdf
freds-mac-mini:~ fred$ identify -format "%n" rose.pdf
3


see http://www.imagemagick.org/script/escape.php

also

num=`identify -format "%n" rose.pdf`
freds-mac-mini:~ fred$ echo $num
3
Michael_M

Re: How do I store ImageMagick output in an array / sql-db?

Post by Michael_M »

I tried it with this line of code:

exec("identify -format "%n" $filepath", $pages);

and it's working. You made my day a brighter day. Thanks a lot!
Post Reply