这些是此代码执行步骤
- 复制源图像
- 计算图像尺寸
- 调整大小图像(指定最大高度/宽度)
- 保留宽高比
- 写入目的地图片
这是从各种代码片段创建的 我在这里发现在php.net和其他地方在网络上。
除了 将这些代码放在一起之外,我对此代码不加分。 http://www.php.net/manual/en/function.getimagesize.php
<?php
$source_pic = 'images/source.jpg';
$destination_pic = 'images/destination.jpg';
$max_width = 500;
$max_height = 500;
$src = imagecreatefromjpeg($source_pic);
list($width,$height)=getimagesize($source_pic);
$x_ratio = $max_width/$width;
$y_ratio = $max_height/$height;
if(($width <= $max_width) && ($height <= $max_height)){
$tn_width = $width;
$tn_height = $height;
}elseif (($x_ratio * $height) < $max_height){
$tn_height = ceil($x_ratio * $height);
$tn_width = $max_width;
}else{
$tn_width = ceil($y_ratio * $width);
$tn_height = $max_height;
}
$tmp=imagecreatetruecolor($tn_width,$tn_height);
imagecopyresampled($tmp,$src,0,0,0,0,$tn_width, $tn_height,$width,$height);
imagejpeg($tmp,$destination_pic,100);
imagedestroy($src);
imagedestroy($tmp);
?>
从数据库本身回想起来,我想根据需要调整图像大小 – praveenjayapal 2009-10-05 09:57:57