PHP製作圖片縮圖、加浮水印、加字型 php網站製作教程 php網站後台製作教程 phpcms模板制

來源:互聯網
上載者:User
下面是我提供的一個類,下面封裝了這三種功能:

imgSrc = $imgSrc;$this->init();}/*** 初始化操作*/private function init() {//擷取圖片資訊(通過GD庫提供的方法,得到你想要處理的圖片的基本資料)$this->info = getimagesize($this->imgSrc);//通過映像的編號來擷取映像的類型$this->type = image_type_to_extension($this->info[2], false);//在記憶體中建立一個和我們映像類型一樣的映像$fun = "imagecreatefrom{$this->type}";//把圖片複製到記憶體中$this->image = $fun($this->imgSrc);$this->showOrSaveFunc = "image{$this->type}";}/*** 給圖片添加字型* @param $fontfile 字型檔路徑* @param $text 文字內容* @param $red 紅色分量* @param $green 綠色分量* @param $blue 藍色分量* @param $alpha 透明度* @param $angle 位移角度* @param $size 字型大小* @param $x 距離左邊的距離* @param $y 距離右邊的距離*/public function addFont($fontfile, $text, $red, $green, $blue, $alpha, $angle, $size, $x, $y) {$color = imagecolorallocatealpha($this->image, $red, $green, $blue, $alpha);imagettftext($this->image, $size, $angle, $x, $y, $color, $fontfile, $text);}/*** 瀏覽器輸出圖片類型*/private function setOutputHeader() {header("Content-Type:".$this->info['mime']);}/*** 輸出圖片到瀏覽器*/public function outputImageToBrowser() {$this->setOutputHeader();$this->excShowOrSaveFunc($this->showOrSaveFunc);}/*** 輸出或者儲存圖片*/private function excShowOrSaveFunc($type, $file='') {if ($file == '') {$type($this->image);} else {$type($this->image, $file);}}/*** 儲存檔案到本地* @param 本地路徑* @param 檔案名稱*/public function outputImageToStorage($filepath, $filename) {$this->setOutputHeader();$this->excShowOrSaveFunc($this->showOrSaveFunc, $filepath.$filename.'.'.$this->type);}/*** 為圖片添加浮水印* @param $waterMarkPath 浮水印圖片路徑* @param $dst_x 浮水印距離原圖左側的距離* @param $dst_y 浮水印距離原圖上側的距離* @param $pct 浮水印的透明度*/public function addWaterMark($waterMarkPath, $dst_x, $dst_y, $pct) {$waterInfo = getimagesize($waterMarkPath);$waterType = image_type_to_extension($waterInfo[2], false);$waterFun = "imagecreatefrom{$waterType}";$waterMarkImage = $waterFun($waterMarkPath);//imagecopymerge(dst_im, src_im, dst_x, dst_y, src_x, src_y, src_w, src_h, pct)//pct透明度imagecopymerge($this->image, $waterMarkImage, $dst_x, $dst_y, 0, 0, $waterInfo[0], $waterInfo[1], $pct);imagedestroy($waterMarkImage);}/*** 產生圖片縮圖* @param $mWidth 想要壓縮的寬度*/public function createThumbnail($mWidth) {$width = $this->info[0];$height = $this->info[1];if ($mWidth >= $width) {return ;}$mHeight = $height * $mWidth / $width;//1.在記憶體中建立一個寬300、高200的真色彩圖片$imageThumb = imagecreatetruecolor($mWidth, $mHeight);//2.核心步驟、將原圖複製到建立的正色彩圖片上,並按照一定比例壓縮//imagecopyresampled(dst_image, src_image, dst_x, dst_y, src_x, src_y, dst_w, dst_h, src_w, src_h)imagecopyresampled($imageThumb, $this->image, 0, 0, 0, 0, $mWidth, $mHeight, $width, $height);$this->image = $imageThumb;}/** 銷毀圖片資源*/public function destroy() {imagedestroy($this->image);}} ?>

使用方法如下:

addFont("consola.ttf", "raid", 0, 0, 0, 50, 0, 45, 20, 20);$imageHelper->addWaterMark('pic2.jpg', 0, 0, 50);*/$imageHelper->createThumbnail(300);// $imageHelper->outputImageToBrowser();$imageHelper->outputImageToStorage('','testImageHelperAddFont');$imageHelper->destroy(); ?>

以上就介紹了PHP製作圖片縮圖、加浮水印、加字型,包括了製作圖片,php方面的內容,希望對PHP教程有興趣的朋友有所協助。

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.