2012-04-08 10 views
0

のサイズを変更します。私のシナリオは次のとおりです:白い背景には、私は、このスクリプトを変更しようとしている画像

イメージのサイズを変更すると、私は望ましくない黒い境界線(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

とはありません。 –

+0

CSS境界線が設定されていません。この黒いボトムボーダーを取得するのはちょっとしたイメージですが、他の人はそうしません。これは - 私が思うものです - なぜなら彼らは黒い境界線を持たないので、下の境界線を取る人は幅と高さの比率が違うからです。 – Fredrik

答えて

0

急いでいるので、正確なコードを書くことはできませんが、私もあなたのようなことをしようとしていました。以下は私のコードです。私はそれをあなたに簡単にするためにコメントを付けました。それはあなたを助けなければなりませんが、確認してください。このコードで

、私は一言で言えば、複数の画像を

を白い背景を持っている傾向イメージ(単一イメージで複数の画像のグループ)を、作成しています、私は白の背景として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サイズのイメージキャンバスを作成し、//そしてここでSTOPする? "============="の前に終わる、あなたの編集されたバージョンコメントに耳を傾ける。 " – Fredrik

関連する問題