调整大小时存储GIF动画帧

时间:2012-03-11 04:05:38

标签: php

我有一个页面,用户可以上传照片,照片将在上传到服务器时调整大小。我正在使用php来调整图像大小,但问题是当用户上传GIF动画时,动画会丢失,当我检查时发现帧没有存储在服务器中。所以无论如何我们也可以使用php来存储帧,这样就可以显示带动画的已调整大小的图像。此外,如何识别用户上传的图像是否使用php进行动画处理。

class resizeimage{
var $type;
var $width;
var $height;
var $resize_width;
var $resize_height;
var $cut;
var $srcimg;
var $dstimg;
var $im;

var $fill;

function resizeimage($img, $image_type, $target, $wid, $hei,$c = 0, $qu = 75, $fill = '')
{
    $this->srcimg = $img;
            $this->fill = $fill;
    $this->resize_width = $wid;
    $this->resize_height = $hei;
    $this->cut = $c;
    if (eregi("jpg",$image_type) || eregi("jpeg",$image_type)) {
        $this->type="jpg";
    }

    elseif (eregi("gif",$image_type)) {
        $this->type="gif";
    }

    elseif (eregi("png",$image_type) || eregi("png",$image_type)) {
        $this->type="png";
    }

    else {
        echo "Not Supported File";
        exit();
    }
    $this->initi_img();
    $this -> dst_img($target);
    $this->width = imagesx($this->im);
    $this->height = imagesy($this->im);
    // add by stan
    if (!$this->resize_height) {
        $this->resize_height = $this->resize_width*($this->height/$this->width);
    }
    if (!$this->resize_width) {
        $this->resize_width = $this->resize_height*($this->width/$this->height);
    }
    $this->newimg($qu);
    ImageDestroy ($this->im);
}
function newimg($qu)
{
    $resize_ratio = ($this->resize_width)/($this->resize_height);
    $ratio = ($this->width)/($this->height);
    if ($ratio>=$resize_ratio) {
        $offset_x = 0;
        $offset_y = ceil(($this->resize_height - $this->resize_width/$ratio)/2);
        $copy_width = $this->resize_width;
        $copy_height = $this->resize_height/$ratio;
    }
    else {
        $offset_y = 0;
        $offset_x = ceil(($this->resize_width-$this->resize_height*$ratio)/2);
        $copy_width = $this->resize_height*$ratio;
        $copy_height = $this->resize_height;
    }
    if(($this->cut)=="1")
    {
        if($ratio>=$resize_ratio)
        {
            $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
            imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width,$this->resize_height, (($this->height)*$resize_ratio), $this->height);
            ImageJpeg ($newimg,$this->dstimg,$qu);
        }
        if($ratio<$resize_ratio)
        {
            $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
            imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, $this->resize_height, $this->width, (($this->width)/$resize_ratio));
            ImageJpeg ($newimg,$this->dstimg,$qu);
        }
    }
    else
    {
        if ($this->fill == 'black') {
            $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
            $bg = imagecolorallocate($newimg, 0, 0, 0);
            imagefill($newimg, 0, 0, $bg);
            imagecopyresampled($newimg, $this->im, $offset_x, $offset_y, 0, 0, $copy_width, $copy_height, $this->width, $this->height);
            ImageJpeg ($newimg,$this->dstimg,$qu);
        }
        elseif ($this->fill == 'white') {
            $newimg = imagecreatetruecolor($this->resize_width,$this->resize_height);
            $bg = imagecolorallocate($newimg, 255, 255, 255);
            imagefill($newimg, 0, 0, $bg);
            imagecopyresampled($newimg, $this->im, $offset_x, $offset_y, 0, 0, $copy_width, $copy_height, $this->width, $this->height);
            ImageJpeg ($newimg,$this->dstimg,$qu);
        }
        elseif($ratio>=$resize_ratio)
        {
            $newimg = imagecreatetruecolor($this->resize_width,($this->resize_width)/$ratio);
            imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, $this->resize_width, ($this->resize_width)/$ratio, $this->width, $this->height);
            ImageJpeg ($newimg,$this->dstimg,$qu);
        }
        elseif($ratio<$resize_ratio)
        {
            $newimg = imagecreatetruecolor(($this->resize_height)*$ratio,$this->resize_height);
            imagecopyresampled($newimg, $this->im, 0, 0, 0, 0, ($this->resize_height)*$ratio, $this->resize_height, $this->width, $this->height);
            ImageJpeg ($newimg,$this->dstimg,$qu);
        }
    }
}
function initi_img()
{
    if($this->type=="jpg")
    {
        if (!$this->im = imagecreatefromjpeg($this->srcimg))
            die('This is not a pic:'.$this->srcimg);
    }
    if($this->type=="gif")
    {
        if (!$this->im = imagecreatefromgif($this->srcimg))
            die('This is not a pic:'.$this->srcimg);
    }
    if($this->type=="png")
    {
        if (!$this->im = imagecreatefrompng($this->srcimg))
            die('This is not a pic:'.$this->srcimg);
    }
}
function dst_img($target)
{
    $full_length  = strlen($this->srcimg);
    $type_length  = strlen($this->type);
    $name_length  = $full_length-$type_length;
    $name         = substr($this->srcimg,0,$name_length-1);
    $this->dstimg = $target.".".$this->type;
}

function type() {
    return $this->type;
}}

请提出建议

1 个答案:

答案 0 :(得分:1)

我可以使用我编写的gif resizer类,并进行一些修改。你需要做的第一个mod是将帧保存在临时目录中,因为我的脚本在调整gif大小后会删除它们。其次,动画可能包含处理方法,当您将动画分割为帧时,您将获得图像相对于第一帧的差异。当你想要真正的(完整的)帧时,你应该首先合并图像。

这是script url