• 技术文章 >php教程 >php手册

    PHP批量生成缩略图的代码

    2016-06-13 12:28:00原创343
    缺点:长宽不一的图片会被拉伸变形,不能智能裁切,需要智能裁切的,请自行研究。
    $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 -> $file" . 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: {$config['prefix']}{$file}" . ENDL;
    $done++;
    }
    }
    }
    closedir($handle);
    $op .= "fin -> {$done} file(s) written" . ENDL;
    echo "
    ";  
    echo $op;
    echo "
    ";
    exit;
    ?>
    声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。
    上一篇:使用PHP socke 向指定页面提交数据 下一篇:php 防止单引号,双引号在接受页面转义
    Web大前端开发直播班

    相关文章推荐

    • php实现高效获取图片尺寸的方法• php使用ffmpeg向视频中添加文字字幕的实现方法,• 大型网站带来的问题,大型网站带来问题• PHP实现搜索相似图片,• PHP中字符安全过滤函数使用总结

    全部评论我要评论

  • 取消发布评论发送
  • 1/1

    PHP中文网