[英]PHP thumbnail generation
I'm trying to generate thumbnails of the images my users upload. 我正在尝试生成用户上传的图片的缩略图。 I have got the basic functionality to work by having my thumbnail class generate a thumbnail that is 50% of the width and height of the original image.
我的缩略图类生成的缩略图是原始图像宽度和高度的50%,因此我可以使用基本功能。 However, I'd like to extend its functionality and enforce a hard limit on thumbnails that will be larger than 400px on either side after the 50% reduction.
但是,我希望扩展其功能并对缩略图实施严格限制,在缩小50%之后,缩略图将大于400px。
This is what I have so far: 这是我到目前为止:
$x = $image_info[0]; // width of original image
$y = $image_info[1]; // height of original image
$x_t = $x/2; // width of 50% thumbnail
$y_t = $y/2; // height of 50% thumbnail
$biggest = ($x_t > $y_t) ? $x_t : $y_t; // determine the biggest side of the thumbnail
if($biggest > 400)
{
// Enforce a 400px limit here
/// somehow :(
}
With this hard limit, I want the original image to be scaled down so that no side exceeds 400px, and I want the other side to be scaled down relative so the image doesn't look distorted. 有了这个硬限制,我希望缩小原始图像,使得没有边超过400px,我希望另一面相对缩小,这样图像看起来不会失真。
Being as terrible with math as I am, I can't work out a way to calculate the image dimensions that my thumbnail class should resize the image to. 和我一样对数学一样糟糕,我无法计算出我的缩略图类应该将图像调整大小的图像尺寸。
Any ideas? 有任何想法吗?
You'd have to compute a scaling factor: 您必须计算缩放系数:
$factor = $biggest / 400; // if 503, then factor = 1.2575;
$new_x = $x / $factor;
$new_y = $y / $factor;
and use those two new dimensions for your scaling. 并使用这两个新尺寸进行缩放。 That'll reduce whatever side is $biggest to 400, and proportionally reduce the other dimension to something less than 400.
这将减少最大值为400的任何一方,并将其他维度按比例减少到小于400。
You will have to check for each length, not both at once: 您必须检查每个长度,而不是同时检查两个长度:
if ($x > 400) {
$x_t = 400;
$y_t = $y * (400 / $x);
}
if ($y > 400) {
...
If $x is 600 for example, the calucalation would become $y_t = $y * (400 / 600), thus reducing $y to 2/3 of its original value. 例如,如果$ x为600,则calucalation将变为$ y_t = $ y *(400/600),从而将$ y减少到其原始值的2/3。
And add the same condition for the $y side. 并为$ y方添加相同的条件。 Additionally you might want to apply the calculations concurrently, if neither side is allowed to be larger than 400.
此外,如果任何一方都不允许大于400,您可能希望同时应用计算。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.