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

类的使用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 $parsedfiles = array();
        private $originalwidth = 0;
        private $originalheight = 0;
        private $wr,$hr;
        private $props = array();
        private $decoding = false;

/**
        * 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->encode($newfile,$width,$height);
            $this->clearframes();
        }

/**
        * GIF Decoder function.
        * Parses 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->closefile();
            $this->decoding = false;
        }

/**
        * GIF Encoder function.
        * Combines the parsed 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->parsedfiles 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(isset($this->encdata[$this->index-1]))
                    $gxdata = $this->encdata[$this->index-1]["graphicsextension"];
                else
                    $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);       
                    }else{
                        //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]["graphicsextension"]) $this->imagedata[$this->index-1]["graphicsextension"] = chr(0x21).chr(0xf9).chr(0x04).chr(0x00).chr(0x00).chr(0x00).chr(0x00).chr(0x00);

$imagedata = &$this->imagedata[$this->index-1]["graphicsextension"];

$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 offset
                $imageblock[1] = chr(round(($this->orgvars[$this->index-1]["offset_left"]*$this->wr) % 256));
                $imageblock[2] = chr(floor(($this->orgvars[$this->index-1]["offset_left"]*$this->wr) / 256));
                $imageblock[3] = chr(round(($this->orgvars[$this->index-1]["offset_top"]*$this->hr) % 256));
                $imageblock[4] = chr(floor(($this->orgvars[$this->index-1]["offset_top"]*$this->hr) / 256));

if($this->index==1){
                    if(!isset($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(!isset($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(isset($this->orgvars["hasgx_type_0"]) && $this->orgvars["hasgx_type_0"]) $mystring .= $this->globaldata["graphicsextension_0"];
                    if(isset($this->orgvars["hasgx_type_1"]) && $this->orgvars["hasgx_type_1"]) $mystring .= $this->globaldata["graphicsextension"];
                }

$mystring .= $imagedata . $imageblock;
                $k++;
                $this->closefile();
            }

$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 Reset function
        * If a instance is used 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->parsedfiles = array();
        }

/**
        * Clear Frames function
        * For deleting the frames after encoding.
        */
        private function clearframes(){
            foreach($this->parsedfiles 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]["graphicsextension"].$this->imagedata[$i]["imagedata"].chr(0x3b));
                $this->parsedfiles[]=$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.
                }else{
                    //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;
                    //set 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);
                }
            }else{
                //global color table doesn't exist. skip.
            }
        }

/**
        * GIF Parser Functions.
        * Below functions are the main structure parser 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);
            }else{
                $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);
            }else{
                $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);
            }else{
                $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]["graphicsextension"] = $this->datapart($start,$this->pointer-$start);
                }else if($type==1){
                    $this->orgvars["hasgx_type_1"] = 1;
                    $this->globaldata["graphicsextension"] = $this->datapart($start,$this->pointer-$start);
                }else if($type==0 && $this->decoding==false){
                    $this->encdata[$this->index]["graphicsextension"] = $this->datapart($start,$this->pointer-$start);
                }else if($type==0 && $this->decoding==true){
                    $this->orgvars["hasgx_type_0"] = 1;
                    $this->globaldata["graphicsextension_0"] = $this->datapart($start,$this->pointer-$start);
                }
            }else{
                $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(isset($this->globaldata["graphicsextension_0"]))
                        $this->imagedata[$this->index]["graphicsextension"]=$this->globaldata["graphicsextension_0"];
                    else
                        $this->imagedata[$this->index]["graphicsextension"]=null;
                    unset($this->globaldata["graphicsextension_0"]);
                }elseif($type==1){
                    if(isset($this->orgvars["hasgx_type_1"]) && $this->orgvars["hasgx_type_1"]==1){
                        $this->orgvars["hasgx_type_1"] = 0;
                        $this->imagedata[$this->index]["graphicsextension"]=$this->globaldata["graphicsextension"];
                        unset($this->globaldata["graphicsextension"]);
                    }else{
                        $this->orgvars["hasgx_type_0"] = 0;
                        $this->imagedata[$this->index]["graphicsextension"]=$this->globaldata["graphicsextension_0"];
                        unset($this->globaldata["graphicsextension_0"]);
                    }
                }

$this->parse_image_data();
                $this->index++;

}
        }
        //-------------------------------------------------------
        private function parse_image_data(){
            $this->imagedata[$this->index]["disposal_method"] = $this->get_imagedata_bit("ext",3,3,3);
            $this->imagedata[$this->index]["user_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]["offset_left"] = $this->dualbyteval($this->get_imagedata_byte("dat",1,2));
            $this->imagedata[$this->index]["offset_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]["offset_left"] = $this->imagedata[$this->index]["offset_left"];
                $this->orgvars[$this->index]["offset_top"] = $this->imagedata[$this->index]["offset_top"];
            }
        }
        //-------------------------------------------------------
        private function get_imagedata_byte($type,$start,$length){
            if($type=="ext")
                return substr($this->imagedata[$this->index]["graphicsextension"],$start,$length);
            elseif($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]["graphicsextension"],$byteindex,1)),$bitstart,$bitlength);
            elseif($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 = fopen($filename,"rb");
            $this->pointer = 0;
        }
        //-------------------------------------------------------
        private function closefile(){
            fclose($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;
            fseek($this->handle,$this->pointer);
        }
        //-------------------------------------------------------
        private function p_forward($length){
            $this->pointer+=$length;
            fseek($this->handle,$this->pointer);
        }
        //-------------------------------------------------------
        private function datapart($start,$length){
            fseek($this->handle,$start);
            $data = fread($this->handle,$length);
            fseek($this->handle,$this->pointer);
            return $data;
        }
        //-------------------------------------------------------
        private function checkbyte($byte){
            if(fgetc($this->handle)==chr($byte)){
                fseek($this->handle,$this->pointer);
                return true;
            }else{
                fseek($this->handle,$this->pointer);
                return false;
            }
        }  
        //-------------------------------------------------------
        private function checkEOF(){
            if(fgetc($this->handle)===false){
                return true;
            }else{
                fseek($this->handle,$this->pointer);
                return false;
            }
        }  
        //-------------------------------------------------------
        /**
        * Debug Functions.  keleyi.com
        * Parses 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->parsedfiles 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, false);
                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++;
            }
        }
    }
?>

(0)

相关推荐

  • php实现按指定大小等比缩放生成上传图片缩略图的方法

    本文实例讲述了php实现按指定大小等比缩放生成上传图片缩略图的方法.分享给大家供大家参考.具体实现方法如下: 复制代码 代码如下: /**  * *  *等比缩放  * @param unknown_type $srcImage   源图片路径  * @param unknown_type $toFile     目标图片路径  * @param unknown_type $maxWidth   最大宽  * @param unknown_type $maxHeight  最大高  * @par

  • php修改上传图片尺寸的方法

    本文实例讲述了php修改上传图片尺寸的方法.分享给大家供大家参考.具体实现方法如下: <?php // This is the temporary file created by PHP $uploadedfile = $_FILES['uploadfile']['tmp_name']; // Create an Image from it so we can do the resize $src = imagecreatefromjpeg($uploadedfile); // Capture

  • php将图片保存为不同尺寸图片的图片类实例

    本文实例讲述了php将图片保存为不同规格的图片类.分享给大家供大家参考.具体如下: 图片处理类.imagecls.php如下: <?php /** 图片处理类 */ class imagecls { /** * 文件信息 */ var $file = array(); /** * 保存目录 */ var $dir = ''; /** * 错误代码 */ var $error_code = 0; /** * 文件上传最大KB */ var $max_size = -1; function es_i

  • PHP图片自动裁切应付不同尺寸的显示

    如果做过那种门户站的朋友,肯定知道,一张图片可能会在不同的地方显示,大小不同,比例也不同, 如果只用一张图的话,那么肯定会变形,而且在显示小图的地方,链接 大图,又太浪费了.....用缩略图来处理,也不完美,因为每个地方出现的比例 大小可能都不一样 ,举个例子! 请看上图. 在这个地方,其实调去出来的是一个列表,但是 图片的大小是不一样的,有多大宽有的窄,,当遇到这样的情况的时候 你们怎么办呢,如果直接用原来的地址,肯定是会变形的,如果搞缩略图也不靠谱,这个调去是自动调去的,你根本不知道哪个图片

  • php实现上传图片生成缩略图示例

    功能很简单,代码中有注释,直接给大家上代码了 复制代码 代码如下: <?php/** * 上传图片生成缩略图 *  * 需要GD2库的支持 *  * 初始化时需要参数new thumbnails('需要缩略的图片的原始地址','缩略图的宽度','缩略图的高度','(可选参数)缩略图的保存路径'); * 如果最后一个参数不指定,那么缩略图就默认保存在原始图片的所在目录里的small文件夹里, * 如果不存在small文件夹,则会自动创建small文件夹 *  * 初始化之后需要调用方法produc

  • php中使用getimagesize获取图片、flash等文件的尺寸信息实例

    如果你还想着通过解析swf文件头信息来获取flash文件的尺寸信息,那真的有点走远了.因为从PHP 4开始已经内置getimagesize函数来做这个事.其功能测定任何 GIF,JPG,PNG,SWF,SWC,PSD,TIFF,BMP,IFF,JP2,JPX,JB2,JPC,XBM 或 WBMP 图像文件的大小并返回图像的尺寸以及文件类型和一个可以用于普通 HTML 文件中 IMG 标记中的 height/width 文本字符串.而且从PHP 4.0.5起还支持参数是一个url.例如: 复制代码

  • php实现高效获取图片尺寸的方法

    本文实例讲述了php实现高效获取图片尺寸的方法.分享给大家供大家参考.具体分析如下: php 获取图片尺寸的方法我们可以使用 getimagesize 获取图片尺寸,但是效率是很低的,首先需要获取整个的图片信息,然后再进行操作,下面的例子更科学算法更好,我们一起来看看吧. 方法可以用于快速获取图片尺寸信息,获取JPEG格式图片的尺寸信息,并且不需要下载读取整个图片,经测试这个函数不是对所有JPEG格式的图片都有效. 1.获取JPEG格式图片的尺寸信息,代码如下: 复制代码 代码如下: <?php

  • PHP中改变图片的尺寸大小的代码

    先介绍一个自己写的函数. 复制代码 代码如下: <?php $imgsrc = "http://www.nowamagic.net/images/3.jpg"; $width = 780; $height = 420; resizejpg($imgsrc,$imgdst,$width,$height); function resizejpg($imgsrc,$imgdst,$imgwidth,$imgheight) { //$imgsrc jpg格式图像路径 $imgdst jp

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

    类的使用demo: 复制代码 代码如下: <?php require_once "roucheng.php";  $gr = new gifresizer; $gr->temp_dir = "keleyi"; $gr->resize("keleyi.gif","keleyi_resized.gif",500,500); ?> 类的源代码,保存为roucheng.php文件: 复制代码 代码如下: <

  • three.js 实现露珠滴落动画效果的示例代码

    前言 大家好,这里是 CSS 魔法使--alphardex. 本文我们将用three.js来实现一种很酷的光学效果--露珠滴落.我们知道,在露珠从一个物体表面滴落的时候,会产生一种粘着的效果.2D平面中,这种粘着效果其实用css滤镜就可以轻松实现.但是到了3D世界,就没那么简单了,这时我们就得依靠光照来实现,其中涉及到了一个关键算法--光线步进(Ray Marching).以下是最终实现的效果图 撒,哈吉马路由! 准备工作 笔者的 three.js模板 :点击右下角的fork即可复制一份 正片

  • 使用JavaScript 实现时间轴与动画效果的示例代码(前端组件化)

    目录 代码整理 JavaScript 中的 "帧" 实现"帧"的方法 1. setInterval 2. setTimeout 3. requestAnimationFrame 实现 Timeline 时间轴 实现 start 函数 实现 Animation 类 设计时间线的更新 添加 Delay 属性支持 实现暂停和重启功能 实现 Pause 实现 Resume 上一篇文章<用 JSX 实现 Carousel 轮播组件>中,我们实现了一个 "

  • 基于Java实现修改图片分辨率示例代码

    目录 前言 环境依赖 代码 验证一下 前言 本文提供可以修改图片分辨率的java工具类,实用主义的狂欢. 环境依赖 添加必要的一些maven依赖. <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.7.15</version> </dependency> <dependency&

  • C#实现自定义打印文字和图片的示例代码

    目录 1.调用打印机设置 2.关联文档 3.绘制内容 C#中打印其实就是自己绘图+调用系统打印函数,于是便有了以下操作 1.调用打印机设置 如果你想在打印前设置打印机属性(或者切换打印机),请务必添加这段代码,否则电脑会直接按照预设的设置进行打印(打印机都没法选) //打印机设置 PrintDialog printDialog = new PrintDialog(); printDialog.Document = ptDoc; printDialog.ShowDialog(); ptDoc就是打

  • Flutter实现牛顿摆动画效果的示例代码

    目录 前言 实现步骤 1.绘制静态效果 2.加入动画 两个关键点 完整源码 总结 前言 牛顿摆大家应该都不陌生,也叫碰碰球.永动球(理论情况下),那么今天我们用Flutter实现这么一个理论中的永动球,可以作为加载Loading使用. - 知识点:绘制.动画曲线.多动画状态更新 效果图: 实现步骤 1.绘制静态效果 首先我们需要把线和小圆球绘制出来,对于看过我之前文章的小伙伴来说这个就很简单了,效果图: 关键代码: // 小圆球半径 double radius = 6; /// 小球圆心和直线终

  • java后台接收app上传的图片的示例代码

    整理文档,搜刮出一个java后台接受app上传的图片的示例代码,稍微整理精简一下做下分享 package com.sujinabo.file; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.util.List; import java.util.UUID; import javax.servlet.S

  • java 生成文字图片的示例代码

    本文主要介绍了java 生成文字图片的示例代码,分享给大家,具体如下: import java.awt.Color; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Rectangle; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO;

  • Java 添加、替换、删除PDF中的图片的示例代码

    概述 本文介绍通过java程序向PDF文档添加图片,以及替换和删除PDF中已有的图片.另外,关于图片的操作还可参考设置PDF 图片背景.设置PDF图片水印.读取PDF中的图片.将PDF保存为图片等文章. 工具:Free Spire.PDF for Java (免费版) Jar获取及导入:官网下载,并解压将lib文件夹下的jar文件导入java程序,或者通过maven仓库下载并导入. jar导入效果: Java代码示例 [示例1]添加图片到PDF import com.spire.pdf.*; i

  • Python无损压缩图片的示例代码

    每个设计师.摄影师或有图片处理需求小编,都会面临批量高清大图的困扰. 因为高清大图放到网站上会严重拖慢加载速度,或是有的地方明确限制了图片大小,因此,为了完成工作,他们总是需要先把图片压缩,再上传. 当需要处理的图片多至十张.百张.千张,则严重影响工作效率.这时候,就可以交给Python啦! 只需要20行Python代码,就可以批量帮你无损压缩数张照片. ---1--- 前期工作 安装Python中现成的图片处理模块,然后将图片打包好导入,用循环的方式自动化处理图片就可以了! ---2--- 运

随机推荐