将图片调整大小/裁剪/填充到固定大小
我需要将图片调整为固定大小。 但它必须保留宽度和高度之间的因素。
假设我想将图片大小从 238 (w) X 182 (h)
调整为 210 / 150
我现在要做的是:
Original width / target width = 1.333333
Original Height / target Height = 1.213333
现在我采用最小的因子。
现在,自 238 / 1.333333 = 210
以来,我始终具有正确的宽度。 但高度仍然是160
。
如何在不破坏图片的情况下将高度降低到 160
?
我需要裁剪吗? 如果是这样怎么办?
I need to resize a picture to a fixed size. But it has to keep the factors between the width and height.
Say I want to resize a picture from 238 (w) X 182 (h)
to 210 / 150
What I do now is:
Original width / target width = 1.333333
Original Height / target Height = 1.213333
Now I take the smallest factor.
Now I always have the right width since 238 / 1.333333 = 210
.
But the height is still 160
.
How do I get the height down to 160
without ruining the pic?
Do I need to crop? If so how?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
也许看看 PHPThumb (它适用于 GD 和 ImageMagick)
Maybe take look at PHPThumb (it works with GD and ImageMagick)
只是从大图像快速生成高质量缩略图的提示:(来自php.net 站点)
如果您分两个阶段生成缩略图:
那么速度会快得多; 步骤 1 中的调整大小相对于其大小来说质量相对较差,但具有足够的额外分辨率,因此在步骤 2 中质量不错,并且中间图像足够小,可以进行高质量重新采样(在 2:1 调整大小上效果很好)进展非常快。
Just a tip for high-quality fast thumbnail generation from large images: (from the php.net site)
If you do the thumbnail generation in two stages:
then this can be much faster; the resize in step 1 is relatively poor quality for its size but has enough extra resolution that in step 2 the quality is decent, and the intermediate image is small enough that the high-quality resample (which works nicely on a 2:1 resize) proceeds very fast.
我宁愿调整大小,使图像包含在您的限制范围内,然后填写空白部分。 因此,在上面的示例中,您将调整大小以使高度合适,然后用背景颜色向左和右填充(我认为每端各 7 个像素)。
I'd much rather resize so that the image is contained within your limit and then fill out the blank parts. So in the above example you would resize so that the height is OK, then fill up (7 pixels on each end I think) to the left and right with a background color.
在 PHP 源网页中调整图像大小可能会出现问题。 较大的图像(磁盘上接近 2+MB)可能会很大,需要超过 32MB 的内存才能处理。
出于这个原因,我倾向于要么从基于 CLI 的脚本(最多可使用 128MB 的可用内存)执行此操作,要么使用标准命令行(也可以根据需要使用尽可能多的内存)。
“convert”是 ImageMagick 命令行工具的一部分。
Resizing images from within a PHP-sourced webpage can be problematic. Larger images (approaching 2+MB on disk) can be so large that they need more than 32MB of memory to process.
For that reason, I tend to either do it from a CLI-based script, with up-to 128MB of memory available to it, or a standard command line, which also uses as much as it needs.
'convert' is part of the ImageMagick command line tools.
这些是缩略图吗? 如果是的话,裁剪就不是什么大问题了。 我们一直这样做。 我什至不回避将任意比例裁剪成残缺的二次缩略图,完全搞乱图像(是的,我就是那个硬核),如果它看起来不错的话。 这是设计师对技术问题的回答,但仍然如此。 不要害怕庄稼!
are these thumbnails? if they are, cropping is not a huge problem. we do it all the time. i don't even shy away from cropping arbitrary ratios into crippled quadratic thumbnails, completely messing up the image (yes, i'm that hardcore), if it just looks good. this is a designers answer to a technical question, but still. don't fear the crop!
我觉得有点混乱..
如果你只想调整大小,保持原来的比例,正确的操作是:
否则,如果前缀newWidth和newHeight无法改变,而拇指比例与原来比例不同,唯一的办法就是裁剪或添加边框到拇指。
如果你想采取剪切方式,这个函数可以帮助你(我几年前在 5 分钟内写的,也许需要一些改进..它只适用于 jpg,例如;):
I think there is a little confusion..
If you only want to resize it, keeping the original ratio, the right operation is:
Else, if the prefixed newWidth and newHeight cant be changed, and the thumb ratio is different from the original ratio, the only way is to crop or add borders to the thumb.
If your'e on to take the cut way, this function can help you (i wrote years ago in 5 minutes, maybe need some improvement.. it works only with jpg, for example ;):
该技术是:
最后,如果您对如何进行调整大小数学感到困惑,请记住,如果源图像和目标图像的比例相同,则以下关系成立:
如果您知道三个参数,则可以轻松计算第四个参数。
我写了一篇关于此的文章:
裁剪以适合图像使用ASP/PHP
The technique is to:
Lastly if you are puzzled about how to do the resize math, remember that if proportions of source and destination images are same, this relation holds:
If you know three parameters, you can calculate the fourth one easily.
I wrote an article about this:
Crop-To-Fit an Image Using ASP/PHP
您必须从顶部和底部裁剪 5 像素才能达到目标尺寸,但这可能会破坏图片。
实际上,您应该有一个目标宽度或高度,然后按相同比例调整其他尺寸。
You'll have to crop 5 px off the top and bottom to get to your target size, however this could ruin the picture.
Really you should have a target width or height, then adjust the other dimension by the same proportion.
这个解决方案与 Can Berk Güder 的解决方案基本相同,但在花了一些时间写作和评论后,我想发帖。
此函数创建的缩略图与您指定的大小完全相同。
图像的大小会调整为最适合缩略图的大小。 如果它在两个方向上不完全适合,则会在缩略图中居中。 大量评论解释了正在发生的事情。
用法示例:
This solution is basically the same as Can Berk Güder's, but after having spent some time writing and commenting, I felt like posting.
This function creates a thumbnail that is exactly as big as the size you give it.
The image is resized to best fit the size of the thumbnail. If it does not fit exactly in both directions, it's centered in the thumnail. Extensive comments explain the goings-on.
Example usage:
这不会裁剪图片,但如有必要,会在新图像周围留下空间,我认为这是创建缩略图时更好的方法(比裁剪)。
This doesn't crop the picture, but leaves space around the new image if necessary, which I think is a better approach (than cropping) when creating thumbnails.
您有 Imagick 吗? 如果是这样,您可以使用它加载图像并执行类似
的操作thumbnailimage()
在这里您可以跳过任一参数(高度或宽度),它将正确调整大小。
Do you have Imagick? If so you can load the image with it and do something like
thumbnailimage()
There you can skip either of the parameters (height or width) and it will resize correctly.