PHP實(shí)例:PHP批量生成縮略圖_PHP教程
推薦:如何避免頁面刷新數(shù)據(jù)重復(fù)寫入數(shù)據(jù)庫何避免頁面刷新數(shù)據(jù)重復(fù)寫入數(shù)據(jù)庫 當(dāng)表單的數(shù)據(jù)是提交給本頁面處理并寫入數(shù)據(jù)庫時,點(diǎn)提交后,刷新頁面的話使數(shù)據(jù)重復(fù)寫入數(shù)據(jù)庫。網(wǎng)上搜索一下,發(fā)現(xiàn)了不少的解決方案: 一、把一頁面分開為兩個,數(shù)據(jù)提交給另一個頁面處理,之后再跳到輸入頁面。 優(yōu)點(diǎn):避
到今天我學(xué)PHP已經(jīng)快一年了。不過小弟資質(zhì)相當(dāng)有限,一直沒有做出什么好東西來。在工作中有時需要把大批量的圖片變小,這是一件很簡單但很無聊的事情。有一天突發(fā)奇想,怎么不用PHP來做一個小程序呢?于是昨天完成了一個批量生成縮略圖的小程序�,F(xiàn)在發(fā)布出來,請各位大哥大姐多多批評!
1.先來分析一下程序的界面。
界面很簡單:首先聲明本程序僅支持JPG、GIF、PNG三種圖片格式。然后分別指出源文件和縮略圖保存的文件夾。接下來可以設(shè)置JPG的質(zhì)量和縮量圖的大小。最后是顯示所消耗的時間和處理的圖片數(shù)。
2.完整PHP源程序。源程序很簡單,不多做解釋。解壓后直接放于可運(yùn)行PHP的WWW目錄下既可運(yùn)行。
<?php
function microtimeFloat()
{
list(uSecond, second) = explode(" ", microtime());
return ((float)uSecond + (float)second);
}
dirFlag = pathinfo(_SERVER['scrīpt_FILENAME'],PATHINFO_DIRNAME);
timeStart = microtimeFloat();
sourcesDir = 'sources/';
thumbsDir = 'thumbs/';
jpgQuality = _POST['jpgQuality'];
thumbWidth = _POST['thumbWidth'];
counter = 0;
if(handle = opendir(sourcesDir)) {
while(imageName = readdir(handle)) {
FileExtName = strtolower(pathinfo(imageName, PATHINFO_EXTENSION));
if((FileExtName == 'gif') or (FileExtName == 'jpg') or (FileExtName == 'jpeg') or (FileExtName == 'png')) {
thumbsUrl = thumbsDir.imageName;
list(realWidth, realHeight) = getimagesize(sourcesDir.imageName);
if(realWidth < thumbWidth){
percent = 1;
} else {
percent = thumbWidth / realWidth;
}
newWidth = realWidth * percent;
newHeight = realHeight * percent;
currentThumb = imagecreatetruecolor(newWidth, newHeight);
list(width, height, pictureType, attrib) = getimagesize(sourcesDir.imageName);
switch(pictureType)
{
case "1": source = imagecreatefromgif(sourcesDir.imageName); break;
case "2": source = imagecreatefromjpeg(sourcesDir.imageName); break;
case "3": source = imagecreatefrompng(sourcesDir.imageName); break;
}
imagecopyresized(currentThumb, source, 0, 0, 0, 0, newWidth, newHeight, realWidth, realHeight);
switch(pictureType)
{
case "1": imagegif(currentThumb, thumbsUrl); break;
case "2": imagejpeg(currentThumb, thumbsUrl, jpgQuality); break;
case "3": imagepng(currentThumb, thumbsUrl); break;
}
counter++;
}
}
closedir(handle);
timeEnd = microtimeFloat();
execTime = round(timeEnd - timeStart, 2);
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" c />
<title>批量生成縮略圖</title>
<scrīpt src="css/SpryValidationTextField.js" type="text/javascrīpt"></scrīpt>
<link href="css/autothumbs.css" rel="stylesheet" type="text/css" />
<link href="css/SpryValidationTextField.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="autothumbs" name="autothumbs" method="post" action="<?php echo _SERVER['PHP_SELF']; ?>">
<table width="760" border="0" align="center">
<tr>
<td> </td>
</tr>
<tr>
<td><div align="center" class="titel">批量生成縮略圖</div></td>
</tr>
<tr>
<td>本程序僅支持JPG、GIF、PNG三種圖片格式。</td>
</tr>
<tr>
<td>請將源文件放于:<strong><?php echo dirFlag.'/sources/'; ?></strong></td>
</tr>
<tr>
<td>縮略圖將保存于:<strong><?php echo dirFlag.'/thumbs/'; ?></strong></td>
</tr>
<tr>
<td>JPG質(zhì)量:<span id="spryjpgquality">
<input name="jpgQuality" type="text" id="jpgQuality" value="80" size="3" maxlength="3" />
<span class="textfieldRequiredMsg">請輸入JPG質(zhì)量</span><span class="textfieldInvalidFormatMsg">僅接受整數(shù)</span><span class="textfieldMaxValueMsg">請不要輸入大于100的整數(shù)</span></span> </td>
</tr>
<tr>
<td>縮略圖大小:<span id="sprythumbsize">
<input name="thumbWidth" type="text" id="thumbWidth" value="800" size="4" maxlength="4" />
<span class="textfieldRequiredMsg">請輸入縮略圖大小</span><span class="textfieldInvalidFormatMsg">僅接受整數(shù)</span></span>< /td>
</tr>
<tr>
<td><?php echo "耗時 execTime 秒, 共處理 counter 張圖片"; ?></td>
</tr>
<tr>
<td> </td>
</tr>
<tr>
<td><div align="center">
<input name="thumbs" type="submit" class="button-css" id="thumbs" value="生 成" />
<input name="reset" type="reset" class="button-css" id="reset" value="重 置" />
</div></td>
</tr>
</table>
</form>
<scrīpt type="text/javascrīpt">
<!--
var sprytextfield1 = new Spry.Widget.ValidationTextField("spryjpgquality", "integer", {maxValue:100});
var sprytextfield2 = new Spry.Widget.ValidationTextField("sprythumbsize", "integer");
//-->
</scrīpt>
</body>
</html>
分享:解析PHP漢字驗(yàn)證碼的實(shí)現(xiàn)?php /* * 文件:chinesechar.php * 作用:漢字?jǐn)?shù)據(jù)儲存 * 作者:PHP實(shí)戰(zhàn)群:33918040 - 魚尾唯一 * 網(wǎng)址:http://bbs.ailf.cn/ http://www.fishwei.com/ * 特注:版權(quán)所有轉(zhuǎn)載注明出處!有付出才會有收獲! */ ChineseChar = array(人,出,來,友,學(xué),孝,
- PHPNOW安裝Memcached擴(kuò)展方法詳解
- php記錄頁面代碼執(zhí)行時間
- PHP中獎概率的抽獎算法程序代碼
- apache設(shè)置靜態(tài)文件緩存方法介紹
- php對圖像的各種處理函數(shù)代碼小結(jié)
- PHP 關(guān)于訪問控制的和運(yùn)算符優(yōu)先級介紹
- 關(guān)于PHP語言構(gòu)造器介紹
- php/js獲取客戶端mac地址的實(shí)現(xiàn)代碼
- php5.5新數(shù)組函數(shù)array_column使用
- PHP preg_match的匹配多國語言的技巧
- php 中序列化和json使用介紹
- php采集文章中的圖片獲取替換到本地
- 相關(guān)鏈接:
- 教程說明:
PHP教程-PHP實(shí)例:PHP批量生成縮略圖
。