Reputation: 61
How can I join a head image and body image so that the head image is precisely fixed over the neck in body image.
Files are at:
I have made an attempt at http://yajurinfotech.com/projects/stickers/preview.php.
What I've got at present is:
$h = 'head1.gif';
$b = 'body2.gif';
$headResource = imagecreatefromgif($h);
$bodyResource = imagecreatefromgif($b);
list($headWidth, $headHeight) = getimagesize($h);
list($bodyWidth, $bodyHeight) = getimagesize($b);
$previewHeight = $headHeight+$bodyHeight;
$previewWidth = $headWidth;
$previewResource = imagecreatetruecolor($previewWidth, $previewHeight);
//make background white
$white = imagecolorallocate($previewResource, 255, 255, 255);
imagefill($previewResource, 0, 0, $white);
// Copy head image
imagecopyresized($previewResource,$headResource, ($headHeight/4)+3,($headHeight/2)-3,0,0,$previewWidth/4,$previewHeight/4,$headWidth,$headHeight);
//copy body image
imagecopy($previewResource,$bodyResource,0,$headHeight,0,0,$bodyWidth,$bodyHeight);
header('Content-type: image/gif');
imagegif($previewResource);
As you can see in preview.php
, head image is not placed correctly over the body image.
Can anyone help me find an algorithm that works for both body2.gif
and body1.gif
?
Upvotes: 2
Views: 2775
Reputation: 2025
This is not a job for php.
The obvious solution would be to design the images so that all the necks are in the same position, e.g. at the center.
Otherwise you will need a database or listing of where the neck is for each image. Then you will just have to shift the relative location of the head or body image.
Shifting images can be done with css or by using a variable sized transparent or background colored pixel. (That is, generate a transparent png images 1x1 pixels large. Then resize it with html.)
Upvotes: 0
Reputation: 3553
As you can see in preview.php, head image is not placed correctly over the body image.
Can anyone help me find an algorithm that works for both body2.gif and body1.gif?
Does it only have to work for the two pictures you supplied or are you looking for a general algorithm that fits a head on a neck?
If you want to do the former that's just a matter of finding the correct parameters which is something that you could do on your own. (trial and error).
If you're pursuing the latter I'm afraid that you can't do that as it's some really advanced stuff. It would require pattern recognition(to find the neck in the body image) and some other AI-stuff to get the head-body ratio right.
Maybe you can tell us a bit more about what you want to achieve with this?
Upvotes: 1
Reputation: 169
you can use another way to do this, using image superposition, so you will need to have a canevas (base image) and you can add another picture over it ..
example :
$background = imagecreatefromjpeg($_SERVER['DOCUMENT_ROOT']. base_path()."/files/polaroid.jpg");
$insert = imagecreatefromjpeg($path. 'photos/'.$imgName);
imagecolortransparent($insert, imagecolorat($insert, 0, 0));
$insert_x = imagesx($insert);
$insert_y = imagesy($insert);
imagecopymerge($background, $insert, 39, 32, 0, 0, $insert_x, $insert_y, 100);
hope that's helpful :)
Upvotes: 1
Reputation: 36899
There a nice PHP class that you could use available, it performs all kinds of image manipulation, I'm sure this will help you.
http://www.verot.net/php_class_upload_samples.htm
Upvotes: 0