源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

PHP批量生成缩略图的代码

  • 时间:2021-09-13 02:02 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:PHP批量生成缩略图的代码
缺点:长宽不一的图片会被拉伸变形,不能智能裁切,需要智能裁切的,请自行研究。 <?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;  ?>
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部