调整图像大小时棘手的数学问题
我有一张图像,我想将其水印到其他图像的右下部分。 水印图像的尺寸为179宽、39高。
现在,如果我有另一张尺寸为 150 宽、20 高的图像怎么办? 如果我们尝试使用原始图像对其添加水印,它显然会太大,并且图像本身将被水印图像完全掩盖,对吧?
那么,如何确定一个较小的宽度和高度来调整水印图像的大小,远小于 150 宽度、20 高度,这样它就会像水印一样显示为静止,并且不会完全遮盖图像?
I have an image that I want to be watermarked to the bottom right section of other images.
The dimensions of the watermark image are 179 width, 39 height.
Now what if I have another image whose dimensions are 150 width, 20 height? If we tried to watermark it using the original image, it would obviously be too large and the image itself would be masked completely by the watermark image, right?
So how can I determine a smaller width and height to which I will resize the watermark image, something much less than 150 width, 20 height, so that it will appear still as a watermark and wont mask the image completely?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我可能会将水印缩放到加水印图像的 20%,或 179x39,以最小者为准。
I would probably scale the watermark to 20% of the image being watermarked, or 179x39, whichever is smallest.
您需要选择允许水印存在的较小图像的高度和宽度的最大百分比,然后将水印图像缩放到这两个最大值中较小的一个。 您的结果将根据目标图像的高度是否大于宽度而有所不同,反之亦然。
例如,如果您希望水印不超过目标图像高度的 25%,宽度不超过目标图像的 50%,则将水印缩放到任一尺寸后,您可以看到水印应有多大。
缩放到宽度的 50% 意味着水印将为 75 x 16 像素,这太高了(基于我任意选择的百分比)。
(75 / 179) * 39 = 16
缩放到高度的 25% 意味着水印为 22 x 5。
(5 / 39) * 150 = 19
如果尺寸最终大于原始水印,则由您决定是否放大水印。放大。 与按相同因素减小图像尺寸相比,增加图像尺寸时图像质量下降得更快。
You need to pick a maximum percentage of height and width of the smaller image that you can allow the watermark to be, then scale watermark image to the smaller of these two maximum values. Your results will vary based on whether the target image is taller than it is wide, or vice versa.
For example, if you want the watermark to be no more than 25% of the height and no more than 50% of the width of the target image, you can see how big the watermark should be if you scaled it to either dimension.
Scaling to 50% of the width would mean the watermark would be 75 x 16 pixels, which is too tall (based on the percentages I selected arbitrarily).
(75 / 179) * 39 = 16
Scaling to 25% of the height would mean a watermark of 22 x 5.
(5 / 39) * 150 = 19
If the dimensions end up being larger than the original watermark, it's up to you whether or not to scale the watermark up. Image quality degrades much faster when increasing the size of an image, compared to decreasing its size by the same factor.
首先,我建议您生成一组不同尺寸的水印图像,而不是“即时”调整大小。
下面我概述了如何在网站上动态添加水印的工作流程:
设计一个好的水印
首先,尝试设计具有透明背景的水印。 这将大大降低掩盖目标图像重要部分的风险。 这可以通过使用 gif 或(最好)png 图像文件格式来完成。 只需确保透明水印在浅色和深色背景上都能正常工作即可。
还要考虑如何最好地设计适用于肖像和风景风格图像以及宽高比不合适的图像的水印。 您应该考虑制作两个版本——一种用于宽图像,一种用于高图像。 对于后一种类型,您可以将水印旋转 90 度,或者,如果水印由文本组成,您可能希望将文本拆分为两行或多行。
预渲染几种不同尺寸的水印
因此,不要动态调整水印的大小,而是建议您渲染一组不同尺寸的水印图像。 这只需要完成一次,它将大大提高水印的清晰度和/或易读性(特别是对于较小的目标图像)。
根据网页上图像的不同程度,您可能需要更多或更少的尺寸。 这是您必须做出的设计选择,但我认为您可以只使用两种或三种不同的尺寸。
应用水印
这将在服务器端动态发生(在您的 php 文件中)。 首先使用函数 getimagesize 找出目标图像的尺寸。 有了这些信息,您就必须根据尺寸、宽高比和方向来决定使用哪个版本的水印。 例如
等。
最后,要应用水印,我建议您查看 gd 库。 这是一个功能强大的库,可以执行许多整齐的矩阵,其中包括合并两个图像。 另一种选择是 ImageMagic。
祝你好运!
First out, I recommend you to generate a set of watermark images of different sizes instead of resizing 'on the fly'.
Below I have outlined a workflow of how to watermark images dynamically on a website:
Design a good watermark
First of all, try to design the watermark with a transparent background. This will greatly reduce the risk of cover up essential parts of the target image. This can be done by using the gif or (preferably) the png imagefile format. Just make sure that the transparent watermark works well both on light and dark backgrounds.
Also take into account how to best design the watermark that works both for portrait and landscape style images and images with awkward aspect ratios. You should consider to make two versions -- one for wide images and one for tall images. For the latter type, you could rotate the watermark 90 degrees or, if the watermark consists of text, you might want to split the text into two or more lines.
Pre-render the watermark in several different sizes
So, don't dynamically resize the watermark, instead I recommend you to render a set of watermark images with different sizes. This only has to be done once and it will greatly enhance the clarity and/or legibility of the watermark (especially for smaller target images).
Depending of how different the images will be on the web page, you might need a larger or smaller number of sizes. This is a design choice that you will have to make, but I think you could get away with only two or three different sizes.
Apply the watermark
This will happen dynamically on the server side (in your php-file). First find out the dimensions of the target image by using the function getimagesize. With this information at hand, you'll have to decide which version of the watermark to use based on the size, aspect ratio and orientation. E.g.
etc.
Finally, to apply the watermark I recommend you to look at the gd library. This is a powerful library that can do many neat trix, among others merging two images. An alternative is ImageMagic.
Good luck!