Newbie here needs help

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?".
stingerman

Re: Newbie here needs help

Post by stingerman »

Bonzo wrote:You will need some sort of code to tell php what to do. I put mine in an upload form and resize on upload.

You can resize "on the fly" using something like:
PUT THIS ON A PAGE OF ITS OWN AND CALL thumbnail.php

Code: Select all

<?php
$photo = $_GET['p'];
$height  = '125';
$width = '200';
$cmd = "convert $photo -thumbnail {$width}x{$height} JPG:-";
header("Content-type: image/jpeg");
passthru($cmd, $retval);
?>
Call from your page using:

Code: Select all

<img src="thumbnail.php?p=input.jpg">
Is there a way to incorporate the drop shadow command into this?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Newbie here needs help

Post by Bonzo »

Code: Select all

<?php
$photo = 'original.jpg';
$height  = '125';
$width = '200';
$cmd = "convert $photo -matte -thumbnail {$width}x{$height}  ( +clone -background black -shadow 90x3+15+15 ) +swap -gravity northwest -geometry -3-3 -composite -background white -flatten +repage PNG:-";
header("Content-type: image/jpeg");
passthru($cmd, $retval);
?>
This would need the output to be a png; you may be able to modify it so the background is the same colour as your page. Not very speedy though and depends on you IM version.
You may need to use \( \) rather than ( )
stingerman

Re: Newbie here needs help

Post by stingerman »

Is there some reason the pics are not thumbnailed to the exact size? I notice some pics are off a few pixels here or there.

Does it depend on the original size pic or something in the command?

For example setting the width and height to 180x270 of a pic 900x1316 -

Imagemagick thumnails it to: 180x263
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Newbie here needs help

Post by anthony »

IM tries to preserve the aspect ratio of images, so that circles remain circles!

If that is not wanted add a '!' flag to the resize/thumbnail option.
Otherwise you can pad out the image using techniques given in
Im Examples, Thumbnails, Padding
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
stingerman

Re: Newbie here needs help

Post by stingerman »

anthony wrote:IM tries to preserve the aspect ratio of images, so that circles remain circles!

If that is not wanted add a '!' flag to the resize/thumbnail option.
Otherwise you can pad out the image using techniques given in
Im Examples, Thumbnails, Padding
Well, I just added the image attributes in the image tags and it resizes automatically. Guess, its a good idea to do that. ;)
stingerman

Re: Newbie here needs help

Post by stingerman »

Another question :D

In the above php code, 'thumbnail' is used.

Does that give less quality compared to 'resize'?

Which is better and how would I use resize in that code?

Thanks!
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Newbie here needs help

Post by Bonzo »

I think the quality of thumbnail and resize is the same. Thumbnail strips out all the EXIF data automaticaly where as with resize you need to use -strip.

To use resize just swap the words.

To speed up jpg resizing try changing:

Code: Select all

$cmd = "convert $photo -matte -thumbnail {$width}x{$height}
To

Code: Select all

$cmd = "convert -size {$width}x{$height} $photo -matte -thumbnail {$width}x{$height}  
stingerman

Re: Newbie here needs help

Post by stingerman »

Awesome Thanks!
stingerman

Re: Newbie here needs help

Post by stingerman »

Is there a way to incorporate two commands into one php?

For instance use the above to resize but then add a shadow and convert to png?

I found this drop shadow code but not sure what to do with it, lol.

Basically I want to resize an image down to 80x120, add a drop shadow with an offset of 8 (for both x and y), blur radius 15 and opacity 80. This makes the image 110x150. Would save me some time instead of using The Gimp. :)

Code: Select all

  convert -page +4+4 thumbnail.gif -matte \
          \( +clone -background navy -shadow 60x4+4+4 \) +swap \
          -background none -mosaic     shadow_soft.png
stingerman

Re: Newbie here needs help

Post by stingerman »

You know what I am having a hard time figuring out (surprise!) is the quality command.

I cant seem to get it to be equivalent to of say 85 (of a photo program like the gimp).
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Newbie here needs help

Post by fmw42 »

IM quality factors may not be truly linear percents.
stingerman

Re: Newbie here needs help

Post by stingerman »

I kinda figured something out, I just exported (from a pdf) the original image at smaller scale. So that worked.

Speaking of that is there a way to use imagemagick to export pdf to jpg?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Newbie here needs help

Post by Bonzo »

Try this code - its one I used for a website just change the values to what you want:

Code: Select all

exec("convert $value -matte -resize 560x450 \( +clone -background black -shadow 90x3+15+15 \) +swap -gravity northwest -geometry -3-3 -composite -background white -flatten +repage $photo");
To convert a pdf you use the same code as normal but you need ghostscript installed:

Code: Select all

exec("convert input.pdf output.jpg");
To control the quality a bit more add a density:

Code: Select all

exec("convert -density 300 input.pdf output.jpg");
To just convert the second page ( note the first page is 0 I think not 1 ):

Code: Select all

exec("convert input.pdf[1] output.jpg");
stingerman

Re: Newbie here needs help

Post by stingerman »

Bonzo wrote:Try this code - its one I used for a website just change the values to what you want:

Code: Select all

exec("convert $value -matte -resize 560x450 \( +clone -background black -shadow 90x3+15+15 \) +swap -gravity northwest -geometry -3-3 -composite -background white -flatten +repage $photo");
Ok, I messed something up:

Here is my php:

Code: Select all

<?php
$photo = $_GET['p'];
$height  = $_GET['h'];
$width = $_GET['w'];
$cmd = "/usr/local/bin/convert exec("convert $value -matte -resize 960x1457 \( +clone -background black -shadow 90x3+15+15 \) +swap -gravity northwest 
-geometry -3-3 -composite -background white -flatten +repage $photo");
passthru($cmd, $retval);
?>
and the href:

Code: Select all

SRC="resizepic.php?p=images/xxxx.jpg&w=960&h=1457"
Image not showing up. Thanks, again.
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Newbie here needs help

Post by Bonzo »

My code was to save an image to do what you want try this:

Code: Select all

<?php
$photo = $_GET['p'];
$height  = $_GET['h'];
$width = $_GET['w'];
$cmd = "/usr/local/bin/convert $photo -matte -resize {$width}x{$height} \( +clone -background black -shadow 90x3+15+15 \) +swap -gravity northwest 
-geometry -3-3 -composite -background white -flatten +repage ";
passthru($cmd, $retval);
?>
Post Reply