2013-12-20 68 views
1

遇到圖像調整大小功能的問題。PHP Image Resize,Thumbnailer問題

功能
// Image Resizer 
function MakeThumbnail($inputFile, $filepath, $ext, $maxWidth, $maxHeight){ 
    /* Get some details about the image */ 
    $srcDetails = getimagesize($inputFile); 
    switch ($srcDetails[2]) { 
     case 1: //GIF 
      $source_image = imagecreatefromgif($inputFile); 
      break;   
     case 2: //JPEG 
      $source_image = imagecreatefromjpeg($inputFile); 
      break;   
     case 3: //PNG 
      $source_image = imagecreatefrompng($inputFile); 
      break;   
     case 6: //WBMP 
      $source_image = imagecreatefromwbmp($inputFile); 
      break;   
     default: 
      break; 
    }  
    /* Original Dimensions */ 
    $width = imagesx($source_image); 
    $height = imagesy($source_image);  
    /* find the "desired height" of this thumbnail, relative to the desired width, and vice-versa */ 
    if(($maxWidth <= $width) && $maxHeight <= $height){ 
     if($target_ratio == $original_ratio){ 
      $desired_height = $maxHeight; 
      $desired_width = $maxWidth; 
     }elseif($target_ratio > $original_ratio){ 
      $desired_height = $maxHeight; 
      $desired_width = $width * ($maxHeight/$height); 
     }elseif($target_ratio < $original_ratio){ 
      $desired_height = $height * ($maxWidth/$width); 
      $desired_width = $maxWidth; 
     } 
    }else{ 
     $desired_height = $maxHeight; 
     $desired_width = $maxWidth; 
    } 
imagecopyresized($virtual_image,$source_image,0,0,0,0,$maxWidth,$desired_height,$width,$height);   
    /* create the physical thumbnail image to its destination */ 
    switch ($srcDetails[2]) { 
     case 1: //GIF 
      imagegif($virtual_image, $filepath); 
      imagedestroy($virtual_image); 
      break;   
     case 2: //JPEG 
      imagejpeg($virtual_image, $filepath, 100); 
      imagedestroy($virtual_image); 
      break;   
     case 3: //PNG 
      imagepng($virtual_image, $filepath, 6); 
      imagedestroy($virtual_image); 
      break;   
     case 6: //WBMP 
      imagewbmp($virtual_image, $filepath); 
      imagedestroy($virtual_image); 
      break;   
     default: 
      imagedestroy($virtual_image); 
      break;   
    } 
} 

用法
MakeThumbnail($pic2['tmp_name'], $pic2Path, $extension, 800, 600); //<- resize original 
MakeThumbnail($pic2['tmp_name'], $pic2ThumbPath, $extension, 150, 100); //<- make a thumbnail 

圖片現在用比率調整大小的工作原理與上面的更新功能。我現在發現的問題是,如果原始寬度比我想要指定的更小,圖像將被調整(製作得更大)。

我所追求的是一個尺寸不會大於$maxWidth的尺寸調整過的圖像,絕不會比$maxHeight更高,但仍保持原始圖像的寬高比。

+0

你想在這裏做什麼? '$ newwidth = $ newheight = min($ size,max($ width,$ height)); ' –

+0

我的意思是你用我引用的那行代碼試圖做什麼;我認爲這是你麻煩的根源。 –

+0

我發現你在前面的問題中繼承的原始代碼:http://stackoverflow.com/questions/20707086/image-resize-on-upload-issue。它在保持寬高比時已經調整大小,只需將MaxHeight值設置爲0即可。 –

回答

1

你要這個,我認爲:

$width = imagesx($source_image); 
    $height = imagesy($source_image);  
    // new code to check if image is too small 
    if ($width <= $maxWidth || height <= $maxHeight) { //because we need to check either, and not both 
    // image is too small; don't resize 
     $doResize = false; 
    } else { 
     $doResize = true; 
    } 

然後在你的imagecopyresized地說:

if ($doResize) { 
    imagecopyresized($virtual_image,$source_image,0,0,0,0,$maxWidth,$desired_height,$width,$height);   
} 
+0

我想你可以看到我的代碼正在嘗試,對嗎?檢查錯誤並進行調整。可能是var case? –

0

明白了。

/* Get some details about the image */ 
    $srcDetails = getimagesize($inputFile); 
    switch ($srcDetails[2]) { 
     case 1: //GIF 
      $source_image = imagecreatefromgif($inputFile); 
      break;   
     case 2: //JPEG 
      $source_image = imagecreatefromjpeg($inputFile); 
      break;   
     case 3: //PNG 
      $source_image = imagecreatefrompng($inputFile); 
      break;   
     case 6: //WBMP 
      $source_image = imagecreatefromwbmp($inputFile); 
      break;   
     default: 
      break; 
    }  
    /* Original Dimensions */ 
    $width = imagesx($source_image); 
    $height = imagesy($source_image); 
    $target_ratio = $maxWidth/$maxHeight; 
    $original_ratio = $width/$height; 
    /* find the "desired height" of this thumbnail, relative to the desired width, and vice-versa */ 
    if(($maxWidth <= $width) || $maxHeight <= $height){ // <-- needed to change this to orelse 
     if($target_ratio == $original_ratio){ 
      $desired_height = $maxHeight; 
      $desired_width = $maxWidth; 
     }elseif($target_ratio > $original_ratio){ 
      $desired_height = $maxHeight; 
      $desired_width = $width * ($maxHeight/$height); 
     }elseif($target_ratio < $original_ratio){ 
      $desired_height = $height * ($maxWidth/$width); 
      $desired_width = $maxWidth; 
     } 
    }else{ 
     $desired_height = $maxHeight; 
     $desired_width = $maxWidth; 
    } 
    $virtual_image = imagecreatetruecolor($desired_width, $desired_height);  
    imagecopyresized($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);  
    /* create the physical thumbnail image to its destination */ 
    switch ($srcDetails[2]) { 
     case 1: //GIF 
      imagegif($virtual_image, $filepath); 
      imagedestroy($virtual_image); 
      break;   
     case 2: //JPEG 
      imagejpeg($virtual_image, $filepath, 100); 
      imagedestroy($virtual_image); 
      break;   
     case 3: //PNG 
      imagepng($virtual_image, $filepath, 6); 
      imagedestroy($virtual_image); 
      break;   
     case 6: //WBMP 
      imagewbmp($virtual_image, $filepath); 
      imagedestroy($virtual_image); 
      break;   
     default: 
      imagedestroy($virtual_image); 
      break;   
    }