[英]Image Resizing in (PHP/GD)
我正在寻找帮助/建议,以找到最有效的方法来使用PHP / GD将图像调整为尽可能小的尺寸,同时保留原始图像的长宽比,但要确保调整后的图像大于定义的最小宽度和高度。
例如,调整大小的图像必须具有> = 400的宽度和> = 300的高度,但在保持原始长宽比的同时,应尽可能接近这些尺寸。
这样,“风景”图像的理想高度将为300或稍大, 宽度> = 400,而“人像”图像的理想宽度将为400或稍大, 高度> = 300 。
我相信这就是您想要的; 具体来说,中间列中的图片:
以下代码来自使用ASP / PHP裁剪图像 :
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
这似乎完成了工作...
$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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.