Skip to content

Instantly share code, notes, and snippets.

@JavaScript-Packer
Last active August 29, 2015 14:27
Show Gist options
  • Select an option

  • Save JavaScript-Packer/21a84b1d7c79eca6ba02 to your computer and use it in GitHub Desktop.

Select an option

Save JavaScript-Packer/21a84b1d7c79eca6ba02 to your computer and use it in GitHub Desktop.
PHP - Split (AKA explode, extract, decode) animated GIF from web URL into frames but show the frames (base64 dataurls) on page instead of saving to hard drive. Most GIFS can be scaled down frames, so we added if($i%2==0) so it will skip every other frame... http://www.whak.ca/animated-gif/
<?php
//get every other frame
$GIF='http://www.whak.ca/animated-gif/www.WHAK.com_logo.gif';
$WHAK=new WHAK_decode(file_get_contents($GIF));
$i=1;
foreach($WHAK->WHAK_frames() as $frame){if($i%2==0){echo '<img src="data:image/gif;base64,'.base64_encode($frame).'">';};$i++;};
Class WHAK_decode{var $WHAK_TR=-1;var $WHAK_TG=-1;var $WHAK_TB=-1;var $WHAK_TI=0;var $WHAK_buffer=Array();var $WHAK_arrays=Array();var $WHAK_delays=Array();var $WHAK_dispos=Array();var $WHAK_stream="";var $WHAK_string="";var $WHAK_bfseek=0;var $WHAK_anloop=0;var $WHAK_screen=Array();var $WHAK_global=Array();var $WHAK_sorted;var $WHAK_colorS;var $WHAK_colorC;var $WHAK_colorF;function WHAK_decode($WHAK_pointer){$this->WHAK_stream=$WHAK_pointer;WHAK_decode::WHAK_get(6);WHAK_decode::WHAK_get(7);$this->WHAK_screen=$this->WHAK_buffer;$this->WHAK_colorF=$this->WHAK_buffer[4]&0x80?1:0;$this->WHAK_sorted=$this->WHAK_buffer[4]&0x08?1:0;$this->WHAK_colorC=$this->WHAK_buffer[4]&0x07;$this->WHAK_colorS=2 << $this->WHAK_colorC;if($this->WHAK_colorF==1){WHAK_decode::WHAK_get(3*$this->WHAK_colorS);$this->WHAK_global=$this->WHAK_buffer;}for($cycle=1;$cycle;){if(WHAK_decode::WHAK_get(1)){switch($this->WHAK_buffer[0]){case 0x21: WHAK_decode::WHAKReadX();break;case 0x2C:WHAK_decode::WHAK_Y();break;case 0x3B:$cycle=0;break;}}else{$cycle=0;}}}function WHAKReadX(){WHAK_decode::WHAK_get(1);if($this->WHAK_buffer[0]==0xff){for(;;){WHAK_decode::WHAK_get(1);if(($u=$this->WHAK_buffer[0])==0x00){break;}WHAK_decode::WHAK_get($u);if($u==0x03){$this->WHAK_anloop=($this->WHAK_buffer[1]|$this->WHAK_buffer[2]<<8);}}}else{for(;;){WHAK_decode::WHAK_get(1);if(($u=$this->WHAK_buffer[0])==0x00){break;}WHAK_decode::WHAK_get($u);if($u==0x04){if($this->WHAK_buffer[4]&0x80){$this->WHAK_dispos[]=($this->WHAK_buffer[0]>>2)-1;}else{$this->WHAK_dispos[]=($this->WHAK_buffer[0]>>2)-0;}$this->WHAK_delays[]=($this->WHAK_buffer[1]|$this->WHAK_buffer[2]<<8);if($this->WHAK_buffer[3]){$this->WHAK_TI=$this->WHAK_buffer[3];}}}}}function WHAK_Y(){$WHAK_screen=Array();WHAK_decode::WHAK_get(9);$WHAK_screen=$this->WHAK_buffer;$WHAK_colorF=$this->WHAK_buffer[8]&0x80?1:0;if($WHAK_colorF){$WHAK_code=$this->WHAK_buffer[8]&0x07;$WHAK_sort=$this->WHAK_buffer[8]&0x20?1:0;}else{$WHAK_code=$this->WHAK_colorC;$WHAK_sort=$this->WHAK_sorted;}$WHAK_size=2 << $WHAK_code;$this->WHAK_screen[4]&=0x70;$this->WHAK_screen[4]|=0x80;$this->WHAK_screen[4]|=$WHAK_code;if($WHAK_sort){$this->WHAK_screen[4]|=0x08;}if($this->WHAK_TI){$this->WHAK_string="GIF89a";}else{$this->WHAK_string="GIF87a";}WHAK_decode::WHAK_add($this->WHAK_screen);if($WHAK_colorF==1){WHAK_decode::WHAK_get(3*$WHAK_size);if($this->WHAK_TI){$this->WHAK_TR=$this->WHAK_buffer[3*$this->WHAK_TI+0];$this->WHAK_TG=$this->WHAK_buffer[3*$this->WHAK_TI+1];$this->WHAK_TB=$this->WHAK_buffer[3*$this->WHAK_TI+2];}WHAK_decode::WHAK_add($this->WHAK_buffer);}else{if($this->WHAK_TI){$this->WHAK_TR=$this->WHAK_global[3*$this->WHAK_TI+0];$this->WHAK_TG=$this->WHAK_global[3*$this->WHAK_TI+1];$this->WHAK_TB=$this->WHAK_global[3*$this->WHAK_TI+2];}WHAK_decode::WHAK_add($this->WHAK_global);}if($this->WHAK_TI){$this->WHAK_string.="!\xF9\x04\x1\x0\x0".chr($this->WHAK_TI)."\x0";}$this->WHAK_string.=chr(0x2C);$WHAK_screen[8]&=0x40;WHAK_decode::WHAK_add($WHAK_screen);WHAK_decode::WHAK_get(1);WHAK_decode::WHAK_add($this->WHAK_buffer);for(;;){WHAK_decode::WHAK_get(1);WHAK_decode::WHAK_add($this->WHAK_buffer);if(($u=$this->WHAK_buffer[0])==0x00){break;}WHAK_decode::WHAK_get($u);WHAK_decode::WHAK_add($this->WHAK_buffer);}$this->WHAK_string.=chr(0x3B);$this->WHAK_arrays[]=$this->WHAK_string;}function WHAK_get($len){$this->WHAK_buffer=Array();for($i=0;$i<$len;$i++){if($this->WHAK_bfseek>strlen($this->WHAK_stream)){return 0;}$this->WHAK_buffer[]=ord($this->WHAK_stream{$this->WHAK_bfseek++});}return 1;}function WHAK_add($bytes){foreach($bytes as $byte){$this->WHAK_string .=chr($byte);}}function WHAK_frames(){return($this->WHAK_arrays);}function WHAK_delay(){return($this->WHAK_delays);}function WHAKGetLoop(){return($this->WHAK_anloop);}function WHAK_disposal(){return($this->WHAK_dispos);}function WHAK_T_R(){return($this->WHAK_TR);}function WHAK_T_G(){return($this->WHAK_TG);}function WHAK_T_B(){return($this->WHAK_TB);}}
// fwrite(fopen("frames/frame0$i.gif" , "wb"), $frame);
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment