开发者

PHP GD Image Problem

I am trying to code something where I can overlay images on top of each other and have them output as 1 image. Also I have it where I can shift one of the images 20 px over and 10 px down. I have everything done, and set up how I want it, but the only issue is when I shift one of the images to the right it shows a black spot on the left, how do I get it all to be transparent. Even when the image is shifted?

<?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 in开发者_如何学编程to 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);

?>


Try something like this:

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

This is off my cakePHP image minipulation class. This perserves PNG and GIF transparency. I wouldnt suggest copying this exact but just to put yu on the right track

PS: Note that $this->image and $new_image are the loaded image.

It is loaded like:

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 IF youre going for a watermark.. this is what i did:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜