• 使用ThinkPHP实现生成缩略图及显示


    首先了解父类Image.class.php(ThinkPHP/Library/Think/Image.class.php)中的一些函数

    1:open() 打开被处理的图片

    2:thumb() 生成缩略图 默认1等比缩放  (其中2,3,4,5,6代表的含义参见父类文件Image.class.php)

    3:save() 缩略图到服务器

    生成缩略图步骤分以下四步

    * 1.实例化
    * 2.打开图片open()
    * 3.生成缩略图thumb() 默认等比缩放
    * 4.保存save()

    控制器代码:

    //大图路径(此处大图路径可参考上篇 “使用ThinkPHP实现附件上传”的上传路径
    $bigimg_path = $upload->rootPath.$file_info['savepath'].$file_info['savename'];
    //小图路径
    $smallimg_path = $upload->rootPath.$file_info['savepath'].'small_'.$file_info['savename'];
    
    $img = new ThinkImage();  //实例化
    $img->open($bigimg_path); //打开被处理的图片
    $img->thumb(100,100); //制作缩略图(100*100)
    $img->save($smallimg_path); //保存缩略图到服务器
    
    //把上传好的附件及缩略图存到数据库
    $_POST['goods_big_img']=$bigimg_path;
    $_POST['goods_small_img']=$smallimg_path;    

    入口文件设置常量

    为前台显示缩略图路径方便,在入口文件index.php设置常量,表示站点路径,和数据库存放的图片路径拼接

    define('SITE_URL', 'http://www.tp.com/TP/shop/');

    前台显示大图及缩略图

    <td><!-- 大图 -->
         <img src="<{$smarty.const.SITE_URL}><{$v.goods_big_img}>" height="60" width="60">
    </td>
    <td><!-- 小图 -->
            <img src="<{$smarty.const.SITE_URL}><{$v.goods_small_img}>" height="40" width="40">
    </td>

    结果

    数据库里存放路径

    图片

    前台显示

  • 相关阅读:
    不负时光,不负自己
    理解无偏估计(unbiased estimation)
    Latex Error:‘acmart.cls’ not found 解决方案:
    Dark theme for Texstudio
    马尔可夫毯(Markov Blanket)
    时间复杂度和空间复杂度的简单讲解
    应用层级时空记忆模型(HTM)实现对实时异常流时序数据检测
    ElasticSearch集群状态查看命令大全
    ElasticSearch API 之 UPDATE
    ElasticSearch API 之 DELETE
  • 原文地址:https://www.cnblogs.com/zxf100/p/6774285.html
Copyright © 2020-2023  润新知