2015-09-01 30 views
0

我創建了一個拍攝圖像的函數,將它們調整爲至少將一個維度完美地放到畫布上,最後剪出多餘的圖像內容。在PHP中調整大小/裁剪圖像會導致一側有黑色邊框的圖像

過程:

  1. 乘坐640x400圖像
  2. 縮放圖像配合到450x450畫布(現在圖像是450x281)
  3. 作物出多餘的數據,如果任何

I」的問題當前遇到的圖像涉及的圖像對於新創建的縮略圖來說太小(例如上面示例中提供的圖像)。我預計輸出要麼是白色背景(JPG),要麼是透明背景(PNGs)。然而,儘管我盡了最大努力,這往往是造成混亂:

Broken image

。可以看出,圖像已經有一套透明/白色背景上的黑色背景。我完全沉迷於如何糾正這個缺陷。

我的代碼的副本:

function create_thumbnail($path, $saveto, $width, $height) { 
    ini_set('memory_limit', '128M'); //Unlocking more memory for download 
    $info = getimagesize($path); 
    $rate = $info[0]/$info[1]; 

    // Determine image size/position 
    if ($info[0] < $width || $info[1] < $height) { 
     // Image is too small 
     if ($info[0] < $width && $info[1] < $height) { 
      // Both width and height too small 
      $nw = $info[0]; 
      $nh = $info[1]; 
     } else if ($info[0] < $width) { 
      // Width is too small 
      $nw = ($info[0]*$height)/$info[1]; 
      $nh = $height; 
     } else if ($info[1] < $height) { 
      // Height is too small 
      $nw = $width; 
      $nh = ($info[1]*$width)/$info[0]; 
     } 
    } else { 
     // Image fits 
     if (($width/$height) > $rate) { 
      $nw = $width; 
      $nh = $width/$rate; 
     } else { 
      $nw = $height*$rate; 
      $nh = $height; 
     } 
    } 

    $nw = round($nw); 
    $nh = round($nh); 

    $x_mid = round($nw/2); 
    $y_mid = round($nh/2); 

    switch($info[2]) { 
     case IMAGETYPE_PNG : 
      $src = imagecreatefrompng($path); 
      break; 
     case IMAGETYPE_JPEG : 
      $src = imagecreatefromjpeg($path); 
      break; 
     case IMAGETYPE_GIF : 
      $src = imagecreatefromgif($path); 
      break; 
     default : 
      return false; 
    } 

    // Create image 
    $proc = imagecreatetruecolor($nw, $nh); 
    $clr = imagecolorallocate($proc, 255, 255, 255); 
    imagefill($proc, 0, 0, $clr); 
    imagecopyresampled($proc, $src, 0, 0, 0, 0, $nw, $nh, $info[0], $info[1]); 

    $thmb = imagecreatetruecolor($width, $height); 
    $clr = imagecolorallocate($thmb, 255, 255, 255); 
    imagefill($thmb, 0, 0, $clr); 
    imagecopyresampled($thmb, $proc, 0, 0, ($x_mid-($width/2)), ($y_mid-($height/2)), $width, $height, $width, $height); 

    if ($info[2] == IMAGETYPE_PNG || $info[2] == IMAGETYPE_GIF) { 
     $trnprt_idx = imagecolortransparent($src); 

     if ($trnprt_idx >= 0) { 
      // Attempt to forcefully correct transparencies using original image's color index 
      $trnprt_clr = imagecolorsforindex($src, $trnprt_idx); 
      $trnprt_idx = imagecolorallocate($thmb, $trnprt_clr['red'], $trnprt_clr['green'], $trnprt_clr['blue']); 
      imagefill($thmb, 0, 0, $trnprt_idx); 
      imagecolortransparent($thmb, $trnprt_idx); 

      imagealphablending($thmb, false); 
      imagesavealpha($thmb, true); 
     } else if ($info[2] == IMAGETYPE_PNG) { 
      // Attempt to forcefully correct transparencies by shutting down blending 
      $clr = imagecolorallocatealpha($thmb, 0, 0, 0, 127); 
      imagefill($thmb, 0, 0, $clr); 
      imagealphablending($thmb, false); 
      imagesavealpha($thmb, true); 
     } 
    } 

    switch($info[2]) { 
     case IMAGETYPE_PNG : 
      imagepng($thmb, $saveto); 
      break; 
     case IMAGETYPE_JPEG : 
      imagejpeg($thmb, $saveto, 100); 
      break; 
     case IMAGETYPE_GIF : 
      imagegif($thmb, $saveto); 
      break; 
     default : 
      return false; 
    } 

    return true; 
} //End of create_thumbnail() 

我試圖糾正透明/着色(如可見在我的代碼),但隻影響圖像的一側。我所嘗試過的所有東西都會導致一面有透明/白色背景,或者兩面完全是黑色。

回答

2

經過很長時間的嘗試弄清究竟究竟是怎麼回事,我發現瞭解決方案。

的問題是在這裏:

$proc = imagecreatetruecolor($nw, $nh); 
$clr = imagecolorallocate($proc, 255, 255, 255); 
imagefill($proc, 0, 0, $clr); 
imagecopyresampled($proc, $src, 0, 0, 0, 0, $nw, $nh, $info[0], $info[1]); 

$thmb = imagecreatetruecolor($width, $height); 
$clr = imagecolorallocate($thmb, 255, 255, 255); 
imagefill($thmb, 0, 0, $clr); 
imagecopyresampled($thmb, $proc, 0, 0, ($x_mid-($width/2)), ($y_mid-($height/2)), $width, $height, $width, $height); 

新創建的圖像$proc沒有轉移它的透明度到$thmb當我從一個到另一個做imagecopyresampled。我找到的解決方案是跳過創建/使用$thmb,或者先將$proc保存爲png/gif,然後將該保存的圖像用於imagecopyresample

相關問題