Using width and height from info:

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
phpknight

Using width and height from info:

Post by phpknight »

I have learned that I can get the width and height of the image back using info: at the end of a command. How do I extract the width and height from that info: part to use it for the next image manipulation?

Also, can this be done when using a PHP exec call as well? If so, is it different?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using width and height from info:

Post by fmw42 »

In command line, you can do:

width=`identify -ping '%w' imagefile`
height=identify -ping '%n' imagefile`
echo $width
echo $height

or
width=`convert imagefile -ping '%w' info:`
height=convert imagefile -ping '%n' info:`
echo $width
echo $height

or if you want them both together as a size

size=`convert imagefile -ping -format '%[fx:w]x%[fx:h]' info:`
echo $size

I do not know PHP well enough to advise. But see http://www.rubblewebs.co.uk/imagemagick/
He has a lot of good information on using ImageMagick with PHP using the exec command.
phpknight

Re: Using width and height from info:

Post by phpknight »

Okay, I am still not getting them to work. So, I am trying to write one long command that does the following:

Makes a large 1000x1000 canvas. Writes something on it. Trims it (to center it). Then, I am putting a wave through it that I want to be an arc. Later, I'll try using distort Arc, but so far, it just makes letters go all over the canvas.

Here is what I wrote:
-size 1000x1000 xc:orange -pointsize 72 -fill red -annotate +0+72 'Test me' -trim convert -wave %[fx:w]x%[fx:h] $this->layerLocation info: ";

I realize that is way off, but no matter where I wrote info or convert or wave, somehow they are always still w and h instead of the new width and height.
phpknight

Re: Using width and height from info:

Post by phpknight »

Actually, this is closer, but I can't get numbers for w and h. But, if I could, that is close to what I need.

convert -size 1000x1000 xc:orange -pointsize 72 -fill red -annotate +0+72 'Test me' -trim info: convert -wave fx:%wx%h tmp1.png
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Using width and height from info:

Post by Bonzo »

If you are using php why do you not use getimagesize?

Code: Select all

$size = getimagesize("image.jpg");

$w = $size[0];
$h = $size[1];

-size {$w}x{$h} xc:orange -pointsize 72 -fill red -annotate +0+72 'Test me' convert -wave {$w}x{$h} $this->layerLocation info: ";
You could use Freds shell script and call that from exec:

Code: Select all

// I renamed the script from texteffect to texteffect.sh and you need to CHMOD it to 777 or 755
exec("/Full/Path/to/script/texteffect.sh fred.png 2>&1", $array); 
    //Display any errors
    echo "<br><pre>".print_r($array)."<br>"; 
    echo "</pre>";
Use distort:

Code: Select all

exec("convert -font ../fonts/jd.ttf -pointsize 30 label:' RubbleWebs ' \\
-virtual-pixel background -background none -distort arc 180  arc.png");
If you want to do it your original way you will probably need to use two lines and read your width and heights into a variable:

Code: Select all

$Image_dimensions = exec("convert ../original_images/sunflower.jpg -format '%wx%h' info:- ");
Lots of ways to create arched text and get the image data; it depends on how you want to do it :D
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using width and height from info:

Post by fmw42 »

you cannot embed "%[fx:w]" in functions as you have. you have to create a variable and use the variable in those functions.

for example to resize an image to half (dumb way)

halfwidth=`convert imagefile -format "%[fx:w/2]" info:`
halfheight=`convert imagefile -format "%[fx:h/2]" info:`
convert imagefile -resize ${halfwidth}x${halfheight} halfimagefile

Note:
exec("/Full/Path/to/script/texteffect.sh fred.png 2>&1", $array);
If you are using my texteffect script as above, the correct command is:

exec("/Full/Path/to/script/texteffect.sh fred.png", $array);

The 2>&1 is only used when you are doing

exec("/Full/Path/to/script/texteffect.sh -h 2>&1", $array);

to get the help information printed out. When you create an output, you don't want that 2>&1 as far as I understand it.
LooInSpain

Re: Using width and height from info:

Post by LooInSpain »

I use this when changing the image size in php on upload:

Code: Select all

$file_tmp = $_POST["image_file"];
$ThumbWidth = 340; //Largest Size of the Resized Image (Width or Height)
//list width and height and keep height ratio.
list($width, $height) = getimagesize($file_tmp);
$imgratio=$width/$height;
if ($imgratio > 0) {
     $newwidth = $ThumbWidth;
     $newheight = $ThumbWidth/$imgratio;
} else {
     $newheight = $ThumbWidth;
     $newwidth = $Thumbwidth * $imgratio;
}
$new_size = $newwidth ."x". $newheight;
exec("convert -resize $new_size -quality 70 -sharpen 1 -strip /$file_tmp /path/to/newimage");
This seems to work well, hope it helps
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Using width and height from info:

Post by Bonzo »

You want to read in the image first LooInSpain and you do not need your getimagesize() code as
widthxheight Maximum values of height and width given, aspect ratio preserved.

Code: Select all

$file_tmp = $_POST["image_file"];
$ThumbWidth = 340; //Largest Size of the Resized Image (Width or Height)
$new_size = $ThumbWidth."x". $ThumbWidth;
exec("convert $file_tmp -resize $new_size -quality 70 -sharpen 1 -strip path/to/newimage");
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Using width and height from info:

Post by fmw42 »

phpknight wrote:Actually, this is closer, but I can't get numbers for w and h. But, if I could, that is close to what I need.

convert -size 1000x1000 xc:orange -pointsize 72 -fill red -annotate +0+72 'Test me' -trim info: convert -wave fx:%wx%h tmp1.png
You cannot put fx: expressions inline. YOu have to do those thinks before or after processing. So you may have to break up your command into two. Do the first part to get an image. Then get the width and height from that image and put them into variables and then do the second command where you use those variables for the -wave parameter.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Using width and height from info:

Post by anthony »

fmw42 wrote:You cannot put fx: expressions inline.
At least not yet. It is available in specific options such as -set -distort and -sparse-color, as well as the
normal string handling of -annotate -format label: -label -caption (and label: and caption:)

I am pushing for a more global handling of options that would allow this, but it would probably require a some incompatible handling for using such escapes in 'geometry' arguments (such as what -crop, -resize, -wave, -level, etc... use) as these arguments can take an optional '%' sign! -- One suggestion is to go to the use of '$' instead of '%' though that may be too-close to 'unix shell' type usage!

Also I am starting to see the need for the ability to 'format' the output of %[fx:...] numbers before being included into the arguments before being parsed. as well as other 'string handling' escapes.

These incompatibilities may mean that to implement we will need to go to a version 7 IM, and re-design the whole 'escape' handling system.

Developmental notes (very raw) for global inline escapes is in
http://www.imagemagick.org/Usage/bugs/future/#settings
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply