PHP GD图像问题

时间:2011-08-27 21:24:55

标签: php gd

我正在尝试编写一些代码,我可以将图像叠加在一起,并将它们输出为1张图像。我也有它可以将图像中的一个图像移动20像素,向下移动10像素。我已经完成了所有工作,并设置了我想要的方式,但唯一的问题是当我将其中一个图像向右移动时,它会在左侧显示一个黑点,如何让它全部变为透明。即使图像移位了?

<?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);

&GT?;

1 个答案:

答案 0 :(得分: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图片小型化课程。这保持了PNG和GIF的透明度。我不建议复制这个,但只是为了把yu放在正确的轨道上

PS:请注意,$ this-&gt;图片和$ 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如果你想要水印......这就是我所做的:

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); 
    }
相关问题