国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 開發 > PHP > 正文

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

2024-05-04 23:04:08
字體:
來源:轉載
供稿:網友

最近幾天看了一下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 [email protected]
//====================================================

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 = "";%20
echo%20$html;%20
}%20
//==========================================%20
//%20函數:%20displaymark($file)%20
//%20功能:%20顯示指定圖片的水印圖%20
//%20參數:%20$file%20文件名%20
//%20返回:%200%20圖片不存在%20
//==========================================%20
function%20displaymark($file)%20
{%20
$markname%20=%20substr($file,%200,%20strrpos($file,%20"."))%20.%20"_mark.jpg";
$file = $this->gallerypath . $markname;
if (!file_exists($file))
return 0;
$html = "";%20
echo%20$html;%20
}%20
//==========================================%20
//%20函數:%20getinfo($file)%20
//%20功能:%20返回圖像信息%20
//%20參數:%20$file%20文件路徑%20
//%20返回:%20圖片信息數組%20
//==========================================%20
function%20getinfo($file)%20
{%20
$file%20=%20$this->sourcepath%20.%20$file;%20
$data%20=%20getimagesize($file);%20
$imageinfo["width"]%20=%20$data[0];%20
$imageinfo["height"]=%20$data[1];%20
$imageinfo["type"]%20=%20$data[2];%20
$imageinfo["name"]%20=%20basename($file);%20
return%20$imageinfo;%20
}%20

}%20

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

//header("content-type:%20image/jpeg");//輸出到瀏覽器的話別忘了打開這個%20
$img%20=%20new%20gdimage(ib_upload_path,%20ib_gallery,%20ib_temp);%20
$text%20=%20array("ice-berg.org","all%20rights%20reserved");%20
$img->maxwidth%20=%20$img->maxheight%20=%20300;%20
$img->tofile%20=%20true;%20
$img->watermark("mm.jpg", $text);
$img->makethumb("mm.jpg");
$img->displaythumb("mm.jpg");
$img->displaymark("mm.jpg");


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 长泰县| 上高县| 博乐市| 阿巴嘎旗| 钦州市| 邵武市| 中牟县| 司法| 焉耆| 祁门县| 西平县| 龙陵县| 曲麻莱县| 龙山县| 偃师市| 磴口县| 子洲县| 潍坊市| 太和县| 九龙坡区| 涟源市| 久治县| 达州市| 唐海县| 五原县| 友谊县| 扶沟县| 衢州市| 舞阳县| 兰州市| 临沧市| 宁远县| 永康市| 册亨县| 台山市| 大方县| 弥勒县| 东乡族自治县| 新营市| 安国市| 读书|