在php中连接两个图像

发布于 2024-07-29 12:40:33 字数 1563 浏览 1 评论 0原文

如何将头部图像和身体图像结合起来,以便头部图像精确地固定在身体图像中的脖子上。

文件位于:

我已尝试 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.gifbody1.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:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

陌伤ぢ 2024-08-05 12:40:33

您可以使用另一种方法来执行此操作,即使用图像叠加,因此您需要有一个canevas(基本图像),并且您可以在其上添加另一张图片..

示例:

$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);

希望这有帮助:)

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

别想她 2024-08-05 12:40:33

正如您在 Preview.php 中看到的,头部图像未正确放置在身体图像上。

谁能帮我找到一个同时适用于 body2.gif 和 body1.gif 的算法吗?

它是否只适用于您提供的两张图片,或者您是否正在寻找一种适合头颈的通用算法?

如果您想做前者,只需找到正确的参数即可,您可以自己做。 (反复试验)。

如果你追求后者,恐怕你做不到,因为它是一些非常先进的东西。 它需要模式识别(在身体图像中找到颈部)和其他一些人工智能技术来获得正确的头身比例。

也许您可以告诉我们更多有关您想通过此实现的目标吗?

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?

梦里兽 2024-08-05 12:40:33

有一个很好的 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

半透明的墙 2024-08-05 12:40:33

这不是 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.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文