這些是此代碼執行步驟
- 複製源圖像
- 計算圖像尺寸
- 調整大小圖像(指定最大高度/寬度)
- 保留寬高比
- 寫入目的地圖片
這是從各種代碼片段創建的 我在這裏發現在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