PHP批量生成缩略图的代码

所属分类: 网络编程 / PHP编程 阅读数: 2032
收藏 0 赞 0 分享
缺点:长宽不一的图片会被拉伸变形,不能智能裁切,需要智能裁切的,请自行研究。
<?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 setTime 设置当前时间的代码

在用JAVA中有个 Calendar 可设置当前时间,在PHP中找了半天,终于给我找到了这个函数,嘎嘎
收藏 0 赞 0 分享

PHP 图片水印类代码

这个类功能很强大,细节很棒!希望大家喜欢,并积极指点
收藏 0 赞 0 分享

PHP实现手机归属地查询API接口实现代码

主要使用curl实现,需要开启php对curl的支持
收藏 0 赞 0 分享

php 解决旧系统 查出所有数据分页的类

不同之处在于 没有实现分页的系统, 默认全部查出来 现在就要不能动后台的基础上进行操作 可以采用 相应的 如下 代码
收藏 0 赞 0 分享

PHP的简易冒泡法代码分享

基础的东西,感觉代码还不够简洁,希望高手指导修改,脚本之家特为大家多准备了几个,方便测试学习
收藏 0 赞 0 分享

PHP 利用AJAX获取网页并输出的实现代码(Zjmainstay)

PHP 利用AJAX获取网页并输出的实现代码,需要的朋友可以参考下
收藏 0 赞 0 分享

php数组一对一替换实现代码

以下方法能实现匹配关键词并分别对关键词做特殊处理的功能,需要的朋友可以参考下
收藏 0 赞 0 分享

关于UEditor编辑器远程图片上传失败的解决办法

因为单纯喜欢 UEditor 的界面,于是把项目中原先的编辑器进行的替换,但在后续操作中发现一些问题,就是远程图片上传
收藏 0 赞 0 分享

Windows下部署Apache+PHP+MySQL运行环境实战

本来嘛,部署PHP没什么复杂,找各种版本着实头疼了一下。
收藏 0 赞 0 分享

比较discuz和ecshop的截取字符串函数php版

网上看到一篇文章 discuz和ecshop截取字符串的两个函数,比较了一下两个版本的函数,都各有局限,只能在特定的前提下使用,但是学习一下有利于拓宽思路,了解PHP的扩展功能
收藏 0 赞 0 分享
查看更多