sun*_*jie 43 php resize image image-processing
我有一个定义的固定宽度和高度来调整图像大小.但是,我有这个问题,因为图像可以有任何类型的尺寸比(它可以是垂直或水平).在这种情况下,固定的宽度和高度会引起问题.我想以更智能的方式计算宽度和高度.
例如,假设我已经定义了宽度1024px和高度768px.我想调整一个垂直的图像(高度为1100px,宽度为200px).所以在我的情况下它会调整到固定大小(1024x768),因此宽度将从100px增加到768px,这将非常难看.同样,如果图像的高度小于768px,则会将力度增加到768px.
因此,我想根据原始图像尺寸比率计算新的图像尺寸.让我们说如果上面的示例图像应该调整到最大高度768px,但那么宽度呢?它已经小于我的"最大宽度",即200px,那么宽度应保持不变吗?还是应该进一步减少?
同样,如果图像的高度为200px,宽度为1100px.所以宽度应减少到1024px,但高度怎么样?
第三个问题是,假设高度和宽度都超过最大高度和最大宽度,我们假设宽度:1100px和高度:4000px.现在,由于宽度和高度都超过最大宽度和最大高度,但图像是垂直的,它将使其成为水平.那么如何在这种情况下检查是否应根据最大高度或根据最大宽度调整图像大小?
我很感激任何帮助.
dec*_*eze 71
这是我个人抓取图像大小调整代码的代码.首先,您需要的数据:
list($originalWidth, $originalHeight) = getimagesize($imageFile);
$ratio = $originalWidth / $originalHeight;
Run Code Online (Sandbox Code Playgroud)
然后,此算法尽可能地将图像拟合到目标大小,保持原始宽高比,而不是将图像拉伸大于原始图像:
$targetWidth = $targetHeight = min($size, max($originalWidth, $originalHeight));
if ($ratio < 1) {
$targetWidth = $targetHeight * $ratio;
} else {
$targetHeight = $targetWidth / $ratio;
}
$srcWidth = $originalWidth;
$srcHeight = $originalHeight;
$srcX = $srcY = 0;
Run Code Online (Sandbox Code Playgroud)
这会使图像完全填满目标尺寸,而不是拉伸它:
$targetWidth = $targetHeight = min($originalWidth, $originalHeight, $size);
if ($ratio < 1) {
$srcX = 0;
$srcY = ($originalHeight / 2) - ($originalWidth / 2);
$srcWidth = $srcHeight = $originalWidth;
} else {
$srcY = 0;
$srcX = ($originalWidth / 2) - ($originalHeight / 2);
$srcWidth = $srcHeight = $originalHeight;
}
Run Code Online (Sandbox Code Playgroud)
这会实际调整大小:
$targetImage = imagecreatetruecolor($targetWidth, $targetHeight);
imagecopyresampled($targetImage, $originalImage, 0, 0, $srcX, $srcY, $targetWidth, $targetHeight, $srcWidth, $srcHeight);
Run Code Online (Sandbox Code Playgroud)
在这种情况下,$size宽度和高度(方形目标尺寸)只是一个数字.我相信你可以修改它以使用非方形目标.它还应该为您提供可以使用的其他调整大小算法的灵感.
小智 51
$ratio = $originalWidth / $originalHeight
Run Code Online (Sandbox Code Playgroud)
如果你想改变高度:
$targetWidth = $targetHeight * $ratio
Run Code Online (Sandbox Code Playgroud)
如果要更改宽度:
$targetHeight = $targetWidth / $ratio
Run Code Online (Sandbox Code Playgroud)
jil*_*wit 13
您想要的是保持原始图像的纵横比.这是图像的宽度和高度之间的比率.因此,您需要计算在垂直和水平方向上调整图像大小的因子,然后保持两者中的较高者.在伪代码中:
target_height = 768
target_width = 1024
# v_fact and h_fact are the factor by which the original vertical / horizontal
# image sizes should be multiplied to get the image to your target size.
v_fact = target_height / im_height
h_fact = target_width / im_width
# you want to resize the image by the same factor in both vertical
# and horizontal direction, so you need to pick the correct factor from
# v_fact / h_fact so that the largest (relative to target) of the new height/width
# equals the target height/width and the smallest is lower than the target.
# this is the lowest of the two factors
im_fact = min(v_fact, h_fact)
new_height = im_height * im_fact
new_width = im_width * im_fact
image.resize(new_width, new_height)
Run Code Online (Sandbox Code Playgroud)
小智 8
这是有效的.
function calculateDimensions($width,$height,$maxwidth,$maxheight)
{
if($width != $height)
{
if($width > $height)
{
$t_width = $maxwidth;
$t_height = (($t_width * $height)/$width);
//fix height
if($t_height > $maxheight)
{
$t_height = $maxheight;
$t_width = (($width * $t_height)/$height);
}
}
else
{
$t_height = $maxheight;
$t_width = (($width * $t_height)/$height);
//fix width
if($t_width > $maxwidth)
{
$t_width = $maxwidth;
$t_height = (($t_width * $height)/$width);
}
}
}
else
$t_width = $t_height = min($maxheight,$maxwidth);
return array('height'=>(int)$t_height,'width'=>(int)$t_width);
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
88361 次 |
| 最近记录: |