This is an age old problem lol! Your transparent images have a nasty black background! Anyway, if you upload a GIF or PNG you need to set the transparent background color etc. In my image class, if I do a straight save there isn’t a problem, only when I resized. So I put the following in my resize function:

public function resize($width,$height) 
    {
        $new_image = imagecreatetruecolor($width, $height);
        if ( ($this->getImageType() == IMAGETYPE_GIF) || ($this->getImageType()  == IMAGETYPE_PNG) ) 
        {
            $transparency = imagecolortransparent($this->_image);
            if ($transparency >= 0) 
            {
                $transparent_color = imagecolorsforindex($this->_image, $transparency);
                $transparency = imagecolorallocate($new_image, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
                imagefill($new_image, 0, 0, $transparency);
                imagecolortransparent($new_image, $transparency);
            }
            elseif ($this->getImageType() == IMAGETYPE_PNG) 
            {
                imagealphablending($new_image, false);
                imagesavealpha($new_image, true);
                $color = imagecolorallocatealpha($new_image, 0, 0, 0, 127);
                imagefill($new_image, 0, 0, $color);
            }
        }
        imagecopyresampled($new_image, $this->_image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
        $this->_image = $new_image;
    }

That will save your PNG’s or GIF’s and retain their transparency. However, PNG’s will NOT outpuut correctly unless you explicitly tell php to retain alpha channel info:

imagealphablending($this->_image,true);
imagesavealpha($this->_image,true);
imagepng($this->_image);

Here is the full Image class, with the transparency feature:

<?php
/**
* Utility class for processing images
* @author alasdair.shields
* 6 Feb 2012
*/
class ESOS_Entity_Image {

    protected $_image;
    protected $_image_type;


    /** 
     *  @param string $filename
     */
    public function load($filename) 
    {

        $image_info = getimagesize($filename);
        $this->_image_type = $image_info[2];
        if( $this->_image_type == IMAGETYPE_JPEG ) 
        {
            $this->_image = imagecreatefromjpeg($filename);
        } 
        elseif( $this->_image_type == IMAGETYPE_GIF ) 
        {
            $this->_image = imagecreatefromgif($filename);
        } 
        elseif( $this->_image_type == IMAGETYPE_PNG ) 
        {
            $this->_image = imagecreatefrompng($filename);
        }
    }


    /** 
     *  @param string $filename
     *  @param string $image_type
     *  @param int $compression
     *  @param string $permissions
     */
    public function save($filename, $compression=75, $permissions=null) 
    {
        switch($this->getImageType())
        {
            case IMAGETYPE_JPEG:
                imagejpeg($this->_image,$filename,$compression);
                break;
            case IMAGETYPE_GIF:
                imagegif($this->_image,$filename);
                break;
            case IMAGETYPE_PNG:
                imagepng($this->_image,$filename);
                break;
        }
        if( $permissions != null) 
        {
            chmod($filename,$permissions);
        }
    }


    /** 
     *  @param string 
     */
    public function output() 
    {
        switch($this->getImageType())
        {
            case IMAGETYPE_JPEG:
                imagejpeg($this->_image);
                break;
            case IMAGETYPE_GIF:
                imagegif($this->_image);
                break;
            case IMAGETYPE_PNG:
                imagealphablending($this->_image,true);
                imagesavealpha($this->_image,true);
                imagepng($this->_image);
                break;
        }
    }

    public function getWidth() 
    {

        return imagesx($this->_image);
    }

    public function getHeight() 
    {

        return imagesy($this->_image);
    }

    public function resizeToHeight($height) 
    {

        $ratio = $height / $this->getHeight();
        $width = $this->getWidth() * $ratio;
        $this->resize($width,$height);
    }

    public function resizeToWidth($width) 
    {
        $ratio = $width / $this->getWidth();
        $height = $this->getheight() * $ratio;
        $this->resize($width,$height);
    }

    public function scale($scale) 
    {
        $width = $this->getWidth() * $scale/100;
        $height = $this->getheight() * $scale/100;
        $this->resize($width,$height);
    }


    /** 
     *  Now with added Transparency resizing feature
     *  @param int $width
     *  @param int $height
     */
    public function resize($width,$height) 
    {
        
        $new_image = imagecreatetruecolor($width, $height);
        if ( ($this->getImageType() == IMAGETYPE_GIF) || ($this->getImageType()  == IMAGETYPE_PNG) ) 
        {
            // Get transparency color's index number
            $transparency = imagecolortransparent($this->_image);
            
            // Transparent Gifs have index > 0
            // Transparent Png's have index -1
            if ($transparency >= 0) 
            {
                // Get the array of RGB vals for the transparency index
                $transparent_color = imagecolorsforindex($this->_image, $transparency);
                
                // Now allocate the color
                $transparency = imagecolorallocate($new_image, $transparent_color['red'], $transparent_color['green'], $transparent_color['blue']);
                
                // Fill the background with the color
                imagefill($new_image, 0, 0, $transparency);
                
                // And set that color as the transparent one
                imagecolortransparent($new_image, $transparency);
            }
            // Or, if its a PNG
            elseif ($this->getImageType() == IMAGETYPE_PNG) 
            {
                // Set blending mode as false
                imagealphablending($new_image, false);
                
                // Tell it we want to save alpha channel info
                imagesavealpha($new_image, true);
                
                // Set the transparent color
                $color = imagecolorallocatealpha($new_image, 0, 0, 0, 127);
                
                // Fill the image with nothingness
                imagefill($new_image, 0, 0, $color);
            }
        }
        // Now resample the image
        imagecopyresampled($new_image, $this->_image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
        
        // And allocate to $this
        $this->_image = $new_image;
    }

    
    public function getImageType()
    {
        return $this->_image_type;
    }


    

    public function getHeader() 
    {
        if( $this->_image_type == IMAGETYPE_JPEG ) 
        {
            return 'image/jpeg';
        } 
        elseif( $this->_image_type == IMAGETYPE_GIF ) 
        {
            return 'image/gif';
        } 
        elseif( $this->_image_type == IMAGETYPE_PNG ) 
        {
            return 'image/png';
        }
    }


    /** 
     *  Free's up memory
     */
    public function destroy()
    {
        imagedestroy($this->_image);
    }
}
Advertisements