給圖片生成縮略圖和加版權的類

發表于:2007-05-25來源:作者:點擊數: 標簽:略圖版權圖片生成
給圖片生成縮略圖和加版權的類 最近幾天看了一下 PHP 的圖片處理方面的功能,以前這方面的 需求 比較少,也就沒怎么看,最近有空看了一下。感覺圖片處理一些簡單的功能還可以,復雜的就算了,GD庫都2.0.1了,還是不支持中文,看了幾篇文章,想使用中文只能先

給圖片生成縮略圖和加版權的類

最近幾天看了一下PHP的圖片處理方面的功能,以前這方面的需求比較少,也就沒怎么看,最近有空看了一下。感覺圖片處理一些簡單的功能還可以,復雜的就算了,GD庫都2.0.1了,還是不支持中文,看了幾篇文章,想使用中文只能先將GB2312轉換成UNICODE再寫入圖片,太麻煩了,索性只使用英文算了。

在圖像生成部分可以定義圖片的最大高,寬,比較適用于新聞及相冊等系統。

GD2.0.1在圖片處理上有很大提高,我試了下imageCopyResized和imageCopyResampled,后者處理的圖片明顯好于前者,據手冊上講后者對改變大小后的圖片重新采樣基本保持不失真,生成縮略圖的效果還真不錯。

-------------------------------------------------------------
下面是類
-----------------------
//====================================================
// 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 = false; //是否生成文件
var $fontName; //使用的TTF字體名稱
var $maxWidth = 500; //圖片最大寬度
var $maxHeight = 600; //圖片最大高度

//==========================================
// 函數: GDImage($sourcePath ,$galleryPath, $fontPath)
// 功能: constructor
// 參數: $sourcePath 圖片源路徑(包括最后一個"/")
// 參數: $galleryPath 生成圖片的路徑
// 參數: $fontPath 字體路徑
//==========================================
function GDImage($sourcePath, $galleryPath, $fontPath)
{
$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=128,$height=128)
{
$imageInfo = $this->getInfo($sourFile);
$sourFile = $this->sourcePath . $sourFile;
$newName = substr($imageInfo["name"], 0, strrpos($imageInfo["name"], ".")) . "_thumb.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 = ($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 = "";
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 = "";
echo $html;
}
//==========================================
// 函數: getInfo($file)
// 功能: 返回圖像信息
// 參數: $file 文件路徑
// 返回: 圖片信息數組
//==========================================
function getInfo($file)
{
$file = $this->sourcePath . $file;
$data = getimagesize($file);
$imageInfo["width"] = $data[0];
$imageInfo["height"]= $data[1];
$imageInfo["type"] = $data[2];
$imageInfo["name"] = basename($file);
return $imageInfo;
}

}

?>
----------------------------------
下面是使用方法
這個類使用了一個04B_08__.TTF字體
使用類的時候指定該字體路徑即可
-----------------------------------
require_once("GDImage.inc.php");

//header("Content-type: image/jpeg");//輸出到瀏覽器的話別忘了打開這個
$img = new GDImage(IB_UPLOAD_PATH, IB_GALLERY, IB_TEMP);
$text = array("ice-berg.org","all rights reserved");
$img->maxWidth = $img->maxHeight = 300;
$img->toFile = true;
$img->waterMark("mm.jpg", $text);
$img->makeThumb("mm.jpg");
$img->displayThumb("mm.jpg");
$img->displayMark("mm.jpg");

原文轉自:http://www.anti-gravitydesign.com

国产97人人超碰caoprom_尤物国产在线一区手机播放_精品国产一区二区三_色天使久久综合给合久久97