首页 > 作文

php调整gif动画图片尺寸示例代码分享

更新时间:2023-04-06 11:55:42 阅读: 评论:0

类的使用demo:

复制代码 代码如下:

<?php

require_once “roucheng.php”;

$gr = new gifresizer;

$gr->temp_dir = “keleyi”;

$gr->resize(“keleyi.gif”,”keleyi_resized.gif”,500,500);

?>

类的源代码,保存为roucheng.php文件:

复制代码 代码如下:

<?

/**

*

* resizes animated gif files

*

* ///important note: the script needs a temporary directory where all the frames should be extracted.

* create a directory with a 777 permission level and write the path into $temp_dir variable below.

*

* default directory is “frames”.

*/

class gifresizer {

public $temp_dir = “frames”;

private $pointer = 0;

private $index = 0;

private $globaldata = array();

private $imagedata = array();

private $imageinfo = array();

private $handle = 0;

private $orgvars = array();

private $encdata = array();

private $pardfiles = array();

private $originalwidth = 0;

private $originalheight = 0;

private $wr,$hr;

private $props = array();

private $decoding = fal;

/**

* public part of the class

*

* @orgfile – original file path

* @newfile – new filename with path

* @width – desired image width

* @height – desired image height

*/

function resize($orgfile,$newfile,$width,$height){

$this->decode($orgfile);

$this->wr=$width/$this->originalwidth;

$this->hr=$height/$this->originalheight;

$this->resizeframes();

$this-&g可复制领导力t;encode($newfile,$width,$height);

$this->clearframes();

}

/**

* gif decoder function.

* pars the gif animation into single frames.

*/

private function decode($filename){

$this->decoding = true;

$this->clearvariables();

$this->loadfile($filename);

$this->get_gif_header();

$this->get_graphics_extension(0);

$this->get_application_经验分享data();

$this->get_application_data();

$this->get_image_block(0);

$this->get_graphics_extension(1);

$this->get_comment_data();

$this->get_application_data();

$this->get_image_block(1);

while(!$this->checkbyte(0x3b) && !$this->checkeof()){

$this->get_comment_data(1);

$this->get_graphics_extension(2);

$this->get_image_block(2);

}

$this->writeframes(time());

$this->clofile();

$this->decoding = fal;

}

/**

* gif encoder function.

* combines the pard gif frames into one single animation.

*/

private function encode($new_filename,$newwidth,$newheight){

$mystring = “”;

$this->pointer = 0;

$this->imagedata = array();

$this->imageinfo = array();

$this->handle = 0;

$this->index=0;

$k=0;

foreach($this->pardfiles as $imagepart){

$this->loadfile($imagepart);

$this->get_gif_header();

$this->get_application_data();

$this->get_comment_data();

$this->get_graphics_extension(0);

$this->get_image_block(0);

//get transparent color index and color

if(ist($this->encdata[$this->index-1]))

$gxdata = $this->encdata[$this->index-1][“graphicxtension”];

el

$gxdata = null;

$ghdata = $this->imageinfo[“gifheader”];

$trcolor = “”;

$hastransparency=($gxdata[3]&&1==1);

if($hastransparency){

$trcx = ord($gxdata[6]);

$trcolor = substr($ghdata,13+$trcx*3,3);

}

//global color table to image data;

$this->transfercolortable($this->imageinfo[“gifheader”],$this->imagedata[$this->index-1][“imagedata”]);

$imageblock = &$this->imagedata[$this->index-1][“imagedata”];

//if transparency exists transfer transparency index

if($hastransparency){

$haslocalcolortable = ((ord($imageblock[9])&128)==128);

if($haslocalcolortable){

//local table exists. determine boundaries and look for it.

$tablesize=(pow(2,(ord($imageblock[9])&7)+1)*3)+10;

$this->orgvars[$this->index-1][“transparent_color_index”] =

((strrpos(substr($this->imagedata[$this->index-1][“imagedata”],0,$tablesize),$trcolor)-10)/3);

}el{

//local table doesnt exist, look at the global one.

$tablesize=(pow(2,(ord($gxdata[10])&7)+1)*3)+10;

$this->orgvars[$this->index-1][“transparent_color_index”] =

((strrpos(substr($ghdata,0,$tablesize),$trcolor)-10)/3);

}

}

//apply original delay time,transparent index and disposal values to graphics extension

if(!$this->imagedata[$this->index-1][“graphicxtension”]) $this->imagedata[$this->index-1][“graphicxtension”] = chr(0x21).chr(0xf9).chr(0x04).chr(0x00).chr(0x00).chr(0x00).chr(0x00).chr(0x00);

$imagedata = &$this->imagedata[$this->index-1][“graphicxtension”];

$imagedata[3] = chr((ord($imagedata[3]) & 0xe3) | ($this->orgvars[$this->index-1][“disposal_method”] << 2));

$imagedata[4] = chr(($this->orgvars[$this->index-1][“delay_time”] % 256));

$imagedata[5] = chr(floor($this->orgvars[$this->index-1][“delay_time”] / 256));

if($hastransparency){

$imagedata[6] = chr($this->orgvars[$this->index-1][“transparent_color_index”]);

}

$imagedata[3] = chr(ord($imagedata[3])|$hastransparency);

//apply calculated left and top offt

$imageblock[1] = chr(round(($this->orgvars[$this->index-1][“offt_left”]*$this->wr) % 256));

$imageblock[2] = chr(floor(($this->orgvars[$this->index-1][“offt_left”]*$this->wr) / 256));

$imageblock[3] = chr(round(($this->orgvars[$this->index-1][“offt_top”]*$this->hr) % 256));

$imageblock[4] = chr(floor(($this->orgvars[$this->index-1][“offt_top”]*$this->hr) / 256));

if($this->index==1){

if(!ist($this->imageinfo[“applicationdata”]) || !$this->imageinfo[“applicationdata”])

$this->imageinfo[“applicationdata”]=chr(0x21).chr(0xff).chr(0x0b).”netscape2.0″.chr(0x03).chr(0x01).chr(0x00).chr(0x00).chr(0x00);

if(!ist($this->imageinfo[“commentdata”]) || !$this->imageinfo[“commentdata”])

$this->imageinfo[“commentdata”] = chr(0x21).chr(0xfe).chr(0x10).”phpgifresizer1.0″.chr(0);

$mystring .= $this->orgvars[“gifheader”]. $this->imageinfo[“applicationdata”].$this->imageinfo[“commentdata”];

if(ist($this->orgvars[“hasgx_type_0”]) && $this->orgvars[“hasgx_type_0”]) $mystring .= $this->globaldata[“graphicxtension_0”];

if(ist($this->orgvars[“hasgx_type_1”]) && $this->orgvars[“hasgx_type_1”]) $mystring .= $this->globaldata[“graphicxtension”];

}

$mystring .= $imagedata . $imageblock;

$k++;

$this->clofile();

}

$mystring .= chr(0x3b);

//applying new width & height to gif header

$mystring[6] = chr($newwidth % 256);

$mystring[7] = chr(floor($newwidth / 256));

$mystring[8] = chr($newheight % 256);

$mystring[9] = chr(floor($newheight / 256));

$mystring[11]= $this->orgvars[“background_color”];

//if(file_exists($new_filename)){unlink($new_filename);}

file_put_contents($new_filename,$mystring);

}

/**

* variable ret function

* if a instan画眉谷ce is ud multiple times, it’s needed. trust me.

*/

private function clearvariables(){

$this->pointer = 0;

$this->index = 0;

$this->imagedata = array();

$this->imageinfo = array();

$this->handle = 0;

$this->pardfiles = array();

}

/**

* clear frames function

* for deleting the frames after encoding.

*/

private function clearframes(){

foreach($this->pardfiles as $temp_frame){

unlink($temp_frame);

}

}

/**

* frame writer

* writes the gif frames into files.

*/

private function writeframes($prepend){

for($i=0;$i<sizeof($this->imagedata);$i++){

file_put_contents($this->temp_dir.”/frame_”.$prepend.”_”.str_pad($i,2,”0″,str_pad_left).”.gif”,$this->imageinfo[“gifheader”].$this->imagedata[$i][“graphicxtension”].$this->imagedata[$i][“imagedata”].chr(0x3b));

$this->pardfiles[]=$this->temp_dir.”/frame_”.$prepend.”_”.str_pad($i,2,”0″,str_pad_left).”.gif”;

}

}

/**

* color palette transfer device

* transferring global color table (gct) from frames into local color tables in animation.

*/

private function transfercolortable($src,&$dst){

//src is gif header,dst is image data block

//if global color table exists,transfer it

if((ord($src[10])&128)==128){

//gif header global color table length

$ghctl = pow(2,$this->readbits(ord($src[10]),5,3)+1)*3;

//cut global color table from gif header

$ghgct = substr($src,13,$ghctl);

//check image block color table length

if((ord($dst[9])&128)==128){

//image data contains color table. skip.

}el{

//image data needs a color table.

//get last color table length so we can truncate the dummy color table

$idctl = pow(2,$this->readbits(ord($dst[9]),5,3)+1)*3;

//t color table flag and length

$dst[9] = chr(ord($dst[9]) | (0x80 | (log($ghctl/3,2)-1)));

//inject color table

$dst = substr($dst,0,10).$ghgct.substr($dst,-1*strlen($dst)+10);

}

}el{

//global color table doesn’t exist. skip.

}

}

/**

* gif parr functions.

* below functions are the main structure parr components.

*/

private function get_gif_header(){

$this->p_forward(10);

if($this->readbits(($mybyte=$this->readbyte_int()),0,1)==1){

$this->p_forward(2);

$this->p_forward(pow(2,$this->readbits($mybyte,5,3)+1)*3);

}el{

$this->p_forward(2);

}

$this->imageinfo[“gifheader”]=$this->datapart(0,$this->pointer);

if($this->decoding){

$this->orgvars[“gifheader”]=$this->imageinfo[“gifheader”];

$this->originalwidth = ord($this->orgvars[“gifheader”][7])*256+ord($this->orgvars[“gifheader”][6]);

$this->originalheight = ord($this->orgvars[“gifheader”][9])*256+ord($this->orgvars[“gifheader”][8]);

$this->orgvars[“background_color”]=$this->orgvars[“gifheader”][11];

}

}

//——————————————————-

private function get_application_data(){

$startdata = $this->readbyte(2);

if($startdata==chr(0x21).chr(0xff)){

$start = $this->pointer – 2;

$this->p_forward($this->readbyte_int());

$this->read_data_stream($this->readbyte_int());

$this->imageinfo[“applicationdata”] = $this->datapart($start,$this->pointer-$start);

}el{

$this->p_rewind(2);

}

}

//——————————————————-

private function get_comment_data(){

$startdata = $this->readbyte(2);

if($startdata==chr(0x21).chr(0xfe)){

$start = $this->pointer – 2;

$this->read_data_stream($this->readbyte_int());

$this->imageinfo[“commentdata”] = $this->datapart($start,$this->pointer-$start);

}el{

$this->p_rewind(2);

}

}

//——————————————————-

private function get_graphics_extension($type){

$startdata = $this->readbyte(2);

if($startdata==chr(0x21).chr(0xf9)){

$start = $this->pointer – 2;

$this->p_forward($this->readbyte_int());

$this->p_forward(1);

if($type==2){

$this->imagedata[$this->index][“graphicxtension”] = $this->datapart($start,$this->pointer-$start);

}el if($type==1){

$this->orgvars[“hasgx_type_1”] = 1;

$this->globaldata[“graphicxtension”] = $this->datapart($start,$this->pointer-$start);

}el if($type==0 && $this->decoding==fal){

$this->encdata[$this->index][“graphicxtension”] = $this->datapart($start,$this->pointer-$start);

}el if($type==0 && $this->decoding==true){

$this->orgvars[“hasgx_type_0”] = 1;

$this->globaldata[“graphicxtension_0”] = $this->datapart($start,$this->pointer-$start);

}

}el{

$this->p_rewind(2);

}

}

//——————————————————-

private function get_image_block($type){

if($this->checkbyte(0x2c)){

$start = $this->pointer;

$this->p_forward(9);

if($this->readbits(($mybyte=$this->readbyte_int()),0,1)==1){

$this->p_forward(pow(2,$this->readbits($mybyte,5,3)+1)*3);

}

$this->p_forward(1);

$this->read_data_stream($this->readbyte_int());

$this->imagedata[$this->index][“imagedata”] = $this->datapart($start,$this->pointer-$start);

if($type==0){

$this->orgvars[“hasgx_type_0”] = 0;

if(ist($this->globaldata[“graphicxtension_0”]))

$this->imagedata[$this->index][“graphicxtension”]=$this->globaldata[“graphicxtension_0”];

el

$this->imagedata[$this->index][“graphicxtension”]=null;

unt($this->globaldata[“graphicxtension_0”]);

}elif($type==1){

if(ist($this->orgvars[“hasgx_type_1”]) && $this->orgvars[“hasgx_type_1”]==1){

$this->orgvars[“hasgx_type_1”] = 0;

$this->imagedata[$this->index][“graphicxtension”]=$this->globaldata[“graphicxtension”];

unt($this->globaldata[“graphicxtension”]);

}el{

$this->orgvars[“hasgx_type_0”] = 0;

$this->imagedata[$this->index][“graphicxtension”]=$this->globaldata[“graphicxtension_0”];

unt($this->globaldata[“graphicxtension_0”]);

}

}

$this->par_image_data();

$this->index++;

}

}

//——————————————————-

private function par_image_data(){

$this->imagedata[$this->index][“disposal_method”] = $this->get_imagedata_bit(“ext”,3,3,3);

$this->imagedata[$this->index][“ur_input_flag”] = $this->get_imagedata_bit(“ext”,3,6,1);

$this->imagedata[$this->index][“transparent_color_flag”] = $this->get_imagedata_bit(“ext”,3,7,1);

$this->imagedata[$this->index][“delay_time”] = $this->dualbyteval($this->get_imagedata_byte(“ext”,4,2));

$this->imagedata[$this->index][“transparent_color_index”] = ord($this->get_imagedata_byte(“ext”,6,1));

$this->imagedata[$this->index][“offt_left”] = $this->dualbyteval($this->get_imagedata_byte(“dat”,1,2));

$this->imagedata[$this->index][“offt_top”] = $this->dualbyteval($this->get_imagedata_byte(“dat”,3,2));

$this->imagedata[$this->index][“width”] = $this->dualbyteval($this->get_imagedata_byte(“dat”,5,2));

$this->imagedata[$this->index][“height”] = $this->dualbyteval($this->get_imagedata_byte(“dat”,7,2));

$this->imagedata[$this->index][“local_color_table_flag”] = $this->get_imagedata_bit(“dat”,9,0,1);

$this->imagedata[$this->index][“interlace_flag”] = $this->get_imagedata_bit(“dat”,9,1,1);

$this->imagedata[$this->index][“sort_flag”] = $this->get_imagedata_bit(“dat”,9,2,1);

$this->imagedata[$this->index][“color_table_size”] = pow(2,$this->get_imagedata_bit(“dat”,9,5,3)+1)*3;

$this->imagedata[$this->index][“color_table”] = substr($this->imagedata[$this->index][“imagedata”],10,$this->imagedata[$this->index][“color_table_size”]);

$this->imagedata[$this->index][“lzw_code_size”] = ord($this->get_imagedata_byte(“dat”,10,1));

if($this->decoding){

$this->orgvars[$this->index][“transparent_color_flag”] = $this->imagedata[$this->index][“transparent_color_flag”];

$this->orgvars[$this->index][“transparent_color_index”] = $this->imagedata[$this->index][“transparent_color_index”];

$this->orgvars[$this->index][“delay_time”] = $this->imagedata[$this->index][“delay_time”];

$this->orgvars[$this->index][“disposal_method”] = $this->imagedata[$this->index][“disposal_method”];

$this->orgvars[$this->index][“offt_left”] = $this->imagedata[$this->index][“offt_left”];

$this->orgvars[$this->index][“offt_top”] = $this->imagedata[$this->index][“offt_top”];

}

}

//——————————————————-

private function get_imagedata_byte($type,$start,$length){

if($type==”ext”)

return substr($this->imagedata[$this->index][“graphicxtension”],$start,$length);

elif($type==”dat”)

return substr($this->imagedata[$this->index][“imagedata”],$start,$length);

}

//——————————————————-

private function get_imagedata_bit($type,$byteindex,$bitstart,$bitlength){

if($type==”ext”)

return $this->readbits(ord(substr($this->imagedata[$this->index][“graphicxtension”],$byteindex,1)),$bitstart,$bitlength);

elif($type==”dat”)

return $this->readbits(ord(substr($this->imagedata[$this->index][“imagedata”],$byteindex,1)),$bitstart,$bitlength);

}

//——————————————————-

private function dualbyteval($s){

$i = ord($s[1])*256 + ord($s[0]);

return $i;

}

//———— helper functions ———————

private function read_data_stream($first_length){

$this->p_forward($first_length);

$length=$this->readbyte_int();

if($length!=0) {

while($length!=0){

$this->p_forward($length);

$length=$this->readbyte_int();

}

}

return true;

}

//——————————————————-

private function loadfile($filename){

$this->handle = fop植树节画画作品图片en($filename,”rb”);

$this->pointer = 0;

}

//——————————————————-

private function clofile(){

fclo($this->handle);

$this->handle=0;

}

//——————————————————-

private function readbyte($byte_count){

$data = fread($this->handle,$byte_count);

$this->pointer += $byte_count;

return $data;

}

//——————————————————-

private function readbyte_int(){

$data = fread($this->handle,1);

$this->pointer++;

return ord($data);

}

//——————————————————-

private function readbits($byte,$start,$length){

$bin = str_pad(decbin($byte),8,”0″,str_pad_left);

$data = substr($bin,$start,$length);

return bindec($data);

}

//——————————————————-

private function p_rewind($length){

$this->pointer-=$length;

fek($this->handle,$this->pointer);

}

//——————————————————-

private function p_forward($length){

$this->pointer+=$length;

fek($this->handle,$this->pointer);

}

//——————————————————-

private function datapart($start,$length){

fek($this->handle,$start);

$data = fread($this->handle,$length);

fek($this->handle,$this->pointer);

return $data;

}

//——————————————————-

private function checkbyte($byte){

if(fgetc($this->handle)==chr($byte)){

fek($this->handle,$this->pointer);

return true;

}el{

fek($this->handle,$this->pointer);

return fal;

}

}

//——————————————————-

private function checkeof(){

if(fgetc(稀释$this->handle)===fal){

return true;

}el{

fek($this->handle,$this->pointer);

return fal;

}

}

//——————————————————-

/**

* debug functions. keleyi.com

* pars the gif animation into single frames.

*/

private function debug($string){

echo “<pre>”;

for($i=0;$i<strlen($string);$i++){

echo str_pad(dechex(ord($string[$i])),2,”0″,str_pad_left). ” “;

}

echo “</pre>”;

}

//——————————————————-

private function debuglen($var,$len){

echo “<pre>”;

for($i=0;$i<$len;$i++){

echo str_pad(dechex(ord($var[$i])),2,”0″,str_pad_left). ” “;

}

echo “</pre>”;

}

//——————————————————-

private function debugstream($length){

$this->debug($this->datapart($this->pointer,$length));

}

//——————————————————-

/**

* gd resizer device

* resizes the animation frames

*/

private function resizeframes(){

$k=0;

foreach($this->pardfiles as $img){

$src = imagecreatefromgif($img);

$sw = $this->imagedata[$k][“width”];

$sh = $this->imagedata[$k][“height”];

$nw = round($sw * $this->wr);

$nh = round($sh * $this->hr);

$sprite = imagecreatetruecolor($nw,$nh);

$trans = imagecolortransparent($sprite);

imagealphablending($sprite, fal);

imagesavealpha($sprite, true);

imagepalettecopy($sprite,$src);

imagefill($sprite,0,0,imagecolortransparent($src));

imagecolortransparent($sprite,imagecolortransparent($src));

imagecopyresized($sprite,$src,0,0,0,0,$nw,$nh,$sw,$sh);

imagegif($sprite,$img);

imagedestroy($sprite);

imagedestroy($src);

$k++;

}

}

}

?>

本文发布于:2023-04-06 11:55:40,感谢您对本站的认可!

本文链接:https://www.wtabcd.cn/fanwen/zuowen/df0ec90a28500b00be5c7aaff94ba3a8.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

本文word下载地址:php调整gif动画图片尺寸示例代码分享.doc

本文 PDF 下载地址:php调整gif动画图片尺寸示例代码分享.pdf

标签:代码   植树节   画眉   源代码
相关文章
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2022 Comsenz Inc.Powered by © 专利检索| 网站地图