THinkPHP上传图片及生成缩略图

版本为:3.2.3


代码如下:

if(empty($_FILES['article_thumb']['tmp_name'])){
    //echo "<script>alert('您没有选择文件');history.back(-1);</script>";exit;
}else{
    //上传图片
    $root_path = ROOT_PATH . 'www/home/data/upload/images';
    if (!is_dir($root_path)) {
        mkdir($root_path);
        chmod($root_path, 0777);
    }
    $upload = new \Think\Upload();// 实例化上传类
    $upload->maxSize = 15000000;// 设置附件上传大小 15M
    $upload->exts = array('jpeg', 'jpg','gif','png');// 设置附件上传类型
    $upload->rootPath = $root_path . '/'; // 设置附件上传根目录
    $upload->savePath = ''; // 设置附件上传(子)目录
    $upload->subName = '';
    $upload->saveName = 'uniqid';
    $info = $upload->upload();
    if (!$info) {
        echo "<script>alert('".$upload->getError()."');history.back(-1);</script>";exit;
    }
    //处理缩略图
    $image_path=$root_path.'/'.$info['article_thumb']['savename'];
    $small_image_path=substr($image_path,0,-4).'_small.'.$info['article_thumb']['ext'];
    $small_image_url='/data/upload/images/'.substr($info['article_thumb']['savename'],0,-4).'_small.'.$info['article_thumb']['ext'];
    $large_image_path=substr($image_path,0,-4).'_large.'.$info['article_thumb']['ext'];
    $large_image_url='/data/upload/images/'.substr($info['article_thumb']['savename'],0,-4).'_large.'.$info['article_thumb']['ext'];
    $image = new \Think\Image();
    //切小图
    $image->open($image_path);
    //设置缩略图最大宽度、高度
    $small_image_max_width=40;
    $small_image_max_height=40;
    $image->thumb($small_image_max_width, $small_image_max_height)->save($small_image_path);
    //切大图
    $image->open($image_path);
    $large_image_max_width=375;
    $large_image_max_height=667;
    $image->thumb($large_image_max_width, $large_image_max_height)->save($large_image_path);
    //删除原图
    unlink($image_path);
    $arr['article_thumb']=$small_image_url;
    $arr['article_image']=$large_image_url;
}


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值