Foros del Web » Programando para Internet » PHP »

[SOLUCIONADO] imagecolortransparent

Estas en el tema de imagecolortransparent en el foro de PHP en Foros del Web. Estoy usando ImageManipulator.php y cuando cargo una imagen en ".png" el fondo transparente lo convierte en Negro , la idea es que carge la imagen ...
  #1 (permalink)  
Antiguo 01/03/2014, 13:55
 
Fecha de Ingreso: noviembre-2004
Ubicación: NULL
Mensajes: 652
Antigüedad: 19 años, 5 meses
Puntos: 6
imagecolortransparent

Estoy usando ImageManipulator.php y cuando cargo una imagen en ".png" el fondo transparente lo convierte en Negro, la idea es que carge la imagen y mantenga el fondo original en este caso transparente, aqui el codigo:

Código PHP:
<?php
class ImageManipulator
{
    
/**
     * @var int
     */
    
protected $width;

    
/**
     * @var int
     */
    
protected $height;

    
/**
     * @var resource
     */
    
protected $image;

    
/**
     * Image manipulator constructor
     * 
     * @param string $file OPTIONAL Path to image file or image data as string
     * @return void
     */
    
protected $transparent true//the watermark transparency
    
    
public function __construct($file null)
    {
        if (
null !== $file) {
            if (
is_file($file)) {
                
$this->setImageFile($file);
            } else {
                
$this->setImageString($file);
            }
        }
    }

    
/**
     * Set image resource from file
     * 
     * @param string $file Path to image file
     * @return ImageManipulator for a fluent interface
     * @throws InvalidArgumentException
     */
    
public function setImageFile($file)
    {
        if (!(
is_readable($file) && is_file($file))) {
            throw new 
InvalidArgumentException("Image file $file is not readable");
        }

        if (
is_resource($this->image)) {
            
imagedestroy($this->image);
        }

        list (
$this->width$this->height$type) = getimagesize($file);

        switch (
$type) {
            case 
IMAGETYPE_GIF  :
                
$this->image imagecreatefromgif($file);
                break;
            case 
IMAGETYPE_JPEG :
                
$this->image imagecreatefromjpeg($file);
                break;
            case 
IMAGETYPE_PNG  :
                
$this->image imagecreatefrompng($file);
                break;
            default             :
                throw new 
InvalidArgumentException("Image type $type not supported");
        }

        return 
$this;
    }
    
    
/**
     * Set image resource from string data
     * 
     * @param string $data
     * @return ImageManipulator for a fluent interface
     * @throws RuntimeException
     */
    
public function setImageString($data)
    {
        if (
is_resource($this->image)) {
            
imagedestroy($this->image);
        }

        if (!
$this->image imagecreatefromstring($data)) {
            throw new 
RuntimeException('Cannot create image from data string');
        }
        
$this->width imagesx($this->image);
        
$this->height imagesy($this->image);
        return 
$this;
    }

    
/**
     * Resamples the current image
     *
     * @param int  $width                New width
     * @param int  $height               New height
     * @param bool $constrainProportions Constrain current image proportions when resizing
     * @return ImageManipulator for a fluent interface
     * @throws RuntimeException
     */
    
public function resample($width$height$constrainProportions true)
    {
        if (!
is_resource($this->image)) {
            throw new 
RuntimeException('No image set');
        }
        if (
$constrainProportions) {
            if (
$this->height >= $this->width) {
                
$width  round($height $this->height $this->width);
            } else {
                
$height round($width $this->width $this->height);
            }
        }
        
$temp imagecreatetruecolor($width$height);
        
imagecopyresampled($temp$this->image0000$width$height$this->width$this->height);
        return 
$this->_replace($temp);
    }
    
    
/**
     * Enlarge canvas
     * 
     * @param int   $width  Canvas width
     * @param int   $height Canvas height
     * @param array $rgb    RGB colour values
     * @param int   $xpos   X-Position of image in new canvas, null for centre
     * @param int   $ypos   Y-Position of image in new canvas, null for centre
     * @return ImageManipulator for a fluent interface
     * @throws RuntimeException
     */
    
public function enlargeCanvas($width$height, array $rgb = array(), $xpos null$ypos null)
    {
        if (!
is_resource($this->image)) {
            throw new 
RuntimeException('No image set');
        }

        
        
$width max($width$this->width);
        
$height max($height$this->height);
        
        
$temp imagecreatetruecolor($width$height);
        if (
count($rgb) == 3) {
            
$bg imagecolorallocate($temp$rgb[0], $rgb[1], $rgb[2]);
            
imagefill($temp00$bg);
        }
        
        if (
null === $xpos) {
            
$xpos round(($width $this->width) / 2);
        }
        if (
null === $ypos) {
            
$ypos round(($height $this->height) / 2);
        }
        
        
imagecopy($temp$this->image, (int) $xpos, (int) $ypos00$this->width$this->height);
        return 
$this->_replace($temp);
    }
    
    
/**
     * Crop image
     * 
     * @param int|array $x1 Top left x-coordinate of crop box or array of coordinates
     * @param int       $y1 Top left y-coordinate of crop box
     * @param int       $x2 Bottom right x-coordinate of crop box
     * @param int       $y2 Bottom right y-coordinate of crop box
     * @return ImageManipulator for a fluent interface
     * @throws RuntimeException
     */
    
public function crop($x1$y1 0$x2 0$y2 0)
    {
        if (!
is_resource($this->image)) {
            throw new 
RuntimeException('No image set');
        }
        if (
is_array($x1) && == count($x1)) {
            list(
$x1$y1$x2$y2) = $x1;
        }
        
        
$x1 max($x10);
        
$y1 max($y10);
        
        
$x2 min($x2$this->width);
        
$y2 min($y2$this->height);
        
        
$width $x2 $x1;
        
$height $y2 $y1;
        
        
$temp imagecreatetruecolor($width$height);
        
imagecopy($temp$this->image00$x1$y1$width$height);
        
        return 
$this->_replace($temp);
    }
    
    
/**
     * Replace current image resource with a new one
     * 
     * @param resource $res New image resource
     * @return ImageManipulator for a fluent interface
     * @throws UnexpectedValueException
     */
    
protected function _replace($res)
    {
        if (!
is_resource($res)) {
            throw new 
UnexpectedValueException('Invalid resource');
        }
        if (
is_resource($this->image)) {
            
imagedestroy($this->image);
        }
        
$this->image $res;
        
$this->width imagesx($res);
        
$this->height imagesy($res);
        return 
$this;
    }
    
    
/**
     * Save current image to file
     * 
     * @param string $fileName
     * @return void
     * @throws RuntimeException
     */
    
public function save($fileName$type IMAGETYPE_JPEG)
    {
        
$dir dirname($fileName);
        if (!
is_dir($dir)) {
            if (!
mkdir($dir0755true)) {
                throw new 
RuntimeException('Error creating directory ' $dir);
            }
        }
        
        try {
            switch (
$type) {
                case 
IMAGETYPE_GIF  :
                    if (!
imagegif($this->image$fileName)) {
                        throw new 
RuntimeException;
                    }
                    break;
                case 
IMAGETYPE_PNG  :
                    if (!
imagepng($this->image$fileName)) {
                        throw new 
RuntimeException;
                    }
                    break;
                case 
IMAGETYPE_JPEG :
                default             :
                    if (!
imagejpeg($this->image$fileName95)) {
                        throw new 
RuntimeException;
                    }
            }
        } catch (
Exception $ex) {
            throw new 
RuntimeException('Error saving image file to ' $fileName);
        }
    }

    
/**
     * Returns the GD image resource
     *
     * @return resource
     */
    
public function getResource()
    {
        return 
$this->image;
    }

    
/**
     * Get current image resource width
     *
     * @return int
     */
    
public function getWidth()
    {
        return 
$this->width;
    }

    
/**
     * Get current image height
     *
     * @return int
     */
    
public function getHeight()
    {
        return 
$this->height;
    }
}
Aqui un ejemplo pero donde lo puedo integrar?
Código PHP:
<?php
// Create a 55x30 image
$im imagecreatetruecolor(5530);
$red imagecolorallocate($im25500);
$black imagecolorallocate($im000);

// Make the background transparent
imagecolortransparent($im$black);

// Draw a red rectangle
imagefilledrectangle($im445025$red);

// Save the image
imagepng($im'./imagecolortransparent.png');
imagedestroy($im);
?>
  #2 (permalink)  
Antiguo 01/03/2014, 14:46
 
Fecha de Ingreso: noviembre-2004
Ubicación: NULL
Mensajes: 652
Antigüedad: 19 años, 5 meses
Puntos: 6
Información Respuesta: imagecolortransparent

Gracias a todos, La solucion al problema de transparencia esta solucionado, reemplace el codigo anterior por este.

Este class es muy bueno y facil de usar espero les sea util.

Código PHP:
<?php
class ImageManipulator
{
    
/**
     * @var int
     */
    
protected $width;

    
/**
     * @var int
     */
    
protected $height;

    
/**
     * @var resource
     */
    
protected $image;

    
/**
     * Image manipulator constructor
     * 
     * @param string $file OPTIONAL Path to image file or image data as string
     * @return void
     */
    
protected $transparent true//the watermark transparency
    
    
public function __construct($file null)
    {
        if (
null !== $file) {
            if (
is_file($file)) {
                
$this->setImageFile($file);
            } else {
                
$this->setImageString($file);
            }
        }
    }

    
/**
     * Set image resource from file
     * 
     * @param string $file Path to image file
     * @return ImageManipulator for a fluent interface
     * @throws InvalidArgumentException
     */
    
public function setImageFile($file)
    {
        if (!(
is_readable($file) && is_file($file))) {
            throw new 
InvalidArgumentException("Image file $file is not readable");
        }

        if (
is_resource($this->image)) {
            
imagedestroy($this->image);
        }

        list (
$this->width$this->height$type) = getimagesize($file);

        switch (
$type) {
            case 
IMAGETYPE_GIF  :
                
$this->image imagecreatefromgif($file);
                break;
            case 
IMAGETYPE_JPEG :
                
$this->image imagecreatefromjpeg($file);
                break;
            case 
IMAGETYPE_PNG  :
                
$this->image imagecreatefrompng($file);
                break;
            default             :
                throw new 
InvalidArgumentException("Image type $type not supported");
        }

        return 
$this;
    }
    
    
/**
     * Set image resource from string data
     * 
     * @param string $data
     * @return ImageManipulator for a fluent interface
     * @throws RuntimeException
     */
    
public function setImageString($data)
    {
        if (
is_resource($this->image)) {
            
imagedestroy($this->image);
        }

        if (!
$this->image imagecreatefromstring($data)) {
            throw new 
RuntimeException('Cannot create image from data string');
        }
        
$this->width imagesx($this->image);
        
$this->height imagesy($this->image);
        return 
$this;
    }

    
/**
     * Resamples the current image
     *
     * @param int  $width                New width
     * @param int  $height               New height
     * @param bool $constrainProportions Constrain current image proportions when resizing
     * @return ImageManipulator for a fluent interface
     * @throws RuntimeException
     */
public function resample($width$height$constrainProportions true)
{
    if (!
is_resource($this->image)) {
        throw new 
RuntimeException('No image set');
    }
    if (
$constrainProportions) {
        if (
$this->height >= $this->width) {
            
$width  round($height $this->height $this->width);
        } else {
            
$height round($width $this->width $this->height);
        }
    }

    
$temp imagecreatetruecolor($width$height);

    
// PNG/GIF Transparency
    
imagealphablending($tempfalse);
    
imagesavealpha($temp,true);
    
$transparent imagecolorallocatealpha($temp255255255127);
    
imagefilledrectangle($temp00$width$height$transparent);

    
imagecopyresampled($temp$this->image0000$width$height$this->width$this->height);
    return 
$this->_replace($temp);
}
////


////    
    /**
     * Enlarge canvas
     * 
     * @param int   $width  Canvas width
     * @param int   $height Canvas height
     * @param array $rgb    RGB colour values
     * @param int   $xpos   X-Position of image in new canvas, null for centre
     * @param int   $ypos   Y-Position of image in new canvas, null for centre
     * @return ImageManipulator for a fluent interface
     * @throws RuntimeException
     */
    
public function enlargeCanvas($width$height, array $rgb = array(), $xpos null$ypos null)
    {
        if (!
is_resource($this->image)) {
            throw new 
RuntimeException('No image set');
        }

        
        
$width max($width$this->width);
        
$height max($height$this->height);
        
        
$temp imagecreatetruecolor($width$height);
        if (
count($rgb) == 3) {
            
$bg imagecolorallocate($temp$rgb[0], $rgb[1], $rgb[2]);
            
imagefill($temp00$bg);
        }
        
        if (
null === $xpos) {
            
$xpos round(($width $this->width) / 2);
        }
        if (
null === $ypos) {
            
$ypos round(($height $this->height) / 2);
        }
        
        
imagecopy($temp$this->image, (int) $xpos, (int) $ypos00$this->width$this->height);
        return 
$this->_replace($temp);
    }
    
    
/**
     * Crop image
     * 
     * @param int|array $x1 Top left x-coordinate of crop box or array of coordinates
     * @param int       $y1 Top left y-coordinate of crop box
     * @param int       $x2 Bottom right x-coordinate of crop box
     * @param int       $y2 Bottom right y-coordinate of crop box
     * @return ImageManipulator for a fluent interface
     * @throws RuntimeException
     */
    
public function crop($x1$y1 0$x2 0$y2 0)
    {
        if (!
is_resource($this->image)) {
            throw new 
RuntimeException('No image set');
        }
        if (
is_array($x1) && == count($x1)) {
            list(
$x1$y1$x2$y2) = $x1;
        }
        
        
$x1 max($x10);
        
$y1 max($y10);
        
        
$x2 min($x2$this->width);
        
$y2 min($y2$this->height);
        
        
$width $x2 $x1;
        
$height $y2 $y1;
        
        
$temp imagecreatetruecolor($width$height);
        
imagecopy($temp$this->image00$x1$y1$width$height);
        
        return 
$this->_replace($temp);
    }
    
    
/**
     * Replace current image resource with a new one
     * 
     * @param resource $res New image resource
     * @return ImageManipulator for a fluent interface
     * @throws UnexpectedValueException
     */
    
protected function _replace($res)
    {
        if (!
is_resource($res)) {
            throw new 
UnexpectedValueException('Invalid resource');
        }
        if (
is_resource($this->image)) {
            
imagedestroy($this->image);
        }
        
$this->image $res;
        
$this->width imagesx($res);
        
$this->height imagesy($res);
        return 
$this;
    }
    
    
/**
     * Save current image to file
     * 
     * @param string $fileName
     * @return void
     * @throws RuntimeException
     */
    
public function save($fileName$type IMAGETYPE_PNG)
{
    
$dir dirname($fileName);
    if (!
is_dir($dir)) {
        if (!
mkdir($dir0755true)) {
            throw new 
RuntimeException('Error creating directory ' $dir);
        }
    }

    try {
        switch (
$type) {
            case 
IMAGETYPE_GIF  :
                if (!
imagegif($this->image$fileName)) {
                    throw new 
RuntimeException;
                }
                break;
            case 
IMAGETYPE_PNG  :

                
// PNG Transparency
                
imagealphablending($this->imagefalse);
                
imagesavealpha($this->image,true);

                if (!
imagepng($this->image$fileName)) {
                    throw new 
RuntimeException;
                }
                break;
            case 
IMAGETYPE_JPEG :
            default             :
                if (!
imagejpeg($this->image$fileName95)) {
                    throw new 
RuntimeException;
                }
        }
    } catch (
Exception $ex) {
        throw new 
RuntimeException('Error saving image file to ' $fileName);
    }
}


    
/**
     * Returns the GD image resource
     *
     * @return resource
     */
    
public function getResource()
    {
        return 
$this->image;
    }

    
/**
     * Get current image resource width
     *
     * @return int
     */
    
public function getWidth()
    {
        return 
$this->width;
    }

    
/**
     * Get current image height
     *
     * @return int
     */
    
public function getHeight()
    {
        return 
$this->height;
    }
}

Etiquetas: Ninguno
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 10:17.