首頁 後端開發 php教程 php调整gif动画图片尺寸示例代码分享_PHP教程

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

Jul 13, 2016 pm 05:17 PM
demo gif php 程式碼 使用 分享 動畫 圖片 複製 尺寸 範例 類別 調整

类的使用demo:

复制代码 代码如下:

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"]                 $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;$iimagedata);$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 "
";<br>            for($i=0;$i<strlen>                echo str_pad(dechex(ord($string[$i])),2,"0",STR_PAD_LEFT). " ";<br>            }<br>            echo "</strlen>
登入後複製
";
        }
        //-------------------------------------------------------
        private function debuglen($var,$len){
            echo "
";<br>            for($i=0;$i                echo str_pad(dechex(ord($var[$i])),2,"0",STR_PAD_LEFT). " ";<br>            }<br>            echo "
登入後複製
";
        }  
        //-------------------------------------------------------
        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++;
            }
        }
    }
?>

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/621696.htmlTechArticle类的使用demo: 复制代码 代码如下: ?php require_once "roucheng.php"; $gr = new gifresizer; $gr-temp_dir = "keleyi"; $gr-resize("keleyi.gif","keleyi_resized.gif",500,500...
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南 適用於 Ubuntu 和 Debian 的 PHP 8.4 安裝和升級指南 Dec 24, 2024 pm 04:42 PM

PHP 8.4 帶來了多項新功能、安全性改進和效能改進,同時棄用和刪除了大量功能。 本指南介紹如何在 Ubuntu、Debian 或其衍生版本上安裝 PHP 8.4 或升級到 PHP 8.4

在PHP API中說明JSON Web令牌(JWT)及其用例。 在PHP API中說明JSON Web令牌(JWT)及其用例。 Apr 05, 2025 am 12:04 AM

JWT是一種基於JSON的開放標準,用於在各方之間安全地傳輸信息,主要用於身份驗證和信息交換。 1.JWT由Header、Payload和Signature三部分組成。 2.JWT的工作原理包括生成JWT、驗證JWT和解析Payload三個步驟。 3.在PHP中使用JWT進行身份驗證時,可以生成和驗證JWT,並在高級用法中包含用戶角色和權限信息。 4.常見錯誤包括簽名驗證失敗、令牌過期和Payload過大,調試技巧包括使用調試工具和日誌記錄。 5.性能優化和最佳實踐包括使用合適的簽名算法、合理設置有效期、

您如何在PHP中解析和處理HTML/XML? 您如何在PHP中解析和處理HTML/XML? Feb 07, 2025 am 11:57 AM

本教程演示瞭如何使用PHP有效地處理XML文檔。 XML(可擴展的標記語言)是一種用於人類可讀性和機器解析的多功能文本標記語言。它通常用於數據存儲

解釋PHP中的晚期靜態綁定(靜態::)。 解釋PHP中的晚期靜態綁定(靜態::)。 Apr 03, 2025 am 12:04 AM

靜態綁定(static::)在PHP中實現晚期靜態綁定(LSB),允許在靜態上下文中引用調用類而非定義類。 1)解析過程在運行時進行,2)在繼承關係中向上查找調用類,3)可能帶來性能開銷。

php程序在字符串中計數元音 php程序在字符串中計數元音 Feb 07, 2025 pm 12:12 PM

字符串是由字符組成的序列,包括字母、數字和符號。本教程將學習如何使用不同的方法在PHP中計算給定字符串中元音的數量。英語中的元音是a、e、i、o、u,它們可以是大寫或小寫。 什麼是元音? 元音是代表特定語音的字母字符。英語中共有五個元音,包括大寫和小寫: a, e, i, o, u 示例 1 輸入:字符串 = "Tutorialspoint" 輸出:6 解釋 字符串 "Tutorialspoint" 中的元音是 u、o、i、a、o、i。總共有 6 個元

什麼是PHP魔術方法(__ -construct,__destruct,__call,__get,__ set等)並提供用例? 什麼是PHP魔術方法(__ -construct,__destruct,__call,__get,__ set等)並提供用例? Apr 03, 2025 am 12:03 AM

PHP的魔法方法有哪些? PHP的魔法方法包括:1.\_\_construct,用於初始化對象;2.\_\_destruct,用於清理資源;3.\_\_call,處理不存在的方法調用;4.\_\_get,實現動態屬性訪問;5.\_\_set,實現動態屬性設置。這些方法在特定情況下自動調用,提升代碼的靈活性和效率。

PHP和Python:比較兩種流行的編程語言 PHP和Python:比較兩種流行的編程語言 Apr 14, 2025 am 12:13 AM

PHP和Python各有優勢,選擇依據項目需求。 1.PHP適合web開發,尤其快速開發和維護網站。 2.Python適用於數據科學、機器學習和人工智能,語法簡潔,適合初學者。

PHP:網絡開發的關鍵語言 PHP:網絡開發的關鍵語言 Apr 13, 2025 am 12:08 AM

PHP是一種廣泛應用於服務器端的腳本語言,特別適合web開發。 1.PHP可以嵌入HTML,處理HTTP請求和響應,支持多種數據庫。 2.PHP用於生成動態網頁內容,處理表單數據,訪問數據庫等,具有強大的社區支持和開源資源。 3.PHP是解釋型語言,執行過程包括詞法分析、語法分析、編譯和執行。 4.PHP可以與MySQL結合用於用戶註冊系統等高級應用。 5.調試PHP時,可使用error_reporting()和var_dump()等函數。 6.優化PHP代碼可通過緩存機制、優化數據庫查詢和使用內置函數。 7

See all articles