2012-04-08 104 views
0

我試圖修改此腳本:http://net.tutsplus.com/tutorials/php/image-resizing-made-easy-with-php/白色背景調整圖像

我希望得到一個(使用裁剪選項時),白色背景,而不是黑色的,因爲它是現在。我的場景是這樣的:

當我調整圖像的大小時,我會在底部(1px)得到一個我不想要的黑色邊框。我希望這是白色的。

我已經看過這個線程,並試圖執行它的腳本:How do I fill white background while resize image

但它似乎並不工作。這裏是我的調整大小類的代碼:

public function resizeImage($newWidth, $newHeight, $option="auto") 
     { 
      // *** Get optimal width and height - based on $option 
      $optionArray = $this->getDimensions($newWidth, $newHeight, $option); 

      $optimalWidth = $optionArray['optimalWidth']; 
      $optimalHeight = $optionArray['optimalHeight']; 


      // *** Resample - create image canvas of x, y size 
      $this->imageResized = imagecreatetruecolor($optimalWidth, $optimalHeight); 

      // MY EDIT STARTS HERE 
      $backgroundColor = imagecolorallocate($this->imageResized, 255, 255, 255); 
      imagefill($this->imageResized, 0, 0, $backgroundColor); 
      // AND STOPS HERE 

      imagecopyresampled($this->imageResized, $this->image, 0, 0, 0, 0, $optimalWidth, $optimalHeight, $this->width, $this->height); 


      // *** if option is 'crop', then crop too 
      if ($option == 'crop') { 
       $this->crop($optimalWidth, $optimalHeight, $newWidth, $newHeight); 
      } 
     } 

我在做什麼錯,我應該改變什麼?

+0

而且沒有css? – 2012-04-08 12:29:19

+0

沒有設置CSS邊框。這只是一些圖像獲取這個黑底部邊界,其他人沒有。這是 - 我認爲 - 因爲他們(沒有底部黑色邊框)有另一個寬度/高度比例,那麼他們誰得到底部邊框。 – Fredrik 2012-04-08 13:15:20

回答

0

Lil在趕時間,所以不能寫出確切的代碼,但我也試圖做你喜歡的事情。以下是我的代碼。我發表了評論,以方便您。它必須幫助你,但請確認。

在該代碼中,我創建一個趨勢圖像(組在單個圖像的多個圖像的),其具有白色背景的多個圖像

總之,我一個PNG圖像作爲白色背景。

<?php 
session_start(); 
include_once("../dbfunction.php"); 

$tid=0; 
if(isset($_REQUEST) && isset($_REQUEST['tid'])){ 
    $tid=$_REQUEST['tid']; 
}else{ 
    echo "Trend id not defined"; 
} 

//This return SQL result. Multiple rows with images name, x1, y1 as initial position and x2,y2 as width height 
$data=getTrendImages($tid); 

//This is path of background image. For me, background image is pure white png image (480x480) 
$trendsbgurl= OS_PATH."image".DIRECTORY_SEPARATOR."trends_background.png"; 
//Get image in PHP 
$trendsbg = @imagecreatefrompng($trendsbgurl); 

//Loop to get images and put them on background 
for($i=0;$i<count($data);$i++){ 
    $imgname=$data[$i]['image_name']; 
    $imgleft=$data[$i]['position_x1']; 
    $imgtop=$data[$i]['position_y1']; 
    $imgwidth=$data[$i]['position_x2']; 
    $imgheight=$data[$i]['position_y2']; 

    //Path of source image 
    $imgpath=OS_PATH."original".DIRECTORY_SEPARATOR.$imgname; 
    //Explode image name to get file extension. I'd to support png, gif and jpeg images 
    $file=explode(".",$imgname); 
    $ext=$file[1]; 

    //If source file exist 
    if(file_exists($imgpath)){ 
     //Create source image in PHP 
     $image=null; 
     if($ext=='jpg' || $ext=='jpeg'){ 
      $image = @imagecreatefromjpeg($imgpath); 
     }else if($ext=='png'){ 
      $image = @imagecreatefrompng($imgpath); 
     }else if($ext=='gif'){ 
      $image = @imagecreatefromgif($imgpath); 
     }else{ 
      exit; 
     } 
     $colorTransparent = @imagecolorat($image, 0, 0); 
     //GEt source image size 
     $imgsize=getimagesize($imgpath); 
     //Copy source on background 
     imagecopyresampled($trendsbg, $image, $imgleft, $imgtop, 0, 0, $imgwidth,   $imgheight, $imgsize[0], $imgsize[1]); 
    }else{ 
    } 
} 
//I need to save image on disk. You can do whatever you need to do with $trendsbg 
imagejpeg($trendsbg,OS_PATH."trendimages".DIRECTORY_SEPARATOR."t_".$tid.".jpg",100); 
+0

感謝您的回答。但是,這不正是我在評論之間做的事情:// *** Resample - 創建x,y大小的圖像畫布並// //在此停止?我參考了編輯過的版本評論,該評論在「===============」之前結束。 – Fredrik 2012-04-08 10:24:11