PHP批量生成缩略图
到今天我学PHP已经快一年了。不过小弟资质相当有限,一直没有做出什么好东西来。在工作中有时需要把大批量的图片变小,这是一件很简单但很无聊的事情。有一天突发奇想,怎么不用PHP来做一个小程序呢?于是昨天完成了一个批量生成缩略图的小程序。现在发布出来,请各位大哥大姐多多批评!
??
1.先来分析一下程序的界面。
界面很简单:首先声明本程序仅支持JPG、GIF、PNG三种图片格式。然后分别指出源文件和缩略图保存的文件夹。接下来可以设置JPG的质量和缩量图的大小。最后是显示所消耗的时间和处理的图片数。
2.完整PHP源程序。源程序很简单,不多做解释。解压后直接放于可运行PHP的WWW目录下既可运行。
??
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);
}
?>
批量生成缩略图
href="css/autothumbs.css" rel="stylesheet" type="text/css" />
href="css/SpryValidationTextField.css" rel="stylesheet" type="text/css" />
本文转自
http://blog.youkuaiyun.com/phphot/archive/2007/12/18/1946591.aspx
到今天我学PHP已经快一年了。不过小弟资质相当有限,一直没有做出什么好东西来。在工作中有时需要把大批量的图片变小,这是一件很简单但很无聊的事情。有一天突发奇想,怎么不用PHP来做一个小程序呢?于是昨天完成了一个批量生成缩略图的小程序。现在发布出来,请各位大哥大姐多多批评!
??
1.先来分析一下程序的界面。
界面很简单:首先声明本程序仅支持JPG、GIF、PNG三种图片格式。然后分别指出源文件和缩略图保存的文件夹。接下来可以设置JPG的质量和缩量图的大小。最后是显示所消耗的时间和处理的图片数。
2.完整PHP源程序。源程序很简单,不多做解释。解压后直接放于可运行PHP的WWW目录下既可运行。
??
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);
}
?>
href="css/autothumbs.css" rel="stylesheet" type="text/css" />
href="css/SpryValidationTextField.css" rel="stylesheet" type="text/css" />
本文转自
http://blog.youkuaiyun.com/phphot/archive/2007/12/18/1946591.aspx