使用 PHP 自动将 3 个图像对齐并调整大小为一个小图像?

发布于 2024-08-04 06:24:11 字数 664 浏览 3 评论 0原文

我不知道这对于 PHP 是否可行,但我想如果可以的话,这里有人会知道如何做。我目前正在开发一个项目,用户可以自定义全身主要头像以在整个网站上使用。有一堆不同的脸部、头发等透明 png 图像,可以选择它们来制作自定义头像。我的工作很好,但这是困难的部分。我希望能够使用脸部、头发和胡须(如果是男性),并自动创建一个 80x80 的图像,该图像将用作论坛帖子等的小头像。

这有一些障碍。首先,所有图像都是187x404(大部分图像是透明的,人物身体图像是通过图像堆叠获得的,所以面部图像实际上没有那么大)。为此,必须有效地自动裁剪图像,以便删除所有额外的空间,并在 80x80 的位置显示实际的脸部、头发或胡须部分。

第二个问题是,一些头发或胡须(当放置在全尺寸面部图像上时)会延伸超过 80x80 并被剪掉。因此,图像必须以完整尺寸拼凑在一起,然后调整大小以适合 80x80。

我知道将 3 个图像组合成一个的基本方法(使用 PHP 将 2-3 个透明 PNG 图像彼此组合在一起),但据我所知。如果我疯了并且这是不可能的,那么请告诉我。我可能把这个复杂化了,所以如果你看到并且明显更容易实现这个目标的方法,我很想听听。

I don't know if this is even possible with PHP, but I figured if it is, someone here will know how. I'm currently working on a project where users can customize a full body main avatar to be used throughout the site. There are a bunch of different face, hair, etc transparent png images that can be selected to make their custom avatar. I have this working great, but here is the hard part. I want to be able to use the face, hair, and beard (if male), and automatically create an 80x80 image that will be used as their small avatar for forum posts, etc.

There are a few obstacles with this. First, all of the images are 187x404 (big amounts of the image are transparent, the character body image is achieved by stacking the images, so a face image isn't actually that big). For this to work, the images would effectively have to be automatically cropped so that all of the extra space was removed and the actual face, hair, or beard part showed in the 80x80 spot.

The second issue is that some of the hair or beards (when placed on the full-size face image) would extend past the 80x80 and be chopped off. So the image would have to be pieced together at full size, and then resized to fit in 80x80.

I know the basic way of combining the 3 images into one (Combine 2-3 transparent PNG images on top of each other with PHP), but that is as far as I've gotten. If I'm crazy and this isn't possible then tell me. I'm probably way overcomplicating this, so if you see and obviously easier way to achieving this, I would love to hear it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

末が日狂欢 2024-08-11 06:24:11

我认为你需要首先决定是裁剪、调整大小还是两者的组合(裁剪到更大的正方形并调整其大小)。

无论如何,如果您已经将图像合并为一张,那么所有三个选项都很容易在 php 中完成。看一下 imagecopyresampled()

I think you need to decide first, cropping, resizing or a combination of both (cropping to a bigger square and resizing that).

Anyway, if you already have the images combined into one, all three options are easy to do in php. Take a look at imagecopyresampled().

江南月 2024-08-11 06:24:11

最简单的方法就是始终将面部/头发/胡须放在图像的同一区域。然后将该区域裁剪掉。

如果必须的话,您可以为每个图像存储额外的数据,指定图像中必须在小头像中可见的矩形。然后在您合成的所有图像中获取这些矩形的最大末端,并将其裁剪+缩小到您的小头像尺寸。

但是,请注意,将 PNG 图像大小调整几个像素(例如 83x83 -> 80x80)会显着降低质量,特别是对于具有大量定义边缘的图像。这是因为新图像中的许多像素[几乎]均匀地分布在原始图像的 4 个像素之间,而在具有锐利边缘的图像中,这会导致模糊。

因此,缩小图像以适合肖像不仅很困难,而且还会降低质量。我宁愿把胡子剪掉!

The easiest way is just to always fit the face/hair/beard in the same area of the image. Then just crop that area out.

If you must, you can store extra data for each image specifying a rectangle in the image that must be visible in the small avatar. Then take the maximum extremities of these rectangles in all the images you compose, and crop+shrink that down to your small avatar size.

However, be aware that resizing PNG images by a few pixels (e.g. 83x83 -> 80x80) can substantially reduce the quality, particularly for images with lots of defined edges. This is because there are many pixels in the new image that are [nearly] evenly split between 4 pixels from the original image, and in images with sharp edges this leads to blurring.

So, shrinking an image to fit a portrait is not just difficult but also reduces quality. I'd cut off the beard instead!

微凉 2024-08-11 06:24:11

我可能过于简单化了,但是您可以尝试:

  1. 跟踪预合成的最大面部尺寸。

  2. 将合成图像输出到临时文件。

  3. 裁剪第 1 步中最大值的正方形

  4. 将裁剪后的图像部分的大小调整为 80 x 80

I may be oversimplifying this, but can you try:

  1. Keep track of max face size dimensions pre-compositing.

  2. Output the composite image to a temporary file.

  3. Crop square of largest values from step 1

  4. Resize cropped image portion to 80 x 80

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