在php中连接两个图像
如何将头部图像和身体图像结合起来,以便头部图像精确地固定在身体图像中的脖子上。
文件位于:
- 头像: http://yajurinfotech.com/projects/stickers/head1.gif
- 身体图片:http://yajurinfotech.com/projects/stickers/body2.gif
我已尝试 http://yajurinfotech.com/projects/stickers/预览.php。
我目前得到的是:
$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);
正如您在 preview.php
中看到的,头部图像没有正确放置在身体图像上。
谁能帮我找到一种适用于 body2.gif
和 body1.gif
的算法?
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:
- Head Image: http://yajurinfotech.com/projects/stickers/head1.gif
- Body Image: http://yajurinfotech.com/projects/stickers/body2.gif
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
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用另一种方法来执行此操作,即使用图像叠加,因此您需要有一个canevas(基本图像),并且您可以在其上添加另一张图片..
示例:
希望这有帮助:)
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 :
hope that's helpful :)
它是否只适用于您提供的两张图片,或者您是否正在寻找一种适合头颈的通用算法?
如果您想做前者,只需找到正确的参数即可,您可以自己做。 (反复试验)。
如果你追求后者,恐怕你做不到,因为它是一些非常先进的东西。 它需要模式识别(在身体图像中找到颈部)和其他一些人工智能技术来获得正确的头身比例。
也许您可以告诉我们更多有关您想通过此实现的目标吗?
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?
有一个很好的 PHP 类可供您使用,它执行各种图像操作,我相信这会对您有所帮助。
http://www.verot.net/php_class_upload_samples.htm
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
这不是 php 的工作。
显而易见的解决方案是设计图像,使所有颈部都位于同一位置,例如在中心。
否则,您将需要每个图像的颈部位置的数据库或列表。 然后您只需移动头部或身体图像的相对位置即可。
移动图像可以使用 css 或使用可变大小的透明或背景色像素来完成。 (即生成一个1x1像素大小的透明png图像。然后用html调整其大小。)
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.)