2012-03-11 88 views
0

我有一個頁面,用戶可以在其中上傳照片,上傳到服務器時照片將被調整大小。我使用PHP來調整圖像大小,但問題是當用戶上傳動畫GIF動畫正在失去,當我檢查我發現幀沒有被存儲在服務器。所以有反正我們可以存儲框架,以及使用PHP,以便可以顯示帶動畫的調整大小的圖像。此外,如何識別用戶上傳的圖像是否使用php進行動畫製作。在調整大小時存儲動畫gif的框架

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; 
}} 

請給建議

+0

我會推薦屏蔽動畫GIF上傳,或者一般的所有GIF - 除非這不是一個選項。 – Alex 2012-03-11 10:11:59

回答

1

你可以使用我的一些修改,我認爲寫的GIF圖像縮放類。你需要做的第一個mod是將幀保存在臨時目錄中,因爲我的腳本在調整gif大小後會刪除它們。其次,動畫可能包含處理方法,當您將動畫分割爲幀時,您只會看到圖像相對於第一幀的差異。當你想要真正的(完整)幀時,你應該先合併圖像。

這裏是script url