分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 教程案例

ci上传图片

发布时间:2023-09-06 01:55责任编辑:蔡小小关键词:上传图片

o_upload.php

<?php/** * Created by PhpStorm. * User: brady * Date: 2018/3/15 * Time: 14:10 */class O_upload extends MY_Model{ ???protected $table = ‘‘; ???public function __construct() ???{ ???????parent::__construct(); ???} ???/** ????* @param $dir 图片路径,以upload下面为准 ?比如 admin ?前后都不要 / ????* @param $base64_img ?base64位的图片 ????* @param int $width ?缩略图片 ????* @return string ????* @throws Exception ????*/ ???public function do_upload($dir,$base64_img,$width=300) ???{ ???????$up_dir = ‘./upload/‘.$dir."/";//存放在当前目录的upload文件夹下 ???????if(!file_exists($up_dir)){ ???????????mkdir($up_dir,0777); ???????} ???????if(preg_match(‘/^(data:\s*image\/(\w+);base64,)/‘, $base64_img, $result)){ ???????????$type = $result[2]; ???????????if(in_array($type,array(‘pjpeg‘,‘jpeg‘,‘jpg‘,‘gif‘,‘bmp‘,‘png‘))){ ???????????????$new_file = $up_dir.date(‘YmdHis_‘).rand(1000,9999).‘.‘.$type; ???????????????if(file_put_contents($new_file, base64_decode(str_replace($result[1], ‘‘, $base64_img)))){ ???????????????????$last_file = $this->resize_img($new_file,$width); ???????????????????unlink($new_file); ???????????????????$img_path = substr($last_file,1); ???????????????????return $img_path; ???????????????}else{ ???????????????????throw new Exception("图片上传失败"); ???????????????} ???????????}else{ ???????????????//文件类型错误 ???????????????throw new Exception(‘图片上传类型错误‘); ???????????} ???????}else{ ???????????//文件错误 ???????????throw new Exception("文件错误"); ???????} ???} ???public function resize_img($file,$size) ???{ ???????$config[‘image_library‘] = ‘gd2‘; ???????$config[‘source_image‘] = $file; ???????$config[‘create_thumb‘] = TRUE; ???????$config[‘thumb_marker‘] = "_".$size; ???????$config[‘maintain_ratio‘] = TRUE; ???????$config[‘width‘] ????= $size; ???????$this->load->library(‘image_lib‘, $config); ???????$this->image_lib->resize(); ???????if ( ! $this->image_lib->resize()){ ???????????throw new Exception($this->image_lib->display_errors()); ???????} else { ???????????$type = pathinfo($file,PATHINFO_EXTENSION ); ???????????$len = strlen($type) + 1; ???????????$last_file = substr($file,0,-$len); ???????????return $last_file."_".$size.".".$type; ???????} ???}}

 image.php 

<?phpclass Images extends MY_Controller{ ???public function __construct() ???{ ???????parent::__construct(); ???} ???public function upload() ???{ ???????$this->load->model("O_upload"); ???????try{ ???????????$img_path = $this->O_upload->do_upload(‘article_list‘); ???????????$this->success_response("上传成功"); ???????}catch(Exception $e){ ???????????$this->error_response($e->getMessage()); ???????} ???} ???/** ????* 上传头像 ????*/ ???public function upload_face() ???{ ???}}

ci上传图片

原文地址:https://www.cnblogs.com/php-linux/p/9058604.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved