Modifiqué algunas partes de la clase para intentar conseguir hacer el rezise al gif animado...
No entiendo muy bien si al separar y volver a unir el gif estoy quitando o añadiendo algo de más que impide la animación o si se debe al guardar la imagen con php. ¿Alguna idea?
-Compruebo si es un gif animado
-Divido los frames
-Los guardo temporalmente
-Los uno en un gif
-Borro los temporales
No hace mucho que empezé a usar php orientado a objetos así que mi código puede dar pena
Código PHP:
<?php
/**
* File: ResizePicture.php
* Author: Simon Jarvis
* Copyright: Simon Jarvis
* Date: Aug-11-06
* Original link: http://www.white-hat-web-design.co.uk/articles/php-image-resizing.php
*
* Modified Date: Jul-05-11
* Gif transparency by iviamontes - http://www.forosdelweb.com/miembros/iviamontes/
*/
class ResizePicture {
private $_image;
private $_image_gif;
private $_head_image_gif;
private $_path;
private $_imageType;
private $_transparent;
public function __construct($fileName=null, $transparent=false)
{
$this->setTransparent($transparent);
if(!is_null($fileName)){
$this->load($fileName);
}
}
public function setTransparent($bool)
{
$this->_transparent = (boolean)$bool;
}
public function load($fileName)
{
$imageInfo = getimagesize($fileName);
$this->_imageType = $imageInfo[2];
$this->_path=$fileName;
if($this->_imageType == IMAGETYPE_JPEG){
$this->_image = imagecreatefromjpeg($fileName);
}
elseif($this->_imageType == IMAGETYPE_GIF){
$this->_image = imagecreatefromgif($fileName);
}
elseif($this->_imageType == IMAGETYPE_PNG){
$this->_image = imagecreatefrompng($fileName);
}
}
public function save($fileName, $compression=75, $permissions=null)
{
if($this->_imageType == IMAGETYPE_JPEG){
imagejpeg($this->_image, $fileName, $compression);
}
elseif($this->_imageType == IMAGETYPE_GIF){
imagegif($this->_image, $fileName);
}
elseif($this->_imageType == IMAGETYPE_PNG){
imagepng($this->_image, $fileName);
}
if(!is_null($permissions)) {
chmod($fileName, $permissions);
}
}
public function output()
{
if($this->_imageType == IMAGETYPE_JPEG){
imagejpeg($this->_image);
}
elseif($this->_imageType == IMAGETYPE_GIF){
imagegif($this->_image);
}
elseif($this->_imageType == IMAGETYPE_PNG){
imagepng($this->_image);
}
}
public function getWidth()
{
return imagesx($this->_image);
}
public function getHeight()
{
return imagesy($this->_image);
}
public function getImageType()
{
switch($this->_imageType){
case IMAGETYPE_JPEG:
$imageType = 'image/jpeg';
break;
case IMAGETYPE_GIF:
$imageType = 'image/gif';
break;
case IMAGETYPE_PNG:
$imageType = 'image/png';
break;
default:
$imageType = null;
}
return $imageType;
}
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);
}
public function resize($width, $height)
{
$newImage = imagecreatetruecolor($width, $height);
if($this->getImageType() == 'image/png' && $this->_transparent === true){
imagealphablending($newImage, false);
imagesavealpha($newImage, true);
imagefilledrectangle($newImage, 0, 0, $width, $height, imagecolorallocatealpha($newImage, 255, 255, 255, 127));
}
elseif($this->getImageType() == 'image/gif' && $this->_transparent === true){
if(($index = imagecolortransparent($this->_image)) != -1){
$colors = imagecolorsforindex($this->_image, $index);
$transparent = imagecolorallocatealpha($newImage, $colors['red'], $colors['green'], $colors['blue'], $colors['alpha']);
imagefill($newImage, 0, 0, $transparent);
imagecolortransparent($newImage, $transparent);
}
}
if($this->is_animated()){ //si es animado
$this->get_frames_gif() ; // extraer cada frame
if(count($this->_image_gif)>1){ // comprobamos que estén bien sacados los frames
foreach ($this->_image_gif as $key => $value){
imagecopyresampled($newImage, $value, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); //redimensionamos cada frame
$this->_image_gif[$key]=$newImage; //guardamos en la misma posición el frame cambiado
}
$this->join_frames_gif(); //volvemos a unir los frames
}
}
else{
imagecopyresampled($newImage, $this->_image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
}
$this->_image = $newImage;
}
public function is_animated()
{
return (bool)preg_match('#\x00\x21\xF9\x04.{4}\x00\x2C#s', file_get_contents($this->_path));
}
public function get_frames_gif()
{
if($this->_imageType == IMAGETYPE_GIF){
if(!($fh = file_get_contents($this->_path))){
break;
}
$reg_exp='#\x00\x21\xF9\x04.{4}\x00\x2C#s';
if(preg_match($reg_exp, $fh, $matches)){
$head=$matches[0];
}
$body= preg_split($reg_exp, $fh);
if(count($body>1) and isset($head)){
$this->_head_image_gif = $body[0];
foreach ($body as $key => $value){
if($key!=0){
$this->_image_gif[]= imagecreatefromstring($body[0].$head.$value);
//guardar frames (Esto lo hace bien)
//if(imagegif ($this->_image, 'probando'.$key.'.gif')) echo '- Se ha creado el frame '.$key.'<br>';
}
}
}
}
}
public function join_frames_gif()
{
//Creamos los frames temporalmente
foreach ($this->_image_gif as $key => $value){
imagegif ($value, 'temp'.$key.'.gif');
}
//Leemos el contenido de cada uno.
foreach ($this->_image_gif as $key => $value){
$name='temp'.$key.'.gif';
if($fh = file_get_contents($name)){
if($key==0){
$frames=$fh;
}
else{ //quitamos los encabezados de todos los frames menos del primero
$frames.=str_replace($this->_head_image_gif,'',$fh);
}
unlink($name);
}
else{
break;
}
}
//echo $frames;
unset($this->_image_gif);
$this->_image = imagecreatefromstring($frames);
}
}
?>