PHP批量生成縮略圖的代碼
更新時(shí)間:2008年07月19日 19:38:30 作者:
本文件可以用于自動(dòng)創(chuàng)建目錄下所有JPG圖片的縮略圖,放到圖片目錄下運(yùn)行即可。
缺點(diǎn):長(zhǎng)寬不一的圖片會(huì)被拉伸變形,不能智能裁切,需要智能裁切的,請(qǐng)自行研究。
<?php
$config = array();
$config['path'] = "./";
$config['t_width'] = 120;
$config['t_height'] = 98;
$config['ignore'] = array("",".","..");
$config['prefix'] = "thumb_";
$done = 0;
define("IMAGE_JPG", 2);
define("ENDL", "\n");
if($handle = opendir($config['path'])) {
while(false !== ($file = readdir($handle))) {
if(!array_search($file,$config['ignore'])) {
list($im_width, $im_height, $type) = getimagesize($file);
if($type != IMAGE_JPG) {
continue;
}
$op .= "found -> <a href='{$file}'>$file</a>" . ENDL;
$im = @imagecreatefromjpeg($file);
if(!$im) {
$op .= "fail -> couldn't create sour image pointer." . ENDL;
continue;
}
if(file_exists($config['prefix'] . $file) || substr($file, 0, strlen($config['prefix'])) == $config['prefix']) {
$op .= "note -> this file has already got a thumbnail." . ENDL;
continue;
}
$to = imagecreatetruecolor($config['t_width'],$config['t_height']);
if(!$to) {
$op .= "fail -> couldn't create dest image pointer." . ENDL;
continue;
}
if(!imagecopyresampled($to, $im, 0, 0, 0, 0, $config['t_width'], $config['t_height'], $im_width, $im_height)) {
$op .= "fail -> couldn't create thumbnail. php fail." . ENDL;
continue;
}
//保存文件
imagejpeg($to, $config['prefix'] . $file);
$op .= "done -> created thumb: <a href='{$config['prefix']}{$file}'>{$config['prefix']}{$file}</a>" . ENDL;
$done++;
}
}
}
closedir($handle);
$op .= "fin -> {$done} file(s) written" . ENDL;
echo "<pre>";
echo $op;
echo "</pre>";
exit;
?>
<?php
$config = array();
$config['path'] = "./";
$config['t_width'] = 120;
$config['t_height'] = 98;
$config['ignore'] = array("",".","..");
$config['prefix'] = "thumb_";
$done = 0;
define("IMAGE_JPG", 2);
define("ENDL", "\n");
if($handle = opendir($config['path'])) {
while(false !== ($file = readdir($handle))) {
if(!array_search($file,$config['ignore'])) {
list($im_width, $im_height, $type) = getimagesize($file);
if($type != IMAGE_JPG) {
continue;
}
$op .= "found -> <a href='{$file}'>$file</a>" . ENDL;
$im = @imagecreatefromjpeg($file);
if(!$im) {
$op .= "fail -> couldn't create sour image pointer." . ENDL;
continue;
}
if(file_exists($config['prefix'] . $file) || substr($file, 0, strlen($config['prefix'])) == $config['prefix']) {
$op .= "note -> this file has already got a thumbnail." . ENDL;
continue;
}
$to = imagecreatetruecolor($config['t_width'],$config['t_height']);
if(!$to) {
$op .= "fail -> couldn't create dest image pointer." . ENDL;
continue;
}
if(!imagecopyresampled($to, $im, 0, 0, 0, 0, $config['t_width'], $config['t_height'], $im_width, $im_height)) {
$op .= "fail -> couldn't create thumbnail. php fail." . ENDL;
continue;
}
//保存文件
imagejpeg($to, $config['prefix'] . $file);
$op .= "done -> created thumb: <a href='{$config['prefix']}{$file}'>{$config['prefix']}{$file}</a>" . ENDL;
$done++;
}
}
}
closedir($handle);
$op .= "fin -> {$done} file(s) written" . ENDL;
echo "<pre>";
echo $op;
echo "</pre>";
exit;
?>
相關(guān)文章
Apache2中實(shí)現(xiàn)多網(wǎng)站域名綁定的實(shí)現(xiàn)方法
Windows下Apache2不同域名解析不同目錄實(shí)現(xiàn)方法 ,需要的朋友可以參考下2011-06-06刪除PHP數(shù)組中的重復(fù)元素的實(shí)現(xiàn)代碼
本文我們講述通過(guò) array_unique()函數(shù)刪除數(shù)組中重復(fù)元素,需要的朋友可以參考下2017-04-04創(chuàng)造世界上最簡(jiǎn)單的 PHP 開(kāi)發(fā)模式
這篇文章主要介紹了創(chuàng)造世界上最簡(jiǎn)單的 PHP 開(kāi)發(fā)模式2006-12-12PHP通過(guò)COM使用ADODB的簡(jiǎn)單例子
PHP通過(guò)COM使用ADODB的簡(jiǎn)單例子...2006-12-12PHP4實(shí)際應(yīng)用經(jīng)驗(yàn)篇(6)
PHP4實(shí)際應(yīng)用經(jīng)驗(yàn)篇(6)...2006-10-10