composite image with gravity

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
robuntu
Posts: 41
Joined: 2012-01-22T10:56:10-07:00
Authentication code: 8675308

composite image with gravity

Post by robuntu »

Hi,
This is what I have:
I have a large image ( called image)
and a small thumbnail image (called thumb)
The thumb is a rectangle: landscape or portrait.
I want to place the thumb into a square area on the image using gravity relative to the square.
When using scaleImage (w,h) and compositeImage(x,y) I have an equivalent to gravity northwest.

Is there a way to perform a gravity setting to this problem?

I even thought about creating a tranparent square image where I put in the scaled thumb
and then composite this square to the image, but even so I don't know how to
use the gravitiy to place the thumb in the square.

If there is no straight php solution but an easy way using command line,
I am fine using an exec() command as I have a private server and security is no issue.

Thanks for your help
Roland
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: composite image with gravity

Post by Bonzo »

I tried this a while ago and it did not work ( Can not remember the problem now ) but may give you a starting point:

Code: Select all

bool compositeImage ( Imagick $composite_object , int $composite , int $x , int $y [, int $channel = Imagick::CHANNEL_ALL ] )

 <?php 
$im = new Imagick($input); 
$top = new Imagick('newPseudoImage.jpg'); 
$im->compositeImage($top, Imagick::COMPOSITE_SCREEN, 0, 0 ); 
$im->writeImage('compositeImage.jpg');  
$im->destroy(); 
?> 
I personaly would use exec() :)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: composite image with gravity

Post by fmw42 »

I have a large image ( called image)
and a small thumbnail image (called thumb)
The thumb is a rectangle: landscape or portrait.
I want to place the thumb into a square area on the image using gravity relative to the square.
What do you mean by "using gravity relative to the square". Gravity settings are relative to the large (first) image in the convert command, either top left or center, etc. You probably need to use the default -gravity northwest. Then you need to use -geometry to locate the top left corner of the square area for placement of the resized thumbnail. But do you want the thumbnail to be cropped or padded to square or distorted?

Lets say your thumbnail is to be 100x100 and cropped.
And say you have the area in the large image with its top left corner at 50,50

convert largeimage \( thumbnailimage -resize 100x100^ -gravity center -crop 100x100+0+0 +repage \) \
-gravity northwest -geometry +50+50 -compose over -composite resultimage

Note you may need to escape the ^ as \^ or put the whole argument in quotes as "100x100^"

That should work in PHP exec()

see
http://www.imagemagick.org/Usage/layers/#convert
http://www.imagemagick.org/Usage/thumbnails/#square
http://www.imagemagick.org/Usage/crop/#crop
http://www.imagemagick.org/script/comma ... p#geometry
robuntu
Posts: 41
Joined: 2012-01-22T10:56:10-07:00
Authentication code: 8675308

Re: composite image with gravity

Post by robuntu »

Hi Fred,
nice to meet you again, thanks for your never ending help!
Hi Bonzo, thanks for your help, too!

I obviously didn't make it clear, sorry.
I have a large image, lets say 3000x4000 and I have an area in this image
at position x/y with w/h which is settable by the user, lets say Position 2000x2000 with Dimensions of 500x500
Into this square I have to fit another image as a (large) thumbnail.
The thumbnail-original Image is larger than 500x500 an not a square.
So I want to resample it the usual way which is "fit into box" - no cropping, no streching.
this thumb will be either 500xwhatsoever or whatsoeverx500, with "whatsoever" beeing less or equal 500.
What I want now is to place this thumb into the square area with a gravity setting. (which has to be user settable again)
Sorry, but I cannot explain better in english...
Thanks again for your help!

Roland
robuntu
Posts: 41
Joined: 2012-01-22T10:56:10-07:00
Authentication code: 8675308

Re: composite image with gravity

Post by robuntu »

Hi,
I just solved it myself, if you know a better way, tell me...

Code: Select all

<?
$vimage = new Imagick();
$vimage->readImage($Filename);	// I leave out all error protection, because it is void for this example
$w=...;									// it doesn't matter how I get these values...
$h=...;
$vimage->scaleImage($w, $h,TRUE);
$x=...;
$y=...;
$gravity=...;							// set by user from a List of (nw, n , ne, w, c, e, sw, s, se)
$imageHight = $vimage->getImageHeight();
$imageWith = $vimage->getImageWidth();
$newx=$x;								// default is norh west
$newy=$y;								// even if a faulty gravity setting is made

// calculate the x position w, c or e
	if (($gravity == "n") OR ($gravity == "c") or ($gravity == "s"))
		{		$newx=$x+($w-$imageWith)/2;		}
	elseif (($gravity == "ne") OR ($gravity == "e") or ($gravity == "se"))
		{		$newx=$x+($w-$imageWith);		}
// calculate the y position n, c or s
	if (($gravity == "w") OR ($gravity == "c") or ($gravity == "e"))
		{		$newy=$y+($h-$imageHight)/2;		}
	elseif (($gravity == "se") OR ($gravity == "s") or ($gravity == "sw"))
		{		$newy=$y+($h-$imageHight);		}
		
	$image->compositeImage($vimage, Imagick::COMPOSITE_DEFAULT, $newx, $newy);
// it works !
?>
Thanks anyway,
Roland
Post Reply