Advertisement
Guest User

Gif Resizer

a guest
Dec 11th, 2012
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 28.49 KB | None | 0 0
  1. <?
  2.     /**
  3.     * Resizes Animated GIF Files
  4.     *
  5.     *   ///IMPORTANT NOTE: The script needs a temporary directory where all the frames should be extracted.
  6.     *   Create a directory with a 777 permission level and write the path into $temp_dir variable below.
  7.     *  
  8.     *   Default directory is "frames".
  9.     */
  10.  
  11.     class gifresizer {
  12.  
  13.         public $temp_dir = "frames";
  14.         private $pointer = 0;
  15.         private $index = 0;
  16.         private $globaldata = array();
  17.         private $imagedata = array();
  18.         private $imageinfo = array();
  19.         private $handle = 0;
  20.         private $orgvars = array();
  21.         private $encdata = array();
  22.         private $parsedfiles = array();
  23.         private $originalwidth = 0;
  24.         private $originalheight = 0;
  25.         private $wr,$hr;
  26.         private $props = array();
  27.         private $decoding = false;
  28.        
  29.         private $resizemethod = 4;
  30.         private $size_data = array();
  31.         /**
  32.         * Public part of the class
  33.         *
  34.         * @orgfile - Original file path
  35.         * @newfile - New filename with path
  36.         * @width   - Desired image width
  37.         * @height  - Desired image height
  38.         */
  39.         function resize($orgfile,$newfile,$width,$height, $resizemethod=4){
  40.             $this->resizemethod = 4;
  41.             $this->size_data['w'] = $width;
  42.             $this->size_data['h'] = $height;
  43.             $this->file = $orgfile;
  44.            
  45.             $this->decode($orgfile);
  46.             $this->wr=$width/$this->originalwidth;
  47.             $this->hr=$height/$this->originalheight;
  48.             $this->resizeframes();
  49.             $this->encode($newfile,$width,$height);
  50.             $this->clearframes();
  51.         }  
  52.         /**
  53.         * GIF Decoder function.
  54.         * Parses the GIF animation into single frames.
  55.         */
  56.         private function decode($filename){
  57.             $this->decoding = true;            
  58.             $this->clearvariables();
  59.             $this->loadfile($filename);
  60.             $this->get_gif_header();
  61.             $this->get_graphics_extension(0);
  62.             $this->get_application_data();
  63.             $this->get_application_data();
  64.             $this->get_image_block(0);
  65.             $this->get_graphics_extension(1);
  66.             $this->get_comment_data();
  67.             $this->get_application_data();
  68.             $this->get_image_block(1);
  69.             while(!$this->checkbyte(0x3b) && !$this->checkEOF()){
  70.                 $this->get_comment_data(1);
  71.                 $this->get_graphics_extension(2);
  72.                 $this->get_image_block(2);
  73.             }
  74.             $this->writeframes(time());    
  75.             $this->closefile();
  76.             $this->decoding = false;
  77.         }
  78.  
  79.         /**
  80.         * GIF Encoder function.
  81.         * Combines the parsed GIF frames into one single animation.
  82.         */
  83.         private function encode($new_filename,$newwidth,$newheight){
  84.             $newwidth = $this->wr;
  85.             $newheight = $this->hr;
  86.             $mystring = "";
  87.             $this->pointer = 0;
  88.             $this->imagedata = array();
  89.             $this->imageinfo = array();
  90.             $this->handle = 0;
  91.             $this->index=0;
  92.  
  93.             $k=0;
  94.             foreach($this->parsedfiles as $imagepart){
  95.                 $this->loadfile($imagepart);
  96.                 $this->get_gif_header();
  97.                 $this->get_application_data();
  98.                 $this->get_comment_data();
  99.                 $this->get_graphics_extension(0);
  100.                 $this->get_image_block(0);
  101.  
  102.                 //get transparent color index and color
  103.                 if(isset($this->encdata[$this->index-1]))
  104.                     $gxdata = $this->encdata[$this->index-1]["graphicsextension"];
  105.                 else
  106.                     $gxdata = null;
  107.                 $ghdata = $this->imageinfo["gifheader"];
  108.                 $trcolor = "";
  109.                 $hastransparency=($gxdata[3]&&1==1);
  110.  
  111.                 if($hastransparency){
  112.                     $trcx = ord($gxdata[6]);
  113.                     $trcolor = substr($ghdata,13+$trcx*3,3);
  114.                 }
  115.  
  116.                 //global color table to image data;
  117.                 $this->transfercolortable($this->imageinfo["gifheader"],$this->imagedata[$this->index-1]["imagedata"]);
  118.  
  119.                 $imageblock = &$this->imagedata[$this->index-1]["imagedata"];
  120.  
  121.                 //if transparency exists transfer transparency index
  122.                 if($hastransparency){
  123.                     $haslocalcolortable = ((ord($imageblock[9])&128)==128);
  124.                     if($haslocalcolortable){
  125.                         //local table exists. determine boundaries and look for it.
  126.                         $tablesize=(pow(2,(ord($imageblock[9])&7)+1)*3)+10;
  127.                         $this->orgvars[$this->index-1]["transparent_color_index"] =
  128.                         ((strrpos(substr($this->imagedata[$this->index-1]["imagedata"],0,$tablesize),$trcolor)-10)/3);     
  129.                     }else{
  130.                         //local table doesnt exist, look at the global one.
  131.                         $tablesize=(pow(2,(ord($gxdata[10])&7)+1)*3)+10;
  132.                         $this->orgvars[$this->index-1]["transparent_color_index"] =
  133.                         ((strrpos(substr($ghdata,0,$tablesize),$trcolor)-10)/3);   
  134.                     }              
  135.                 }
  136.  
  137.                 //apply original delay time,transparent index and disposal values to graphics extension
  138.  
  139.                 if(!$this->imagedata[$this->index-1]["graphicsextension"]) $this->imagedata[$this->index-1]["graphicsextension"] = chr(0x21).chr(0xf9).chr(0x04).chr(0x00).chr(0x00).chr(0x00).chr(0x00).chr(0x00);
  140.  
  141.                 $imagedata = &$this->imagedata[$this->index-1]["graphicsextension"];
  142.  
  143.                 $imagedata[3] = chr((ord($imagedata[3]) & 0xE3) | ($this->orgvars[$this->index-1]["disposal_method"] << 2));
  144.                 $imagedata[4] = chr(($this->orgvars[$this->index-1]["delay_time"] % 256));
  145.                 $imagedata[5] = chr(floor($this->orgvars[$this->index-1]["delay_time"] / 256));
  146.                 if($hastransparency){
  147.                     $imagedata[6] = chr($this->orgvars[$this->index-1]["transparent_color_index"]);
  148.                 }
  149.                 $imagedata[3] = chr(ord($imagedata[3])|$hastransparency);
  150.  
  151.                 //apply calculated left and top offset
  152.                 $imageblock[1] = chr(round(($this->orgvars[$this->index-1]["offset_left"]*$this->wr) % 256));
  153.                 $imageblock[2] = chr(floor(($this->orgvars[$this->index-1]["offset_left"]*$this->wr) / 256));
  154.                 $imageblock[3] = chr(round(($this->orgvars[$this->index-1]["offset_top"]*$this->hr) % 256));
  155.                 $imageblock[4] = chr(floor(($this->orgvars[$this->index-1]["offset_top"]*$this->hr) / 256));           
  156.  
  157.                 if($this->index==1){
  158.                     if(!isset($this->imageinfo["applicationdata"]) || !$this->imageinfo["applicationdata"])
  159.                         $this->imageinfo["applicationdata"]=chr(0x21).chr(0xff).chr(0x0b)."NETSCAPE2.0".chr(0x03).chr(0x01).chr(0x00).chr(0x00).chr(0x00);
  160.                     if(!isset($this->imageinfo["commentdata"]) || !$this->imageinfo["commentdata"])
  161.                         $this->imageinfo["commentdata"] = chr(0x21).chr(0xfe).chr(0x10)."PHPGIFRESIZER1.0".chr(0);
  162.                     $mystring .= $this->orgvars["gifheader"]. $this->imageinfo["applicationdata"].$this->imageinfo["commentdata"];
  163.                     if(isset($this->orgvars["hasgx_type_0"]) && $this->orgvars["hasgx_type_0"]) $mystring .= $this->globaldata["graphicsextension_0"];
  164.                     if(isset($this->orgvars["hasgx_type_1"]) && $this->orgvars["hasgx_type_1"]) $mystring .= $this->globaldata["graphicsextension"];
  165.                 }
  166.  
  167.                 $mystring .= $imagedata . $imageblock;
  168.                 $k++;
  169.                 $this->closefile();
  170.             }
  171.  
  172.             $mystring .= chr(0x3b);
  173.  
  174.             //applying new width & height to gif header
  175.             $mystring[6] = chr($newwidth % 256);
  176.             $mystring[7] = chr(floor($newwidth / 256));
  177.             $mystring[8] = chr($newheight % 256);
  178.             $mystring[9] = chr(floor($newheight / 256));
  179.             $mystring[11]= $this->orgvars["background_color"];
  180.             //if(file_exists($new_filename)){unlink($new_filename);}
  181.             file_put_contents($new_filename,$mystring);
  182.         }
  183.  
  184.         /**
  185.         * Variable Reset function
  186.         * If a instance is used multiple times, it's needed. Trust me.
  187.         */
  188.         private function clearvariables(){
  189.             $this->pointer = 0;
  190.             $this->index = 0;
  191.             $this->imagedata = array();
  192.             $this->imageinfo = array();            
  193.             $this->handle = 0;
  194.             $this->parsedfiles = array();
  195.         }
  196.  
  197.         /**
  198.         * Clear Frames function
  199.         * For deleting the frames after encoding.
  200.         */
  201.         private function clearframes(){
  202.             foreach($this->parsedfiles as $temp_frame){
  203.                 unlink($temp_frame);
  204.             }
  205.         }
  206.  
  207.         /**
  208.         * Frame Writer
  209.         * Writes the GIF frames into files.
  210.         */
  211.         private function writeframes($prepend){
  212.             for($i=0;$i<sizeof($this->imagedata);$i++){
  213.                 file_put_contents($this->temp_dir."/frame_".$prepend."_".str_pad($i,2,"0",STR_PAD_LEFT).".gif",$this->imageinfo["gifheader"].$this->imagedata[$i]["graphicsextension"].$this->imagedata[$i]["imagedata"].chr(0x3b));
  214.                 $this->parsedfiles[]=$this->temp_dir."/frame_".$prepend."_".str_pad($i,2,"0",STR_PAD_LEFT).".gif";
  215.             }
  216.         }
  217.  
  218.         /**
  219.         * Color Palette Transfer Device
  220.         * Transferring Global Color Table (GCT) from frames into Local Color Tables in animation.
  221.         */
  222.         private function transfercolortable($src,&$dst){
  223.             //src is gif header,dst is image data block
  224.             //if global color table exists,transfer it
  225.             if((ord($src[10])&128)==128){
  226.                 //Gif Header Global Color Table Length
  227.                 $ghctl = pow(2,$this->readbits(ord($src[10]),5,3)+1)*3;
  228.                 //cut global color table from gif header
  229.                 $ghgct = substr($src,13,$ghctl);
  230.                 //check image block color table length
  231.                 if((ord($dst[9])&128)==128){
  232.                     //Image data contains color table. skip.
  233.                 }else{
  234.                     //Image data needs a color table.
  235.                     //get last color table length so we can truncate the dummy color table
  236.                     $idctl = pow(2,$this->readbits(ord($dst[9]),5,3)+1)*3;
  237.                     //set color table flag and length  
  238.                     $dst[9] = chr(ord($dst[9]) | (0x80 | (log($ghctl/3,2)-1)));
  239.                     //inject color table
  240.                     $dst = substr($dst,0,10).$ghgct.substr($dst,-1*strlen($dst)+10);
  241.                 }
  242.             }else{
  243.                 //global color table doesn't exist. skip.
  244.             }
  245.         }
  246.  
  247.         /**
  248.         * GIF Parser Functions.
  249.         * Below functions are the main structure parser components.
  250.         */
  251.         private function get_gif_header(){
  252.             $this->p_forward(10);
  253.             if($this->readbits(($mybyte=$this->readbyte_int()),0,1)==1){
  254.                 $this->p_forward(2);
  255.                 $this->p_forward(pow(2,$this->readbits($mybyte,5,3)+1)*3);
  256.             }else{
  257.                 $this->p_forward(2);
  258.             }
  259.  
  260.             $this->imageinfo["gifheader"]=$this->datapart(0,$this->pointer);
  261.             if($this->decoding){
  262.                 $this->orgvars["gifheader"]=$this->imageinfo["gifheader"];
  263.                 $this->originalwidth = ord($this->orgvars["gifheader"][7])*256+ord($this->orgvars["gifheader"][6]);
  264.                 $this->originalheight = ord($this->orgvars["gifheader"][9])*256+ord($this->orgvars["gifheader"][8]);
  265.                 $this->orgvars["background_color"]=$this->orgvars["gifheader"][11];
  266.             }
  267.  
  268.         }
  269.         //-------------------------------------------------------
  270.         private function get_application_data(){
  271.             $startdata = $this->readbyte(2);
  272.             if($startdata==chr(0x21).chr(0xff)){
  273.                 $start = $this->pointer - 2;
  274.                 $this->p_forward($this->readbyte_int());
  275.                 $this->read_data_stream($this->readbyte_int());
  276.                 $this->imageinfo["applicationdata"] = $this->datapart($start,$this->pointer-$start);
  277.             }else{
  278.                 $this->p_rewind(2);
  279.             }
  280.         }
  281.         //-------------------------------------------------------
  282.         private function get_comment_data(){
  283.             $startdata = $this->readbyte(2);
  284.             if($startdata==chr(0x21).chr(0xfe)){
  285.                 $start = $this->pointer - 2;
  286.                 $this->read_data_stream($this->readbyte_int());
  287.                 $this->imageinfo["commentdata"] = $this->datapart($start,$this->pointer-$start);
  288.             }else{
  289.                 $this->p_rewind(2);
  290.             }
  291.         }
  292.         //-------------------------------------------------------
  293.         private function get_graphics_extension($type){
  294.             $startdata = $this->readbyte(2);
  295.             if($startdata==chr(0x21).chr(0xf9)){
  296.                 $start = $this->pointer - 2;
  297.                 $this->p_forward($this->readbyte_int());
  298.                 $this->p_forward(1);
  299.                 if($type==2){
  300.                     $this->imagedata[$this->index]["graphicsextension"] = $this->datapart($start,$this->pointer-$start);
  301.                 }else if($type==1){
  302.                     $this->orgvars["hasgx_type_1"] = 1;
  303.                     $this->globaldata["graphicsextension"] = $this->datapart($start,$this->pointer-$start);
  304.                 }else if($type==0 && $this->decoding==false){
  305.                     $this->encdata[$this->index]["graphicsextension"] = $this->datapart($start,$this->pointer-$start);
  306.                 }else if($type==0 && $this->decoding==true){
  307.                     $this->orgvars["hasgx_type_0"] = 1;
  308.                     $this->globaldata["graphicsextension_0"] = $this->datapart($start,$this->pointer-$start);
  309.                 }
  310.             }else{
  311.                 $this->p_rewind(2);
  312.             }
  313.         }
  314.         //-------------------------------------------------------
  315.         private function get_image_block($type){
  316.             if($this->checkbyte(0x2c)){
  317.                 $start = $this->pointer;
  318.                 $this->p_forward(9);
  319.                 if($this->readbits(($mybyte=$this->readbyte_int()),0,1)==1){
  320.                     $this->p_forward(pow(2,$this->readbits($mybyte,5,3)+1)*3);
  321.                 }
  322.                 $this->p_forward(1);
  323.                 $this->read_data_stream($this->readbyte_int());
  324.                 $this->imagedata[$this->index]["imagedata"] = $this->datapart($start,$this->pointer-$start);
  325.  
  326.                 if($type==0){
  327.                     $this->orgvars["hasgx_type_0"] = 0;
  328.                     if(isset($this->globaldata["graphicsextension_0"]))
  329.                         $this->imagedata[$this->index]["graphicsextension"]=$this->globaldata["graphicsextension_0"];
  330.                     else
  331.                         $this->imagedata[$this->index]["graphicsextension"]=null;
  332.                     unset($this->globaldata["graphicsextension_0"]);
  333.                 }elseif($type==1){
  334.                     if(isset($this->orgvars["hasgx_type_1"]) && $this->orgvars["hasgx_type_1"]==1){
  335.                         $this->orgvars["hasgx_type_1"] = 0;
  336.                         $this->imagedata[$this->index]["graphicsextension"]=$this->globaldata["graphicsextension"];
  337.                         unset($this->globaldata["graphicsextension"]);
  338.                     }else{
  339.                         $this->orgvars["hasgx_type_0"] = 0;
  340.                         $this->imagedata[$this->index]["graphicsextension"]=$this->globaldata["graphicsextension_0"];
  341.                         unset($this->globaldata["graphicsextension_0"]);
  342.                     }
  343.                 }
  344.  
  345.                 $this->parse_image_data();
  346.                 $this->index++;
  347.  
  348.             }
  349.         }
  350.         //-------------------------------------------------------
  351.         private function parse_image_data(){
  352.             $this->imagedata[$this->index]["disposal_method"] = $this->get_imagedata_bit("ext",3,3,3);
  353.             $this->imagedata[$this->index]["user_input_flag"] = $this->get_imagedata_bit("ext",3,6,1);
  354.             $this->imagedata[$this->index]["transparent_color_flag"] = $this->get_imagedata_bit("ext",3,7,1);
  355.             $this->imagedata[$this->index]["delay_time"] = $this->dualbyteval($this->get_imagedata_byte("ext",4,2));
  356.             $this->imagedata[$this->index]["transparent_color_index"] = ord($this->get_imagedata_byte("ext",6,1));
  357.             $this->imagedata[$this->index]["offset_left"] = $this->dualbyteval($this->get_imagedata_byte("dat",1,2));
  358.             $this->imagedata[$this->index]["offset_top"] = $this->dualbyteval($this->get_imagedata_byte("dat",3,2));
  359.             $this->imagedata[$this->index]["width"] = $this->dualbyteval($this->get_imagedata_byte("dat",5,2));
  360.             $this->imagedata[$this->index]["height"] = $this->dualbyteval($this->get_imagedata_byte("dat",7,2));
  361.             $this->imagedata[$this->index]["local_color_table_flag"] = $this->get_imagedata_bit("dat",9,0,1);
  362.             $this->imagedata[$this->index]["interlace_flag"] = $this->get_imagedata_bit("dat",9,1,1);
  363.             $this->imagedata[$this->index]["sort_flag"] = $this->get_imagedata_bit("dat",9,2,1);
  364.             $this->imagedata[$this->index]["color_table_size"] = pow(2,$this->get_imagedata_bit("dat",9,5,3)+1)*3;
  365.             $this->imagedata[$this->index]["color_table"] = substr($this->imagedata[$this->index]["imagedata"],10,$this->imagedata[$this->index]["color_table_size"]);
  366.             $this->imagedata[$this->index]["lzw_code_size"] = ord($this->get_imagedata_byte("dat",10,1));
  367.             if($this->decoding){
  368.                 $this->orgvars[$this->index]["transparent_color_flag"] = $this->imagedata[$this->index]["transparent_color_flag"];
  369.                 $this->orgvars[$this->index]["transparent_color_index"] = $this->imagedata[$this->index]["transparent_color_index"];
  370.                 $this->orgvars[$this->index]["delay_time"] = $this->imagedata[$this->index]["delay_time"];
  371.                 $this->orgvars[$this->index]["disposal_method"] = $this->imagedata[$this->index]["disposal_method"];
  372.                 $this->orgvars[$this->index]["offset_left"] = $this->imagedata[$this->index]["offset_left"];
  373.                 $this->orgvars[$this->index]["offset_top"] = $this->imagedata[$this->index]["offset_top"];
  374.             }
  375.         }
  376.         //-------------------------------------------------------
  377.         private function get_imagedata_byte($type,$start,$length){
  378.             if($type=="ext")
  379.                 return substr($this->imagedata[$this->index]["graphicsextension"],$start,$length);
  380.             elseif($type=="dat")
  381.                 return substr($this->imagedata[$this->index]["imagedata"],$start,$length);
  382.         }
  383.         //-------------------------------------------------------
  384.         private function get_imagedata_bit($type,$byteindex,$bitstart,$bitlength){
  385.             if($type=="ext")
  386.                 return $this->readbits(ord(substr($this->imagedata[$this->index]["graphicsextension"],$byteindex,1)),$bitstart,$bitlength);
  387.             elseif($type=="dat")
  388.                 return $this->readbits(ord(substr($this->imagedata[$this->index]["imagedata"],$byteindex,1)),$bitstart,$bitlength);
  389.         }
  390.         //-------------------------------------------------------
  391.         private function dualbyteval($s){
  392.             $i = ord($s[1])*256 + ord($s[0]);
  393.             return $i;
  394.         }
  395.         //------------   Helper Functions ---------------------
  396.         private function read_data_stream($first_length){
  397.             $this->p_forward($first_length);
  398.             $length=$this->readbyte_int();
  399.             if($length!=0) {
  400.                 while($length!=0){
  401.                     $this->p_forward($length);
  402.                     $length=$this->readbyte_int();
  403.                 }
  404.             }
  405.             return true;
  406.         }
  407.         //-------------------------------------------------------
  408.         private function loadfile($filename){
  409.             $this->handle = fopen($filename,"rb");
  410.             $this->pointer = 0;
  411.         }
  412.         //-------------------------------------------------------
  413.         private function closefile(){
  414.             fclose($this->handle);
  415.             $this->handle=0;
  416.         }
  417.         //-------------------------------------------------------
  418.         private function readbyte($byte_count){
  419.             $data = fread($this->handle,$byte_count);
  420.             $this->pointer += $byte_count;
  421.             return $data;
  422.         }
  423.         //-------------------------------------------------------
  424.         private function readbyte_int(){
  425.             $data = fread($this->handle,1);
  426.             $this->pointer++;
  427.             return ord($data);
  428.         }
  429.         //-------------------------------------------------------
  430.         private function readbits($byte,$start,$length){
  431.             $bin = str_pad(decbin($byte),8,"0",STR_PAD_LEFT);
  432.             $data = substr($bin,$start,$length);
  433.             return bindec($data);
  434.         }
  435.         //-------------------------------------------------------
  436.         private function p_rewind($length){
  437.             $this->pointer-=$length;
  438.             fseek($this->handle,$this->pointer);
  439.         }
  440.         //-------------------------------------------------------
  441.         private function p_forward($length){
  442.             $this->pointer+=$length;
  443.             fseek($this->handle,$this->pointer);
  444.         }
  445.         //-------------------------------------------------------
  446.         private function datapart($start,$length){
  447.             fseek($this->handle,$start);
  448.             $data = fread($this->handle,$length);
  449.             fseek($this->handle,$this->pointer);
  450.             return $data;
  451.         }
  452.         //-------------------------------------------------------
  453.         private function checkbyte($byte){
  454.             if(fgetc($this->handle)==chr($byte)){
  455.                 fseek($this->handle,$this->pointer);
  456.                 return true;
  457.             }else{
  458.                 fseek($this->handle,$this->pointer);
  459.                 return false;
  460.             }
  461.         }  
  462.         //-------------------------------------------------------
  463.         private function checkEOF(){
  464.             if(fgetc($this->handle)===false){
  465.                 return true;
  466.             }else{
  467.                 fseek($this->handle,$this->pointer);
  468.                 return false;
  469.             }
  470.         }  
  471.         //-------------------------------------------------------
  472.         /**
  473.         * Debug Functions.
  474.         * Parses the GIF animation into single frames.
  475.         */
  476.         private function debug($string){
  477.             echo "<pre>";
  478.             for($i=0;$i<strlen($string);$i++){
  479.                 echo str_pad(dechex(ord($string[$i])),2,"0",STR_PAD_LEFT). " ";
  480.             }
  481.             echo "</pre>";
  482.         }
  483.         //-------------------------------------------------------
  484.         private function debuglen($var,$len){
  485.             echo "<pre>";
  486.             for($i=0;$i<$len;$i++){
  487.                 echo str_pad(dechex(ord($var[$i])),2,"0",STR_PAD_LEFT). " ";
  488.             }
  489.             echo "</pre>";
  490.         }  
  491.         //-------------------------------------------------------
  492.         private function debugstream($length){
  493.             $this->debug($this->datapart($this->pointer,$length));
  494.         }
  495.         //-------------------------------------------------------
  496.         /**
  497.         * GD Resizer Device
  498.         * Resizes the animation frames
  499.         */
  500.        
  501.         private function resizeframes(){
  502.             $k=0;
  503.             $w = $this->size_data['w'];
  504.             $h = $this->size_data['h'];
  505.             $resizemethod = 2;
  506.             //list($width, $height, $type, $attr) = getimagesize($this->file);
  507.            
  508.             foreach($this->parsedfiles as $img){               
  509.                 $newimg = null;
  510.                 $oldimg =imagecreatefromgif( $img);
  511.                
  512.                 $width = $this->imagedata[$k]["width"];
  513.                 $height = $this->imagedata[$k]["height"];
  514.                
  515.                 if($resizemethod == 4){
  516.                     $ratio = 1.0;
  517.                     $ratio_w = $width / $w;
  518.                     $ratio_h = $height / $h;
  519.                     $ratio = ($ratio_h < $ratio_w ? $ratio_h : $ratio_w);
  520.                     $neww = intval($width / $ratio);
  521.                     $newh = intval($height / $ratio);
  522.                     $tempimg = imagecreatetruecolor($neww, $newh);
  523.                    
  524.                     //save transparency
  525.                     imagecolortransparent($tempimg, imagecolorallocate($tempimg, 0, 0, 0));
  526.                     imagealphablending($tempimg, false);
  527.                     imagesavealpha($tempimg, true);
  528.                    
  529.                     /******* NOUVELLE IMAGE VIDE *******/  
  530.                     $trans = imagecolortransparent($tempimg);
  531.                     imagealphablending($tempimg, false);
  532.                     imagesavealpha($tempimg, true);
  533.                
  534.                     /*** COPIE DES PARAMETRE DE L'ORIGINAL *****/
  535.                     imagepalettecopy($tempimg,$oldimg);                
  536.                     imagefill($tempimg,0,0,imagecolortransparent($oldimg));
  537.                     imagecolortransparent($tempimg,imagecolortransparent($oldimg));                    
  538.                     imagecopyresized($tempimg, $oldimg, 0, 0, 0, 0, $neww, $newh, $width, $height);
  539.  
  540.                     $clipw = 0; $cliph = 0;
  541.                     if($neww > $w) $clipw = $neww - $w;
  542.                     if($newh > $h) $cliph = $newh - $h;
  543.    
  544.    
  545.                     $cliptop = floor($cliph / 2);
  546.                     $clipleft = floor($clipw / 2);
  547.                     $newimg = imagecreatetruecolor($w, $h);
  548.                     imagecopy($newimg, $tempimg, 0, 0, $clipleft, $cliptop, $w, $h);
  549.                    
  550.                     /********* SUPPRESSION DES IMAGES VIDES ******/
  551.                     imagedestroy($tempimg);            
  552.                 }else if($resizemethod == 3){
  553.                     $newimg = imagecreatetruecolor($w, $h);
  554.                     //save transparency
  555.                     imagecolortransparent($newimg, imagecolorallocate($newimg, 0, 0, 0));
  556.                     imagealphablending($newimg, false);
  557.                     imagesavealpha($newimg, true);
  558.                    
  559.                         /******* NOUVELLE IMAGE VIDE *******/  
  560.                     $trans = imagecolortransparent($newimg);
  561.                     imagealphablending($newimg, false);
  562.                     imagesavealpha($newimg, true);
  563.                
  564.                     /*** COPIE DES PARAMETRE DE L'ORIGINAL *****/
  565.                     imagepalettecopy($newimg,$oldimg);                 
  566.                     imagefill($newimg,0,0,imagecolortransparent($oldimg));
  567.                     imagecolortransparent($newimg,imagecolortransparent($oldimg));                     
  568.                     imagecopyresized($newimg, $oldimg, 0, 0, 0, 0, $w, $h, $width, $height);
  569.                 }else if($resizemethod == 2){
  570.                     $ratio = 1.0;
  571.                     $ratio_w = $width / $w;
  572.                     $ratio_h = $height / $h;
  573.                     $ratio = ($ratio_h > $ratio_w ? $ratio_h : $ratio_w);
  574.                     $newimg = imagecreatetruecolor(intval($width / $ratio), intval($height / $ratio));
  575.                     $this->wr = intval($width / $ratio);
  576.                     $this->hr = intval($height / $ratio);
  577.            
  578.                     //save transparency
  579.                     imagecolortransparent($newimg, imagecolorallocate($newimg, 0, 0, 0));
  580.                     imagealphablending($newimg, false);
  581.                     imagesavealpha($newimg, true);
  582.                    
  583.                     /******* NOUVELLE IMAGE VIDE *******/  
  584.                     $trans = imagecolortransparent($newimg);
  585.                    
  586.                     imagealphablending($newimg, false);
  587.                     imagesavealpha($newimg, true);
  588.                
  589.                     /*** COPIE DES PARAMETRE DE L'ORIGINAL *****/
  590.                     imagepalettecopy($newimg,$oldimg);
  591.            
  592.                     imagefill($newimg,0,0,imagecolortransparent($oldimg));
  593.                     imagecolortransparent($newimg,imagecolortransparent($oldimg));                 
  594.                    
  595.                     //store new image
  596.                     imagecopyresized($newimg, $oldimg, 0, 0, 0, 0, intval($width / $ratio), intval($height / $ratio), $width, $height);
  597.                 }else{
  598.                     $ratio = 1.0;
  599.                     if($width > $w || $height > $h){
  600.                         $ratio = $width / $w;
  601.                         if(($height / $h) > $ratio) $ratio = $height / $h; 
  602.                     }
  603.                     $newimg = imagecreatetruecolor(intval($width / $ratio), intval($height / $ratio));
  604.                    
  605.                     //save transparency
  606.                     imagecolortransparent($newimg, imagecolorallocate($newimg, 0, 0, 0));
  607.                     imagealphablending($newimg, false);
  608.                     imagesavealpha($newimg, true);
  609.                    
  610.                     /******* NOUVELLE IMAGE VIDE *******/  
  611.                     $trans = imagecolortransparent($newimg);
  612.                     imagealphablending($newimg, false);
  613.                     imagesavealpha($newimg, true);
  614.                
  615.                     /*** COPIE DES PARAMETRE DE L'ORIGINAL *****/
  616.                     imagepalettecopy($newimg,$oldimg);                 
  617.                     imagefill($newimg,0,0,imagecolortransparent($oldimg));
  618.                     imagecolortransparent($newimg,imagecolortransparent($oldimg));                     
  619.                     imagecopyresized($newimg, $oldimg, 0, 0, 0, 0, intval($width / $ratio), intval($height / $ratio), $width, $height);
  620.                 }
  621.                
  622.                 /******** SAUVEGARDE LA NOUVELLE IMAGE **********/
  623.                 imagegif($newimg,$img);
  624.                 /********* SUPPRESSION DES IMAGES VIDES ******/
  625.                 imagedestroy($oldimg);
  626.                
  627.                 $k++;
  628.             }
  629.            
  630.         }
  631.     }
  632.  
  633.  
  634. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement