PHP上傳自動(dòng)生成縮略圖及水印類(含代碼)_PHP教程
推薦:PHP技術(shù):txtSQL安裝手冊(cè)中文版txtsql的最大優(yōu)點(diǎn)之一是文檔很詳細(xì),可惜,我在網(wǎng)上找了半天也找不到中文版的文檔,所以只好自己動(dòng)手,利人利已吧,不過自己的E文水平自己是很清楚的,希望大家看了不會(huì)笑掉大牙才好,還希望大家多多指教。 歡迎使用txtSQL 2.2快速安裝手冊(cè)。這頁(yè)將指引你如何
思路很大一部分是原創(chuàng)的,但也有一些是COPY網(wǎng)絡(luò)的,寫得不夠規(guī)范,還請(qǐng)各位大大不要見笑,同時(shí)給小弟些意見。
開始第一步:
創(chuàng)建文件夾,布局:
annex:附件(該目錄下存放上傳的原圖片)
|— smallimg:存放縮略圖片
|— mark:存放水印圖片
include:存放類文件,字體(本程序代碼使用的是:04B_08__.TTF)
|— upfile.php:集成簡(jiǎn)單上傳,生成縮略圖及水印的類文件信息
|— 04B_08__.TTF:字體文件
test.php:測(cè)試文件
進(jìn)入第二步:
代碼研究,希望各位能好好看看,小弟也是初學(xué)者,同時(shí)也希望各位能提出寶貴意見,小弟定會(huì)虛心領(lǐng)教的(寫得不好不要拿雞蛋砸偶噢)
-------------------------------------------------------------------------------
upfile.php
<?php
class UPImages {
var annexFolder = "annex";//附件存放點(diǎn),默認(rèn)為:annex
var smallFolder = "smallimg";//縮略圖存放路徑,注:必須是放在 annexFolder下的子目錄,默認(rèn)為:smallimg
var markFolder = "mark";//水印圖片存放處
var upFileType = "jpg gif png";//上傳的類型,默認(rèn)為:jpg gif png rar zip
var upFileMax = 1024;//上傳大小限制,單位是“KB”,默認(rèn)為:1024KB
var fontType;//字體
var maxWidth = 500; //圖片最大寬度
var maxHeight = 600; //圖片最大高度
function UPImages(annexFolder,smallFolder,includeFolder) {
this->annexFolder = annexFolder;
this->smallFolder = smallFolder;
this->fontType = includeFolder."/04B_08__.TTF";
}
function upLoad(inputName) {
imageName = time();//設(shè)定當(dāng)前時(shí)間為圖片名稱
if(@empty(_FILES[inputName]["name"])) die(error("沒有上傳圖片信息,請(qǐng)確認(rèn)"));
name = explode(".",_FILES[inputName]["name"]);//將上傳前的文件以“.”分開取得文件類型
imgCount = count(name);//獲得截取的數(shù)量
imgType = name[imgCount-1];//取得文件的類型
if(strpos(this->upFileType,imgType) === false) die(error("上傳文件類型僅支持 ".this->upFileType." 不支持 ".imgType));
photo = imageName.".".imgType;//寫入數(shù)據(jù)庫(kù)的文件名
uploadFile = this->annexFolder."/".photo;//上傳后的文件名稱
upFileok = move_uploaded_file(_FILES[inputName]["tmp_name"],uploadFile);
if(upFileok) {
imgSize = _FILES[inputName]["size"];
kSize = round(imgSize/1024);
if(kSize > (this->upFileMax*1024)) {
@unlink(uploadFile);
die(error("上傳文件超過 ".this->upFileMax."KB"));
}
} else {
die(error("上傳圖片失敗,請(qǐng)確認(rèn)你的上傳文件不超過 upFileMax KB 或上傳時(shí)間超時(shí)"));
}
return photo;
}
function getInfo(photo) {
photo = this->annexFolder."/".photo;
imageInfo = getimagesize(photo);
imgInfo["width"] = imageInfo[0];
imgInfo["height"] = imageInfo[1];
imgInfo["type"] = imageInfo[2];
imgInfo["name"] = basename(photo);
return imgInfo;
}
function smallImg(photo,width=128,height=128) {
imgInfo = this->getInfo(photo);
photo = this->annexFolder."/".photo;//獲得圖片源
newName = substr(imgInfo["name"],0,strrpos(imgInfo["name"], "."))."_thumb.jpg";//新圖片名稱
if(imgInfo["type"] == 1) {
img = imagecreatefromgif(photo);
} elseif(imgInfo["type"] == 2) {
img = imagecreatefromjpeg(photo);
} elseif(imgInfo["type"] == 3) {
img = imagecreatefrompng(photo);
} else {
img = "";
}
if(empty(img)) return False;
width = (width > imgInfo["width"]) ? imgInfo["width"] : width;
height = (height > imgInfo["height"]) ? imgInfo["height"] : height;
srcW = imgInfo["width"];
srcH = imgInfo["height"];
if (srcW * width > srcH * height) {
height = round(srcH * width / srcW);
} else {
width = round(srcW * height / srcH);
}
if (function_exists("imagecreatetruecolor")) {
newImg = imagecreatetruecolor(width, height);
ImageCopyResampled(newImg, img, 0, 0, 0, 0, width, height, imgInfo["width"], imgInfo["height"]);
} else {
newImg = imagecreate(width, height);
ImageCopyResized(newImg, img, 0, 0, 0, 0, width, height, imgInfo["width"], imgInfo["height"]);
}
if (this->toFile) {
if (file_exists(this->annexFolder."/".this->smallFolder."/".newName)) @unlink(this->annexFolder."/".this->smallFolder."/".newName);
ImageJPEG(newImg,this->annexFolder."/".this->smallFolder."/".newName);
return this->annexFolder."/".this->smallFolder."/".newName;
} else {
ImageJPEG(newImg);
}
ImageDestroy(newImg);
ImageDestroy(img);
return newName;
}
function waterMark(photo,text) {
imgInfo = this->getInfo(photo);
photo = this->annexFolder."/".photo;
newName = substr(imgInfo["name"], 0, strrpos(imgInfo["name"], ".")) . "_mark.jpg";
switch (imgInfo["type"]) {
case 1:
img = imagecreatefromgif(photo);
break;
case 2:
img = imagecreatefromjpeg(photo);
break;
case 3:
img = imagecreatefrompng(photo);
break;
default:
return False;
}
if (empty(img)) return False;
width = (this->maxWidth > imgInfo["width"]) ? imgInfo["width"] : this->maxWidth;
height = (this->maxHeight > imgInfo["height"]) ? imgInfo["height"] : this->maxHeight;
srcW = imgInfo["width"];
srcH = imgInfo["height"];
if (srcW * width > srcH * height) {
height = round(srcH * width / srcW);
} else {
width = round(srcW * height / srcH);
}
if (function_exists("imagecreatetruecolor")) {
newImg = imagecreatetruecolor(width, height);
ImageCopyResampled(newImg, img, 0, 0, 0, 0, width, height, imgInfo["width"], imgInfo["height"]);
} else {
newImg = imagecreate(width, height);
ImageCopyResized(newImg, img, 0, 0, 0, 0, width, height, imgInfo["width"], imgInfo["height"]);
}
white = imageColorAllocate(newImg, 255, 255, 255);
black = imageColorAllocate(newImg, 0, 0, 0);
alpha = imageColorAllocateAlpha(newImg, 230, 230, 230, 40);
ImageFilledRectangle(newImg, 0, height-26, width, height, alpha);
ImageFilledRectangle(newImg, 13, height-20, 15, height-7, black);
ImageTTFText(newImg, 4.9, 0, 20, height-14, black, this->fontType, text[0]);
ImageTTFText(newImg, 4.9, 0, 20, height-6, black, this->fontType, text[1]);
if(this->toFile) {
if (file_exists(this->annexFolder."/".this->markFolder."/".newName)) @unlink(this->annexFolder."/".this->markFolder."/".newName);
ImageJPEG(newImg,this->annexFolder."/".this->markFolder."/".newName);
return this->annexFolder."/".this->markFolder."/".newName;
} else {
ImageJPEG(newImg);
}
ImageDestroy(newImg);
ImageDestroy(img);
return newName;
}
}
?>
-------------------------------------------------------------------------
test.php
<?php
annexFolder = "annex";
smallFolder = "smallimg";
markFolder = "mark";
includeFolder = "include";
require("./".includeFolder."/upfile.php");
img = new UPImages(annexFolder,smallFolder,includeFolder);
text = array(www.dwww.cn,"all rights reserved");
if(@_GET["go"]) {
photo = img->upLoad("upfile");
img->maxWidth = img->maxHeight = 350;//設(shè)置生成水印圖像值
img->toFile = true;
newSmallImg = img->smallImg(photo);
newMark = img->waterMark(photo,text);
echo "<img src='".newSmallImg."' border='0'><br><br>";
echo "<img src='".newMark."' border='0'><br><br>";
echo "<a href='./test.php'>繼續(xù)上傳</a>";
} else {
?>
<form method="post" action="./test.php?go=go" enctype="multipart/form-data">
<input type="file" name="upfile"><br><br>
<input type="submit" value="上傳">
</form>
<?php
}
?>
來源:網(wǎng)絡(luò)
分享:初學(xué)PHP指導(dǎo):php.ini 配置詳細(xì)選項(xiàng)php.ini 或 php3.ini 是 PHP 在啟動(dòng)時(shí)會(huì)讀取的配置文件。該文件的存放路徑為 /usr/local/lib/。在 PHP 3.x 版的配置文件為 php3.ini;而在 PHP 4.x 版改為 php.ini。若 PHP 安裝成服務(wù)器的模塊,則在 Web 服務(wù)器啟動(dòng)執(zhí)行時(shí)會(huì)讀取,之后就不再讀取,因此改動(dòng)
- PHPNOW安裝Memcached擴(kuò)展方法詳解
- php記錄頁(yè)面代碼執(zhí)行時(shí)間
- PHP中獎(jiǎng)概率的抽獎(jiǎng)算法程序代碼
- apache設(shè)置靜態(tài)文件緩存方法介紹
- php對(duì)圖像的各種處理函數(shù)代碼小結(jié)
- PHP 關(guān)于訪問控制的和運(yùn)算符優(yōu)先級(jí)介紹
- 關(guān)于PHP語言構(gòu)造器介紹
- php/js獲取客戶端mac地址的實(shí)現(xiàn)代碼
- php5.5新數(shù)組函數(shù)array_column使用
- PHP preg_match的匹配多國(guó)語言的技巧
- php 中序列化和json使用介紹
- php采集文章中的圖片獲取替換到本地
PHP教程Rss訂閱編程教程搜索
PHP教程推薦
- PHP技術(shù):txtSQL安裝手冊(cè)中文版
- PHP程序員不可忽略的幾點(diǎn)精華
- 深入php函數(shù)file_get_contents超時(shí)處理的方法詳解
- 基于PHP生成靜態(tài)頁(yè)的實(shí)現(xiàn)方法
- php+mysql實(shí)現(xiàn)無限級(jí)分類
- 搜索引擎技術(shù)核心揭密(PHP版)
- 用PHP程序?qū)崿F(xiàn)刪除目錄的三種方法實(shí)例
- PHP中::、-gt;、self、$this幾種操作符的區(qū)別介紹
- 如何在PHP中執(zhí)行系統(tǒng)外部命令
- PHP應(yīng)用程序加速探索之簡(jiǎn)介
- 相關(guān)鏈接:
- 教程說明:
PHP教程-PHP上傳自動(dòng)生成縮略圖及水印類(含代碼)
。