我正在寻找帮助/建议,以找到最有效的方法来使用以下方法将图像大小调整为尽可能小PHP/GD同时保留原始图像的纵横比,但确保调整后的图像大于定义的最小宽度和高度。
例如,调整大小后的图像必须具有宽度 >= 400 且高度 >= 300但应尽可能接近这些尺寸,同时保持原始的纵横比。
这样“风景”图像就会有一个理想的高度300或稍大一些宽度 >= 400和“肖像”图像会有一个理想的宽度400或稍大一些高度 >= 300.
我相信这就是您正在寻找的;具体来说,中间一栏的图片:
以下代码源自使用 ASP/PHP 裁剪图像 http://salman-w.blogspot.com/2009/04/crop-to-fit-image-using-aspphp.html:
list(
$source_image_width,
$source_image_height
) = getimagesize( '/path/to/image' );
$target_image_width = 400;
$target_image_height = 300;
$source_aspect_ratio = $source_image_width / $source_image_height;
$target_aspect_ratio = $target_image_width / $target_image_height;
if ( $target_aspect_ratio > $source_aspect_ratio )
{
// if target is wider compared to source then
// we retain ideal width and constrain height
$target_image_height = ( int ) ( $target_image_width / $source_aspect_ratio );
}
else
{
// if target is taller (or has same aspect-ratio) compared to source then
// we retain ideal height and constrain width
$target_image_width = ( int ) ( $target_image_height * $source_aspect_ratio );
}
// from here, use GD library functions to resize the image
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)