2011-08-27 90 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()我认为 –