Php 在调整大小时存储动画gif帧

Php 在调整大小时存储动画gif帧,php,Php,我有一个页面,用户可以上传照片,照片将被调整大小,同时上传到服务器。我使用php来调整图像大小,但问题是当用户上传动画GIF时,动画会丢失,当我检查时,我发现帧没有存储在服务器中。我们也可以使用php来存储帧,这样可以显示带有动画的调整大小的图像。此外,如何识别用户上传的图像是否使用php制作动画 class resizeimage{ var $type; var $width; var $height; var $resize_width; var $resize_height; var $c

我有一个页面,用户可以上传照片,照片将被调整大小,同时上传到服务器。我使用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;
}}
类大小调整图像{
var$类型;
var$宽度;
var$高度;
var$resize\u宽度;
var$resize\u高度;
var美元削减;
var$srcimg;
var$dstimg;
var$im;
var$填充;
函数resizeimage($img,$image\u 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_类型)){
$this->type=“gif”;
}
elseif(eregi(“png”,“$image_类型”)| | eregi(“png”,“$image_类型)){
$this->type=“png”;
}
否则{
回显“不支持的文件”;
退出();
}
$this->initi_img();
$this->dst_img($target);
$this->width=imagesx($this->im);
$this->height=imagesy($this->im);
//斯坦加
如果(!$this->调整高度){
$this->resize\u height=$this->resize\u width*($this->height/$this->width);
}
如果(!$this->resize_width){
$this->resize\u width=$this->resize\u height*($this->width/$this->height);
}
$this->newimg($qu);
ImageDestroy($this->im);
}
函数newimg($qu)
{
$resize\u ratio=($this->resize\u width)/($this->resize\u height);
$ratio=($this->width)/($this->height);
如果($ratio>=$resize\u ratio){
$offset_x=0;
$offset\u y=ceil($this->resize\u height-$this->resize\u width/$ratio)/2);
$copy\u width=$this->resize\u width;
$copy\u height=$this->resize\u height/$ratio;
}
否则{
$offset_y=0;
$offset\u x=ceil($this->resize\u width-$this->resize\u height*$ratio)/2);
$copy\u width=$this->resize\u height*$ratio;
$copy\u height=$this->resize\u height;
}
如果(($this->cut)=“1”)
{
如果($ratio>=$resize\u ratio)
{
$newimg=imagecreatetruecolor($this->resize\u width,$this->resize\u height);
imagecopyresampled($newimg,$this->im,0,0,0,$this->resize\u width,$this->resize\u height,($this->height)*$resize\u ratio),$this->height);
ImageJpeg($newimg,$this->dstimg,$qu);
}
如果($ratioresize\u width,$this->resize\u height);
imagecopyresampled($newimg,$this->im,0,0,0,$this->resize\u width,$this->resize\u height,$this->width,($this->width)/$resize\u ratio));
ImageJpeg($newimg,$this->dstimg,$qu);
}
}
其他的
{
如果($this->fill=='black'){
$newimg=imagecreatetruecolor($this->resize\u width,$this->resize\u height);
$bg=imagecolorallocate($newimg,0,0,0);
图像填充($newimg,0,0,$bg);
imagecopyresampled($newimg,$this->im,$offset\u x,$offset\u y,0,0,$copy\u width,$copy\u height,$this->width,$this->height);
ImageJpeg($newimg,$this->dstimg,$qu);
}
elseif($this->fill=='white'){
$newimg=imagecreatetruecolor($this->resize\u width,$this->resize\u height);
$bg=imagecolorallocate($newimg,255,255,255);
图像填充($newimg,0,0,$bg);
imagecopyresampled($newimg,$this->im,$offset\u x,$offset\u y,0,0,$copy\u width,$copy\u height,$this->width,$this->height);
ImageJpeg($newimg,$this->dstimg,$qu);
}
elseif($ratio>=$resize\u ratio)
{
$newimg=imagecreatetruecolor($this->resize\u width,($this->resize\u width)/$ratio);
imagecopyresampled($newimg,$this->im,0,0,0,$this->resize\u width,($this->resize\u width)/$ratio,$this->width,$this->height);
ImageJpeg($newimg,$this->dstimg,$qu);
}
elseif($ratioresize\u height)*$ratio,$this->resize\u height);
imagecopyresampled($newimg,$this->im,0,0,0,($this->resize\u height)*$ratio,$this->resize\u height,$this->width,$this->height);
ImageJpeg($newimg,$this->dstimg,$qu);
}
}
}
函数initi_img()
{
如果($this->type==“jpg”)
{
如果(!$this->im=imagecreatefromjpeg($this->srcimg))
die('这不是一张图片:'。$This->srcimg);
}
如果($this->type==“gif”)
{
如果(!$this->im=imagecreatefromformgif($this->srcimg))
die('这不是一张图片:'。$This->srcimg);
}
如果($this->type==“png”)
{
如果(!$this->im=imagecreatefrompng($this->srcimg))
die('这不是一张图片:'。$This->srcimg);
}
}
功能dst_img($target)
{
$full_length=strlen($this->srcimg);
$type_length=strlen($this->type);
$name\u length=$full\u length-$type\u length;
$name=substr($this->srcimg,0,$name\u length-1);
$this->dstimg=$target.“.”$this->type;
}
函数类型(){
返回$this->type;
}}

请给出建议

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


以下是.

我建议阻止动画GIF上传,或一般情况下阻止所有GIF上传,除非这不是一个选项。