Codeigniter实现多文件上传并创建多个缩略图
网络编程 2021-07-05 09:50www.168986.cn编程入门
这篇文章主要介绍了Codeigniter实现多文件上传并创建多个缩略图,需要的朋友可以参考下
该程序可以实现:
1.上传5张图片
2.生成两种尺寸的缩略图
3.保存到mysql
controllersupload.php文件
代码如下:
<?php
class Upload extends Controller {
function go() {
if(isset($_POST['go'])) {
//初始化
$config['upload_path'] = 'album/source';
$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
$config['encrypt_name'] = TRUE;
$config['remove_spaces'] = TRUE;
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
//170170图片
$configThumb = array();
$configThumb['image_library'] = 'gd2';
$configThumb['source_image'] = '';
$configThumb['create_thumb'] = TRUE;
$configThumb['maintain_ratio'] = TRUE; //保持图片比例
$configThumb['new_image'] = 'album/thumb';
$configThumb['width'] = 170;
$configThumb['height'] = 170;
//600600图片
$configLarge = array();
$configLarge['image_library'] = 'gd2';
$configLarge['source_image'] = '';
$configLarge['create_thumb'] = TRUE;
$configLarge['maintain_ratio'] = TRUE; //保持图片比例
$configLarge['new_image'] = 'album/large';
$configLarge['width'] = 600;
$configLarge['height'] = 600;
$this->load->library('image_lib');
for($i = 1; $i < 6; $i++) {
$upload = $this->upload->do_upload('image'.$i);
if($upload === FALSE) continue;
$data = $this->upload->data();//返回上传文件的所有相关信息的数组
$uid = $this->session->userdata('uid');
$uploadedFiles[$i] = $data;
if($data['is_image'] == 1) {
//初始化170170
$configThumb['source_image'] = $data['full_path']; //文件路径带文件名
$this->image_lib->initialize($configThumb);
$this->image_lib->resize();
//初始化600600
$configLarge['source_image'] = $data['full_path']; //文件路径带文件名
$this->image_lib->initialize($configLarge);
$this->image_lib->resize();
}
//插入图片信息到album表,插入的文件名为source目录文件名
$picture = array(
'filename' => $data['file_name'],
'albumID' => $this->uri->segment(4,0),
'uid' => $this->session->userdata('uid'),
'dateline' => time(),
'describe' => '',
'click' => 0
);
$this->load->model('album_model');
$this->album_model->AddPic($picture);
$picture = array();
}
}
class Upload extends Controller {
function go() {
if(isset($_POST['go'])) {
//初始化
$config['upload_path'] = 'album/source';
$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
$config['encrypt_name'] = TRUE;
$config['remove_spaces'] = TRUE;
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
//170170图片
$configThumb = array();
$configThumb['image_library'] = 'gd2';
$configThumb['source_image'] = '';
$configThumb['create_thumb'] = TRUE;
$configThumb['maintain_ratio'] = TRUE; //保持图片比例
$configThumb['new_image'] = 'album/thumb';
$configThumb['width'] = 170;
$configThumb['height'] = 170;
//600600图片
$configLarge = array();
$configLarge['image_library'] = 'gd2';
$configLarge['source_image'] = '';
$configLarge['create_thumb'] = TRUE;
$configLarge['maintain_ratio'] = TRUE; //保持图片比例
$configLarge['new_image'] = 'album/large';
$configLarge['width'] = 600;
$configLarge['height'] = 600;
$this->load->library('image_lib');
for($i = 1; $i < 6; $i++) {
$upload = $this->upload->do_upload('image'.$i);
if($upload === FALSE) continue;
$data = $this->upload->data();//返回上传文件的所有相关信息的数组
$uid = $this->session->userdata('uid');
$uploadedFiles[$i] = $data;
if($data['is_image'] == 1) {
//初始化170170
$configThumb['source_image'] = $data['full_path']; //文件路径带文件名
$this->image_lib->initialize($configThumb);
$this->image_lib->resize();
//初始化600600
$configLarge['source_image'] = $data['full_path']; //文件路径带文件名
$this->image_lib->initialize($configLarge);
$this->image_lib->resize();
}
//插入图片信息到album表,插入的文件名为source目录文件名
$picture = array(
'filename' => $data['file_name'],
'albumID' => $this->uri->segment(4,0),
'uid' => $this->session->userdata('uid'),
'dateline' => time(),
'describe' => '',
'click' => 0
);
$this->load->model('album_model');
$this->album_model->AddPic($picture);
$picture = array();
}
}
/ 转出 /
$albumID = $this->uri->segment(4);
$backurl = site_url() . 'photo/editpic/album/' .$albumID;
$this->session->set_flashdata('msg','图片上传成功.');
redirect($backurl,'refresh');
}
}
views:new_pic.view文件
代码如下:
<form method="post" action="<?php echo site_url() ?>photo/upload/go/<?php echo $albumID ?>" enctype="multipart/form-data">
<input type="file" name="image1" class="files"/><br />
<input type="file" name="image2" class="files"/><br />
<input type="file" name="image3" class="files"/><br />
<input type="file" name="image4" class="files"/><br />
<input type="file" name="image5" class="files"/><br />
<br />
<p><input type="submit" name="go" value="上传照片" class="button" /></p>
</form>
<input type="file" name="image1" class="files"/><br />
<input type="file" name="image2" class="files"/><br />
<input type="file" name="image3" class="files"/><br />
<input type="file" name="image4" class="files"/><br />
<input type="file" name="image5" class="files"/><br />
<br />
<p><input type="submit" name="go" value="上传照片" class="button" /></p>
</form>
需要注意
1.要一次上传几个文件,修改表单和控制器中循环部分的参数就好。
2.album\\source 是上传后原图目录 large和thumb分别是两次执行$this->image_lib->resize();后存放缩略图的目录
3.缩略图文件名如需和album\\source目录一致,请添加参数 $config['thumb_marker'] = '';
4.$picture这部分数组是保存到数据库的东西,可以不用管了。
编程语言
- 如何快速学会编程 如何快速学会ug编程
- 免费学编程的app 推荐12个免费学编程的好网站
- 电脑怎么编程:电脑怎么编程网咯游戏菜单图标
- 如何写代码新手教学 如何写代码新手教学手机
- 基础编程入门教程视频 基础编程入门教程视频华
- 编程演示:编程演示浦丰投针过程
- 乐高编程加盟 乐高积木编程加盟
- 跟我学plc编程 plc编程自学入门视频教程
- ug编程成航林总 ug编程实战视频
- 孩子学编程的好处和坏处
- 初学者学编程该从哪里开始 新手学编程从哪里入
- 慢走丝编程 慢走丝编程难学吗
- 国内十强少儿编程机构 中国少儿编程机构十强有
- 成人计算机速成培训班 成人计算机速成培训班办
- 孩子学编程网上课程哪家好 儿童学编程比较好的
- 代码编程教学入门软件 代码编程教程