2010-11-09 129 views
0

如何在PHP中調整圖像大小以保持縱橫比,透明度(如果文件爲gifpng圖像),然後垂直或水平放置圖像,具體取決於最終尺寸?在PHP中調整圖像大小,保持比例,透明度和居中

+0

操作的哪個方面您有問題嗎?您是否熟悉GD庫? – 2010-11-09 12:51:18

+0

另外:http://stackoverflow.com/search?q=php+resize+image – 2010-11-09 12:51:42

+0

所有這些,我不熟悉GD,因爲我不是一個PHP開發人員 – Mark 2010-11-09 12:53:37

回答

2

有幾個技巧。這裏是我的FUNC:

function im_resize($file_src,$file_dest,$wd,$hd) { 

    if (!file_exists($file_src)) return false; 

    $size = getimagesize($file_src); 
    if ($size === false) return false; 

    if ($size['mime']=='image/pjpeg') $size['mime'] = 'image/jpeg'; 

    $format = strtolower(substr($size['mime'], strpos($size['mime'], '/')+1)); 
    $destformat = strtolower(substr($file_dest, -4)); 
    $icfunc = "imagecreatefrom" . $format; 
    if (!function_exists($icfunc)) return false; 

    $src = $icfunc($file_src); 

    $ws = imagesx($src); 
    $hs = imagesy($src); 

    if ($ws >= $hs) { 
    $hd = ceil(($wd * $hs)/$ws); 
    } 
    else { 
    $wd = ceil(($hd*$ws)/$hs); 
    } 
    if ($ws <= $wd) { 
    $wd = $ws; 
    $hd = $hs; 
    } 
    $wc=($wd * $hs)/$hd; 

    if ($wc<=$ws) { 
    $hc=($wc * $hd)/$wd; 
    } 
    else { 
    $hc=($ws * $hd)/$wd; 
    $wc=($wd * $hc)/$hd; 
    } 

    $dest = imagecreatetruecolor($wd,$hd); 
    switch ($format) { 
    case "png": 
     imagealphablending($dest, false); 
     imagesavealpha($dest, true); 
     $transparent = imagecolorallocatealpha($dest, 255, 255, 255, 127); 
     imagefilledrectangle($dest, 0, 0, $nw, $nh, $transparent); 

     break; 
    case "gif": 
     // integer representation of the color black (rgb: 0,0,0) 
     $background = imagecolorallocate($src, 0, 0, 0); 
     // removing the black from the placeholder 
     imagecolortransparent($src, $background); 

     break; 
    } 

    imagecopyresampled($dest,$src,0,0,($ws-$wc)/2,($hs-$hc)/2, $wd, $hd, $wc, $hc); 

    if (!isset($q)) $q = 100; 
    if ($destformat=='.png') $saved=imagepng($dest,$file_dest); 
    if ($destformat=='.jpg') $saved=imagejpeg($dest,$file_dest,$q); 
    if (!$saved) my_error_log('saving failed'); 

    imagedestroy($dest); 
    imagedestroy($src); 
    @chmod($file_dest, 0666); 

    return true; 
} 
1

如果

(...),然後垂直或水平(...)居中圖像

意味着,要圍繞增加白色或黑色邊框,我不會那樣做。只需保存調整大小的圖像,並使用html/css在瀏覽器中進行居中。

0
// --------------------------------------------------------------------------------- 
// Resize Image 
// --------------------------------------------------------------------------------- 
function ResizeImage($FileName,$SaveFile, $MaxWidth, $MaxHeight = null) { 

    $extension = GetFileExtension($FileName); 

    switch(strtolower($extension)) { 
     case "gif": 
      $objImage = imagecreatefromgif($FileName); 
      break; 
     case "png": 
      $objImage = imagecreatefrompng($FileName); 
      break; 
     default: 
      $objImage = imagecreatefromjpeg($FileName); 
      break; 
    } 

    list($width, $height, $type, $attr) = getimagesize($FileName); 
    $TargetWidth = $width; 
    $TargetHeight = $height; 
    if (!is_null($MaxWidth)) { 
     if ($MaxWidth < $TargetWidth) { 
      $TargetWidth = $MaxWidth; 
      $TargetHeight = round($TargetHeight * $TargetWidth/$width); 
     } 
    } 
    if (!is_null($MaxHeight)) { 
     if ($MaxHeight < $TargetHeight) { 
      $TargetHeight = $MaxHeight; 
      $TargetWidth = round($TargetWidth * $TargetHeight/$height); 
     } 
    } 


    $DestImage = imagecreatetruecolor($TargetWidth, $TargetHeight); 

    // handle transparancy  
    if (($type == IMAGETYPE_GIF) || ($type == IMAGETYPE_PNG)) { 
     $trnprt_indx = imagecolortransparent($objImage); 
     // If we have a specific transparent color 
     if ($trnprt_indx >= 0) { 
      // Get the original image's transparent color's RGB values 
      $trnprt_color = imagecolorsforindex($objImage, $trnprt_indx); 
      // Allocate the same color in the new image resource 
      $trnprt_indx = imagecolorallocate($DestImage, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']); 

      // Completely fill the background of the new image with allocated color. 
      imagefill($DestImage, 0, 0, $trnprt_indx); 

      // Set the background color for new image to transparent 
      imagecolortransparent($DestImage, $trnprt_indx); 
     } elseif ($type == IMAGETYPE_PNG) { 

      // Turn off transparency blending (temporarily) 
      imagealphablending($DestImage, false); 

      // Create a new transparent color for image 
      $color = imagecolorallocatealpha($DestImage, 0, 0, 0, 127); 

      // Completely fill the background of the new image with allocated color. 
      imagefill($DestImage, 0, 0, $color); 

      // Restore transparency blending 
      imagesavealpha($DestImage, true); 
     } 
    } 



    imagecopyresampled($DestImage, $objImage, 0, 0, 0, 0, $TargetWidth, $TargetHeight, $width, $height); 
    switch(strtolower($extension)) { 
     case "gif": 
      imagegif($DestImage, $SaveFile); 
      break; 
     case "png": 
      imagepng($DestImage, $SaveFile,0); 
      break; 
     default: 
      imagejpeg($DestImage,$SaveFile,100); 
      break; 
    } 

} 
// --------------------------------------------------------------------------------- 
// GetFileExtension 
// --------------------------------------------------------------------------------- 
function GetFileExtension($inFileName) { 
    return substr($inFileName, strrpos($inFileName, '.') + 1); 
} 
0

首先使用getimagesize()讀取要調整大小的圖像的圖像大小;

找出你想調整大小的圖像的最大寬度和高度。

找出哪些是您的圖像的最大尺寸,寬度或高度來調整大小。

找出你需要劃分多少,使其在調整大小的寬度或高度範圍內。取這個值,然後分開圖像的另一側進行調整。

現在,您已獲得調整大小的圖像的寬度和高度。只需使用imagecopyresampled()。

0

有一個名爲Image_Transform的PEAR包(http://pear.php.net/package/Image_Transform),我過去曾經使用過。它有很多偉大的功能來操縱圖像,並且它非常易於使用(不必是高級PHP程序員)

相關問題