2012-07-11 169 views
0

我正在使用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; 
} 

圖像被旋轉但仍然保持其原始高寬比並裁剪照片。任何想法我做錯了什麼?

回答

1

你的問題是如何設置的新值:

$new_width = $img_width * $scale; 
$new_height = $img_height * $scale; 

應該是,在旋轉90度的情況下:

$new_width = $img_height * $scale; // reverse height and width 
$new_height = $img_width * $scale; // reverse height and width 

編輯:而作爲原始圖像舊的寬度和高度必須顛倒:

$success = $src_img && @imagecopyresampled(
     $new_img, 
     $src_img, 
     0, 0, 0, 0, 
     $new_width, 
     $new_height, 
     $img_height, // reverse width and height 
     $img_width  // reverse width and height 
) && $write_image($new_img, $new_file_path, $image_quality); 
+0

嗯...仍然不正確。 – Paul 2012-07-11 01:09:31

+0

@Paul旋轉源圖像後,還需要在成功函數中切換源圖像的寬度和高度,因爲新寬度是舊高度等。 – jeroen 2012-07-11 01:22:27

+0

謝謝jeroen!這對90度的工作,但在180的情況下,我需要再次扭轉它們... – Paul 2012-07-11 01:31:49

0

請注意:如果您打算使用那麼多@來忽略錯誤,那麼您可以在代碼的開始處放置error_reporting(0);

其次,我看到像素比率沒有變化的原因是因爲你的$scale倒退了。你會說scale = Whole/Part除非最大尺寸小於圖像尺寸,否則幾乎總會給你一個大於1的值(增加像素比率)。你會希望它是scale = Part/Whole所以它的小數比例(即100px/400px,scale = .25)。

祝你好運!

+0

Thanks for the 反饋。我的比例尺工作正常,但問題是旋轉的圖像仍具有原始高寬比。 – Paul 2012-07-11 00:48:00

相關問題