views:

311

answers:

4

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?

A: 

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

Roland
Thank you Roland, that's an excellent class. I'll surely use that but it doesn't suit for the problem I mentioned.
Sreejith
A: 

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 :)

Houssem
Thank you Houssem. I'll give a try..
Sreejith
+1  A: 

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?

André Hoffmann
The scenario is like this. User will have options to select a body image(there are 30 images at least) and then he/she selects the body image(from 60 plus images). At this point, we have to show them a preview like I mentioned above. So I would like to have a general algorithm.Thanks André Hoffmann for you time.
Sreejith
I'm trying to achieve a functionality similar to what done in this page: familystickers.com/family-stickers/default.asp/… Just scroll down to "Create Your Own Family Stickers" in the page and you will see the thing implemented here. I want the same functionality..
Sreejith
Create a database table with all your body images in it and for every body you also store at what position the head needs to be. If there are other accessoires like arms and stuff you can store there position in this table either. It might be a bit painful but I'm afraid that's the best option you have.
André Hoffmann
A: 

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.)

dala