PHP实例:PHP批量生成缩略图_PHP教程

编辑Tag赚U币
教程Tag:暂无Tag,欢迎添加,赚取U币!

推荐:如何避免页面刷新数据重复写入数据库
何避免页面刷新数据重复写入数据库 当表单的数据是提交给本页面处理并写入数据库时,点提交后,刷新页面的话使数据重复写入数据库。网上搜索一下,发现了不少的解决方案: 一、把一页面分开为两个,数据提交给另一个页面处理,之后再跳到输入页面。 优点:避

到今天我学PHP已经快一年了。不过小弟资质相当有限,一直没有做出什么好东西来。在工作中有时需要把大批量的图片变小,这是一件很简单但很无聊的事情。有一天突发奇想,怎么不用PHP来做一个小程序呢?于是昨天完成了一个批量生成缩略图的小程序。现在发布出来,请各位大哥大姐多多批评!
­
1.先来分析一下程序的界面。
界面很简单:首先声明本程序仅支持JPG、GIF、PNG三种图片格式。然后分别指出源文件和缩略图保存的文件夹。接下来可以设置JPG的质量和缩量图的大小。最后是显示所消耗的时间和处理的图片数。
2.完整PHP源程序。源程序很简单,不多做解释。解压后直接放于可运行PHP的WWW目录下既可运行。
­
<?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质量:<span id="spryjpgquality">
<input name="jpgQuality" type="text" id="jpgQuality" value="80" size="3" maxlength="3" />
<span class="textfieldRequiredMsg">请输入JPG质量</span><span class="textfieldInvalidFormatMsg">仅接受整数</span><span class="textfieldMaxValueMsg">请不要输入大于100的整数</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">仅接受整数</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汉字验证码的实现
?php /* * 文件:chinesechar.php * 作用:汉字数据储存 * 作者:PHP实战群:33918040 - 鱼尾唯一 * 网址:http://bbs.ailf.cn/ http://www.fishwei.com/ * 特注:版权所有转载注明出处!有付出才会有收获! */ ChineseChar = array(人,出,来,友,学,孝,

来源:模板无忧//所属分类:PHP教程/更新时间:2010-04-13
相关PHP教程