搜尋
首頁後端開發php教程php图片水印添加,压缩,剪切的封装类

  php对图片文件的操作主要是利用GD库扩展。当我们频繁利用php对图片进行操作时,会自然封装很多函数,否则会写太多重复的代码。当有很多对图片的相关函数的时候,我们可以考虑将这些函数也整理一下,因而就有了封装成类的想法。

  操作图片主要历经四个步骤:

  1. 打开图片
  2. 操作图片
  3. 输出图片
  4. 销毁图片

  1,3,4三个步骤每次都要写,每次又都差不多。真正需要变通的只有操作图片的这一步骤了。操作图片又往往通过1或多个主要的GD函数来完成。

  本文封装类里面的四种方法,文字水印(imagettftext()),图片水印(imagecopymerge()),图片压缩,图片剪切(imagecopyresampled()),其余的常用GD函数便不赘述。直接上代码:

<?php class Image{        private $info;    private $image;    public $type;    public function __construct($src)    {        $this->info=getimagesize($src);        $this->type=image_type_to_extension($this->info['2'],false);        $fun="imagecreatefrom{$this->type}";        $this->image=$fun($src);    }    /**     * 文字水印     * @param  [type]  $font     字体     * @param  [type]  $content  内容     * @param  [type]  $size     文字大小     * @param  [type]  $col      文字颜色(四元数组)     * @param  array   $location 位置      * @param  integer $angle    倾斜角度     * @return [type]                */    public function fontMark($font,$content,$size,$col,$location,$angle=0){        $col=imagecolorallocatealpha($this->image, $col['0'], $col['1'], $col['2'],$col['3']);        imagettftext($this->image, $size, $angle, $location['0'], $location['1'], $col,$font,$content);    }        /**     * 图片水印     * @param  [type] $imageMark 水印图片地址     * @param  [type] $dst       水印图片在原图片中的位置     * @param  [type] $pct       透明度     * @return [type]                 */    public function imageMark($imageMark,$dst,$pct){        $info2=getimagesize($imageMark);        $type=image_type_to_extension($info2['2'],false);        $func2="imagecreatefrom".$type;        $water=$func2($imageMark);        imagecopymerge($this->image, $water, $dst[0], $dst[1], 0, 0, $info2['0'], $info2['1'], $pct);        imagedestroy($water);    }    /**     * 压缩图片     * @param  [type] $thumbSize 压缩图片大小     * @return [type]            [description]     */    public function thumb($thumbSize){        $imageThumb=imagecreatetruecolor($thumbSize[0], $thumbSize[1]);                imagecopyresampled($imageThumb, $this->image, 0, 0, 0, 0, $thumbSize[0], $thumbSize[1], $this->info['0'], $this->info['1']);        imagedestroy($this->image);        $this->image=$imageThumb;    }    /**    * 裁剪图片     * @param  [type] $cutSize  裁剪大小     * @param  [type] $location 裁剪位置     * @return [type]           [description]     */     public function cut($cutSize,$location){         $imageCut=imagecreatetruecolor($cutSize[0],$cutSize[1]);         imagecopyresampled($imageCut, $this->image, 0, 0, $location[0], $location[1],$cutSize[0],$cutSize[1],$cutSize[0],$cutSize[1]);         imagedestroy($this->image);         $this->image=$imageCut;     }    /**     * 展现图片     * @return [type] [description]     */    public function show(){        header("content-type:".$this->info['mime']);        $funn="image".$this->type;        $funn($this->image);    }    /**     * 保存图片 * @param  [type] $newname 新图片名 * @return [type]          [description] */     public function save($newname){         header("content-type:".$this->info['mime']);         $funn="image".$this->type;         $funn($this->image,$newname.'.'.$this->type);     }     public function __destruct(){         imagedestroy($this->image);     } } ?>

  如果还需要其他操作,只需要再往这个类里面添加就好啦~~

陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
如何檢查PHP會話是否已經開始?如何檢查PHP會話是否已經開始?Apr 30, 2025 am 12:20 AM

在PHP中,可以使用session_status()或session_id()來檢查會話是否已啟動。 1)使用session_status()函數,如果返回PHP_SESSION_ACTIVE,則會話已啟動。 2)使用session_id()函數,如果返回非空字符串,則會話已啟動。這兩種方法都能有效地檢查會話狀態,選擇使用哪種方法取決於PHP版本和個人偏好。

描述一個場景,其中使用會話在Web應用程序中至關重要。描述一個場景,其中使用會話在Web應用程序中至關重要。Apr 30, 2025 am 12:16 AM

sessionsarevitalinwebapplications,尤其是在commercePlatform之前。

如何管理PHP中的並發會話訪問?如何管理PHP中的並發會話訪問?Apr 30, 2025 am 12:11 AM

在PHP中管理並發會話訪問可以通過以下方法:1.使用數據庫存儲會話數據,2.採用Redis或Memcached,3.實施會話鎖定策略。這些方法有助於確保數據一致性和提高並發性能。

使用PHP會話的局限性是什麼?使用PHP會話的局限性是什麼?Apr 30, 2025 am 12:04 AM

PHPsessionshaveseverallimitations:1)Storageconstraintscanleadtoperformanceissues;2)Securityvulnerabilitieslikesessionfixationattacksexist;3)Scalabilityischallengingduetoserver-specificstorage;4)Sessionexpirationmanagementcanbeproblematic;5)Datapersis

解釋負載平衡如何影響會話管理以及如何解決。解釋負載平衡如何影響會話管理以及如何解決。Apr 29, 2025 am 12:42 AM

負載均衡會影響會話管理,但可以通過會話複製、會話粘性和集中式會話存儲解決。 1.會話複製在服務器間複製會話數據。 2.會話粘性將用戶請求定向到同一服務器。 3.集中式會話存儲使用獨立服務器如Redis存儲會話數據,確保數據共享。

說明會話鎖定的概念。說明會話鎖定的概念。Apr 29, 2025 am 12:39 AM

Sessionlockingisatechniqueusedtoensureauser'ssessionremainsexclusivetooneuseratatime.Itiscrucialforpreventingdatacorruptionandsecuritybreachesinmulti-userapplications.Sessionlockingisimplementedusingserver-sidelockingmechanisms,suchasReentrantLockinJ

有其他PHP會議的選擇嗎?有其他PHP會議的選擇嗎?Apr 29, 2025 am 12:36 AM

PHP會話的替代方案包括Cookies、Token-basedAuthentication、Database-basedSessions和Redis/Memcached。 1.Cookies通過在客戶端存儲數據來管理會話,簡單但安全性低。 2.Token-basedAuthentication使用令牌驗證用戶,安全性高但需額外邏輯。 3.Database-basedSessions將數據存儲在數據庫中,擴展性好但可能影響性能。 4.Redis/Memcached使用分佈式緩存提高性能和擴展性,但需額外配

在PHP的上下文中定義'會話劫持”一詞。在PHP的上下文中定義'會話劫持”一詞。Apr 29, 2025 am 12:33 AM

Sessionhijacking是指攻擊者通過獲取用戶的sessionID來冒充用戶。防範方法包括:1)使用HTTPS加密通信;2)驗證sessionID的來源;3)使用安全的sessionID生成算法;4)定期更新sessionID。

See all articles

熱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

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

熱工具

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具

SublimeText3 Mac版

SublimeText3 Mac版

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

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。