Skip to content

Instantly share code, notes, and snippets.

@leizongmin
Created August 23, 2012 05:02
Show Gist options
  • Save leizongmin/3432652 to your computer and use it in GitHub Desktop.
Save leizongmin/3432652 to your computer and use it in GitHub Desktop.
遍历目录并生成缩略图的PHP程序
<?php
/**
* 生成缩略图
*/
function makethumb ($srcFile, $dstFile, $dstW, $dstH) {
$dstW = intval($dstW);
$dstH = intval($dstH);
if($dstW == 0 && $dstH == 0)
return false;
$size = @GetImageSize($srcFile,$info);
$newWidth = $size[0];
$newHeight = $size[1];
if($size[0] > $dstW) {
$newWidth = $dstW;
$newHeight = intval(($newWidth / $size[0]) * $size[1]);
}
if($newHeight > $dstH) {
$newHeight = $dstH;
$newWidth = intval(($newHeight / $size[1]) * $size[0]);
}
if($newWidth == 0 || $newHeight == 0)
return false;
switch ($size[2]) {
case 1:
$im = @ImageCreateFromGIF($srcFile);
break;
case 2:
$im = @imagecreatefromjpeg($srcFile);
break;
case 3:
$im = @ImageCreateFromPNG($srcFile);
break;
}
if($im) {
$srcW= $size[0];
$srcH= $size[1];
$ni=@imagecreatetruecolor($newWidth,$newHeight);
if($ni) {
if(@ImageCopyResized($ni,$im,0,0,0,0,$newWidth,$newHeight,$srcW,$srcH)) {
switch ($size[2]) {
case 1:
return @ImageGif($ni,$dstFile);
case 2:
return @Imagejpeg($ni,$dstFile);
case 3:
return @ImagePng($ni,$dstFile);
}
}
}
}
return false;
}
/**
* 文件类
*/
class TFile {
var $err_msg = "";
var $report = false;
//新建目录
function newdir ($path) {
$tmp = explode("/",$path);
if(count($tmp) > 0) {
for($i=0; $i<count($tmp); $i++) {
@$path1 .= $tmp[$i]."/";
if(!file_exists($path1)) {
if(!mkdir($path1,0777)) {
$this->err_msg = "Create directory $path1 fail!";
$this->error();
}
if(!@chmod($path1,0777)) {
$this->err_msg = "Change directory $path1 attribute fail!";
$this->error();
}
}
}
}
else {
if(!file_exists($path)) {
if(!mkdir($path,0777)) {
$this->err_msg = "Create directory $path fail!";
$this->error();
}
if(!@chmod($path,0777)) {
$this->err_msg = "Change directory $path attribute fail!";
$this->error();
}
}
}
}
//新建立文件
function newfile($filename) {
$fp = @fopen($filename,"wb");
@fclose($fp);
if(!@chmod($filename,0777)) {
$this->err_msg = "Change directory attribute fail!";
$this->error();
}
}
//读文件
function readfile($filename) {
if(!file_exists($filename)) {
$this->err_msg = "File $filename not exists!";
$this->error();
}
$html_mod = file_get_contents($filename);
return $html_mod;
}
//写文件末尾
function appendfile($filename,$append) {
$fp = @fopen($filename,"a");
@fputs($fp,$append);
@fclose($fp);
}
//写文件
function writefile($filename,$content) {
if(empty($filename))
return false;
$this->newdir(substr($filename,0,strrpos($filename,"/")));
$fp = @fopen($filename,"w+b");
@fwrite($fp,$content);
@fclose($fp);
if(!@chmod($filename,0777)) {
$this->err_msg = "Change directory attribute fail!";
$this->error();
}
}
//检查文件是否存在
function checkfile($filename) {
if(!file_exists($filename))
return false;
else
return true;
}
//错误
function error() {
if($this->err_msg == "") $this->err_msg = "Unknow error!";
if($this->report) {
print "<font color=#999999>".$this->err_msg."</font>";
@mysql_close();
exit;
}
}
}
/**
* 写文件内容
*/
function writeFileContent($path,$buffer) {
$fileOpt = new TFile;
$fileOpt->writefile($path,$buffer);
}
function printLine ($str) {
echo "$str\n";
}
/**
* 遍历目录,生成缩略图
*/
function expandDirectory ($spicDir, $dir) {
if(is_dir($dir)) {
if ($dh = opendir($dir)) {
printLine('Expand directory: '.$dir);
while (($file= readdir($dh)) !== false) {
if ($file == '.' || $file == '..')
continue;
$filename = "$dir/$file";
if((is_dir($filename)) && $filename != $spicDir) {
expandDirectory($spicDir, $filename);
}
else {
$spicFilename = "$spicDir/$filename";
writeFileContent($spicFilename, 'SPIC');
$ret = makethumb($filename, $spicFilename, 250, 250);
printLine(' - make thumb '.($ret ? 'success' : 'fail').': '.$spicFilename);
}
}
closedir($dh);
}
}
}
expandDirectory('./spic', '.');
printLine('==============================');
printLine('Finish.');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment