2011-08-27 128 views
0

我想編碼的東西,我可以將圖像疊加在一起,並將它們輸出爲1圖像。此外,我也可以將其中的一幅圖像向下移動20像素和10像素。我已經做好了一切,並設定了我想要的方式,但唯一的問題是,當我將其中一幅圖像向右移動時,它顯示了左側的黑點,我如何才能將它全部透明化。即使圖像被移位? ?PHP GD Image問題

<?php 
    $layers = array(); 
    $shiftx = array(); 
    $shifty = array(); 
    $wid = array(); 
    $hei = array(); 
    $layers[] = imagecreatefrompng("egg.png"); 
    $shiftx[] = "0"; 
    $shifty[] = "0"; 
    $wid[] = "75"; 
    $hei[] = "75"; 
    $layers[] = imagecreatefrompng("fedora.png");//Top Layer 
    $shiftx[] = "-20"; 
    $shifty[] = "0"; 
    $wid[] = "56"; 
    $hei[] = "56"; 
    $image = imagecreatetruecolor(100, 100); 
    $transparent = imagecolorallocatealpha($image, 255, 255, 255, 127); 
    imagefill($image, 0, 0, $transparent); 
//Will merge the layers into one image// 
    for ($i = 0; $i < count($layers); $i++) 
    { 
     $y=$shifty[$i]; 
     $x=$shiftx[$i]; 
     $w=$wid[$i]-$shiftx[$i]; 
     $h=$hei[$i]-$shifty[$i]; 
     imagecopy($image, $layers[$i], 0, 0, $x, $y, $w, $h); 
    } 
//Everything is now done, except for the image output. We will do this now.// 
    header('Content-type: image/png'); 
    imagealphablending($image, false); 
    imagesavealpha($image, true); 
    imagepng($image); 
    imagedestroy($image); 

>

回答

0

嘗試是這樣的:

if(($this->image_type == 1) || ($this->image_type == 3)){ 
    imagealphablending($new_image, false); 
    imagesavealpha($new_image,true); 
    $transparent = imagecolorallocatealpha($new_image, 255, 255, 255, 127); 
    imagefilledrectangle($new_image, 0, 0, $width, $height, $transparent); 
} 

這關我的CakePHP的圖像minipulation類。這保持了PNG和GIF的透明度。我不會建議複製這個確切的,但只是爲了把正確的軌道上

PS:請注意$ this-> image和$ new_image是加載的圖像。

它被裝載,如:

function load($filename) { 
     $image_info = getimagesize($filename); 
     $this->image_type = $image_info[2]; 
     if($this->image_type == IMAGETYPE_JPEG) { 
      $this->image_type == 2; 
      $this->image_ext = 'jpg'; 
      $this->image = imagecreatefromjpeg($filename); 
     } elseif($this->image_type == IMAGETYPE_GIF) { 
      $this->image_type == 3; 
      $this->image_ext = 'gif'; 
      $this->image = imagecreatefromgif($filename); 
     } elseif($this->image_type == IMAGETYPE_PNG) { 
      $this->image_type = 1; 
      $this->image_ext = 'png'; 
      $this->image = imagecreatefrompng($filename); 
     } 
    } 

PSS如果youre去一個水印..這是我做過什麼:

function waterMark($image, $horizontal='right', $vertical='bottom'){ 
     // Get extension 
     $extension = $this->__getExtension($image); 

     // Image info 
     list($width, $height, $type) = getimagesize($image); 

     // Get image resource 
     $watermark = $this->__getImageResource($image, $extension); 

     // Resource width and height 
     $image_width = imagesx($this->image); 
     $image_height = imagesy($this->image); 

     // Calculate positions 
     $position_x = $horizontal; 
     $position_y = $vertical; 
     switch($position_x){ 
      case "left": 
       $position_x = 0; 
       break; 
      case "center": 
       $position_x = ceil($image_width/2) - floor($width/2); 
       break; 
      case "right": 
       $position_x = $image_width - $width; 
       break; 
     } 
     switch($position_y){ 
      case "top": 
       $position_y = 0; 
       break; 
      case "center": 
       $position_y = ceil($image_height/2) - floor($height/2); 
       break; 
      case "bottom": 
       $position_y = $image_height - $height; 
       break; 
     } 

     $extension = $this->__getExtension($image); 
     if($extension == "png"){ 
      $this->__imagecopymergePng($this->image, $watermark, $position_x, $position_y, 0, 0, $width, $height, 100); 
     }else{ 
      imagecopymerge($this->image, $watermark, $position_x, $position_y, 0, 0, $width, $height, 100); 
     } 

     // Destroy watermark 
     imagedestroy($watermark); 
    } 
+0

我遇到的問題是,例如,當我把在-20移位(這樣它會向右移20),並將圖像更改爲+20,它顯示爲左側黑色。前20個像素是黑色的,但其餘的都是透明的。爲什麼會這樣做? – John

+0

你需要imagealphablend()我認爲 –