?? gdimage.inc.php
字號:
<?php/** * 基本圖片處理,用于完成圖片縮入,水印添加 * 當水印圖超過目標圖片尺寸時,水印圖能自動適應目標圖片而縮小 * 水印圖可以設置跟背景的合并度 * * Copyright(c) 2005 by ustb99. All rights reserved * * To contact the author write to {@link mailto:ustb99@hotmail.com} * * @author 偶然 * @version $Id: thumb.class.php,v 1.0 2005/6/21 19:00:04 $ * @package system *///==================================================== // FileName:GDImage.inc.php // Summary: 圖片處理程序 // Author: ice_berg16(尋夢的稻草人) // CreateTime: 2004-10-12 // LastModifed:2004-10-12 // copyright (c)2004 ice_berg16@163.com //==================================================== class GDImage { var $sourcePath; //圖片存儲路徑 var $galleryPath; //圖片縮略圖存儲路徑 var $toFile = true; //是否生成文件 var $fontName; //使用的TTF字體名稱 var $maxWidth = 500; //圖片最大寬度 var $maxHeight = 600; //圖片最大高度 //========================================== // 函數: GDImage($sourcePath ,$galleryPath, $fontPath) // 功能: constructor // 參數: $sourcePath 圖片源路徑(包括最后一個"/") // 參數: $galleryPath 生成圖片的路徑 // 參數: $fontPath 字體路徑 //========================================== function GDImage($sourcePath, $galleryPath, $fontPath = null) { $this->sourcePath = $sourcePath; $this->galleryPath = $galleryPath; //$this->fontName = $fontPath . "04B_08__.TTF"; } //========================================== // 函數: makeThumb($sourFile,$width=128,$height=128) // 功能: 生成縮略圖(輸出到瀏覽器) // 參數: $sourFile 圖片源文件 // 參數: $width 生成縮略圖的寬度 // 參數: $height 生成縮略圖的高度 // 返回: 0 失敗 成功時返回生成的圖片路徑 //========================================== function makeThumb($sourFile,$width=120,$height=120) { $imageInfo = $this->getInfo($sourFile); $sourFile = $this->sourcePath . $sourFile;$tmpParts = explode(".",$imageInfo["name"]);//print_r($tmpParts);$file_ext = $tmpParts[1];$newName = substr($imageInfo["name"], 0, strrpos($imageInfo["name"], ".")) . ".".$file_ext; switch ($imageInfo["type"]) { case 1: //gif $img = imagecreatefromgif($sourFile); break; case 2: //jpg $img = imagecreatefromjpeg($sourFile); break; case 3: //png $img = imagecreatefrompng($sourFile); break; default: return 0; break; } if (!$img) return 0; $width = ($width > $imageInfo["width"]) ? $imageInfo["width"] : $width; $height = ($height > $imageInfo["height"]) ? $imageInfo["height"] : $height; $srcW = $imageInfo["width"]; $srcH = $imageInfo["height"]; if ($srcW * $width > $srcH * $height) $height = round($srcH * $width / $srcW); else $width = round($srcW * $height / $srcH); //* if (function_exists("imagecreatetruecolor")) //GD2.0.1 { $new = imagecreatetruecolor($width, $height); ImageCopyResampled($new, $img, 0, 0, 0, 0, $width, $height, $imageInfo["width"], $imageInfo["height"]); } else { $new = imagecreate($width, $height); ImageCopyResized($new, $img, 0, 0, 0, 0, $width, $height, $imageInfo["width"], $imageInfo["height"]); } //*/ if ($this->toFile) { if (file_exists($this->galleryPath . $newName)) unlink($this->galleryPath . $newName); ImageJPEG($new, $this->galleryPath . $newName); return $this->galleryPath . $newName; } else { ImageJPEG($new); } ImageDestroy($new); ImageDestroy($img); } //========================================== // 函數: waterMark($sourFile, $text) // 功能: 給圖片加水印 // 參數: $sourFile 圖片文件名 // 參數: $text 文本數組(包含二個字符串) // 返回: 1 成功 成功時返回生成的圖片路徑 //========================================== function waterMark($sourFile, $text) { $imageInfo = $this->getInfo($sourFile); $sourFile = $this->sourcePath . $sourFile; $newName = substr($imageInfo["name"], 0, strrpos($imageInfo["name"], ".")) . "_mark.jpg"; switch ($imageInfo["type"]) { case 1: //gif $img = imagecreatefromgif($sourFile); break; case 2: //jpg $img = imagecreatefromjpeg($sourFile); break; case 3: //png $img = imagecreatefrompng($sourFile); break; default: return 0; break; } if (!$img) return 0; $width = ($this->maxWidth > $imageInfo["width"]) ? $imageInfo["width"] : $this->maxWidth; $height = ($this->maxHeight > $imageInfo["height"]) ? $imageInfo["height"] : $this->maxHeight; $srcW = $imageInfo["width"]; $srcH = $imageInfo["height"]; if ($srcW * $width > $srcH * $height) $height = round($srcH * $width / $srcW); else $width = round($srcW * $height / $srcH); //* if (function_exists("imagecreatetruecolor")) //GD2.0.1 { $new = imagecreatetruecolor($width, $height); ImageCopyResampled($new, $img, 0, 0, 0, 0, $width, $height, $imageInfo["width"], $imageInfo["height"]); } else { $new = imagecreate($width, $height); ImageCopyResized($new, $img, 0, 0, 0, 0, $width, $height, $imageInfo["width"], $imageInfo["height"]); } $white = imageColorAllocate($new, 255, 255, 255); $black = imageColorAllocate($new, 0, 0, 0); $alpha = imageColorAllocateAlpha($new, 230, 230, 230, 40); //$rectW = max(strlen($text[0]),strlen($text[1]))*7; ImageFilledRectangle($new, 0, $height-26, $width, $height, $alpha); ImageFilledRectangle($new, 13, $height-20, 15, $height-7, $black); ImageTTFText($new, 4.9, 0, 20, $height-14, $black, $this->fontName, $text[0]); ImageTTFText($new, 4.9, 0, 20, $height-6, $black, $this->fontName, $text[1]); //*/ if ($this->toFile) { if (file_exists($this->galleryPath . $newName)) unlink($this->galleryPath . $newName); ImageJPEG($new, $this->galleryPath . $newName); return $this->galleryPath . $newName; } else { ImageJPEG($new); } ImageDestroy($new); ImageDestroy($img); } //========================================== // 函數: displayThumb($file) // 功能: 顯示指定圖片的縮略圖 // 參數: $file 文件名 // 返回: 0 圖片不存在 //========================================== function displayThumb($file) { $thumbName = substr($file, 0, strrpos($file, ".")) . "_thumb.jpg"; $file = $this->galleryPath . $thumbName; if (!file_exists($file)) return 0; $html = "<img src='$file' style='border:1px solid #000'/>"; echo $html; } //========================================== // 函數: displayMark($file) // 功能: 顯示指定圖片的水印圖 // 參數: $file 文件名 // 返回: 0 圖片不存在 //========================================== function displayMark($file) { $markName = substr($file, 0, strrpos($file, ".")) . "_mark.jpg"; $file = $this->galleryPath . $markName; if (!file_exists($file)) return 0; $html = "<img src='$file' style='border:1px solid #000'/>"; echo $html; } //========================================== // 函數: getInfo($file) // 功能: 返回圖像信息 // 參數: $file 文件路徑 // 返回: 圖片信息數組 //========================================== function getInfo($file) { $file = $this->sourcePath . $file;//echo "file:".$file;$data = getimagesize($file); $imageInfo["width"] = $data[0]; $imageInfo["height"]= $data[1]; $imageInfo["type"] = $data[2]; $imageInfo["name"] = basename($file); return $imageInfo; } } ?>
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -