我正在使用jQuery文件上傳的一些PHP代碼,我試圖在保存之前旋轉圖像。以下是我的函數調用:爲什麼旋轉的圖像具有相同的寬高比?
public function CreateThumb($file_name, $options){
$file_path = $options['src_dir'].$file_name;
$new_file_path = $options['dst_dir'].$file_name;
list($img_width, $img_height) = @getimagesize($file_path);
if (!$img_width || !$img_height) {
return false;
}
$scale = min(
$options['max_width']/$img_width,
$options['max_height']/$img_height
);
$new_width = $img_width * $scale;
$new_height = $img_height * $scale;
$new_img = @imagecreatetruecolor($new_width, $new_height);
switch (strtolower(substr(strrchr($file_name, '.'), 1))) {
case 'jpg':
case 'jpeg':
$src_img = @imagecreatefromjpeg($file_path);
$write_image = 'imagejpeg';
$image_quality = isset($options['jpeg_quality']) ?
$options['jpeg_quality'] : 95;
break;
case 'gif':
@imagecolortransparent($new_img, @imagecolorallocate($new_img, 0, 0, 0));
$src_img = @imagecreatefromgif($file_path);
$write_image = 'imagegif';
$image_quality = null;
break;
case 'png':
@imagecolortransparent($new_img, @imagecolorallocate($new_img, 0, 0, 0));
@imagealphablending($new_img, false);
@imagesavealpha($new_img, true);
$src_img = @imagecreatefrompng($file_path);
$write_image = 'imagepng';
$image_quality = isset($options['png_quality']) ?
$options['png_quality'] : 9;
break;
default:
$src_img = null;
}
$src_img = imagerotate($src_img, 90, 0) ;
$success = $src_img && @imagecopyresampled(
$new_img,
$src_img,
0, 0, 0, 0,
$new_width,
$new_height,
$img_width,
$img_height
) && $write_image($new_img, $new_file_path, $image_quality);
// Free up memory (imagedestroy does not delete files):
@imagedestroy($src_img);
@imagedestroy($new_img);
return $success;
}
圖像被旋轉但仍然保持其原始高寬比並裁剪照片。任何想法我做錯了什麼?
嗯...仍然不正確。 – Paul 2012-07-11 01:09:31
@Paul旋轉源圖像後,還需要在成功函數中切換源圖像的寬度和高度,因爲新寬度是舊高度等。 – jeroen 2012-07-11 01:22:27
謝謝jeroen!這對90度的工作,但在180的情況下,我需要再次扭轉它們... – Paul 2012-07-11 01:31:49