图像大小调整(PHP / GD) [英] Image Resizing in (PHP/GD)
问题描述
我正在寻找帮助/建议,找到最有效的方法,使用 PHP / GD 尽可能小地调整图像的大小,同时保留原始图像的宽高比但确保调整大小的图像大于定义的最小宽度&高度。
I'm looking for help/suggestions in finding the most efficient way to resize an image to be as small as possible using PHP/GD while preserving the aspect ratio of the original image but ensuring the the resized image is bigger than a defined minimum width & height.
例如,调整大小的图像必须具有宽度> = 400且高度> = 300 ,但应尽可能接近尺寸尽可能保持原始宽高比。
For example, the resized image must have a width >= 400 and a height >= 300 but should be as close to those dimensions as possible while maintaining the original aspect ratio.
这样风景图像的理想高度为300 或略大于宽度> = 400 ,纵向图像的理想宽度为400 或略大,高度> = 300 。
Such that a "landscape" image would have an ideal height of 300 or slightly larger with a width >= 400 and a "portrait" image would have an ideal width of 400 or slightly larger with a height >= 300.
推荐答案
这似乎可以完成工作......
This seems to get the job done...
$data = @getimagesize($image);
$o_w = $data[0];
$o_h = $data[1];
$m_w = 400;
$m_h = 300;
$c_w = $m_w / $o_w;
$c_h = $m_h / $o_h;
if($o_w == $o_h){
$n_w = ($n_w >= $n_h)?$n_w:$n_h;
$n_h = $n_w;
} else {
if( $c_w > $c_h ) {
$n_w = $m_w;
$n_h = $o_h * ($n_w/$o_w);
} else {
$n_h = $m_h;
$n_w = $o_w * ($n_h/$o_h);
}
}
这篇关于图像大小调整(PHP / GD)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!