分享一段改变图像大小和生成缩略图的代码
参考别人的经验写的一段代码,首先需要在 _construct中载入类库
$this->load->library('image_lib'); //加载图片处理库
$this->load->library('upload'); //加载文件上传类
然后使用:
/*
* 图片上传
* @param --表单里面的文件域名称
* return --上传文件的所有相关信息的数组
*/
function _upload_img($file_area){
$config['upload_path'] = './uploads/id_card/big/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '2048';
$config['max_width'] = '0';
$config['max_height'] = '0';
$config['encrypt_name'] = TRUE;//允许重命名
$this->upload->initialize($config);
if(!$this->upload->do_upload($file_area)){
$error = array('message' => 'failed at uplaod',
'error' => $this->upload->display_errors());
echo json_encode($error);
return;
}else {
$file_info = $this->upload->data();
$this->_create_thumbnail($file_info['file_name'],$file_info['image_width'],$file_info['image_height']);
$this->_img_resize($file_info['file_name'],$file_info['image_width'],$file_info['image_height']);
//$data_img['goods_image'] = $file_info['file_name'];
//$data['thumb_image'] = 'thumb/'.$file_info['raw_name'] . '_thumb' . $file_info['file_ext'];
return $file_info;
}
}
里面调用的两个函数
/*
* 修改图片大小
* @param --文件名称
* @param --文件宽度
* @param --文件高度
*/
function _img_resize($file_name,$image_width,$image_height)
{
$set_width = 800;
$set_height = 600;
$config['image_library'] = 'gd2';
$config['source_image'] = './uploads/id_card/big/'. $file_name;
$config['new_image'] = './uploads/id_card/big/'. $file_name;
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
if($image_width > $set_width || $image_height > $set_height){
$config['width'] = $set_width;
$config['height'] = $set_height;
}
//$this->load->library('image_lib', $config);
$this->image_lib->initialize($config);
if(!$this->image_lib->resize()){
$error = array('message' => 'failed at resize',
'error' => $this->upload->display_errors());
echo json_encode($error);
return;
}
unset($config);
$this->image_lib->clear();
return TRUE;
}
/*
* 生成缩略图
* @param --文件名称
* @param --文件宽度
* @param --文件高度
*/
function _create_thumbnail($file_name,$image_width,$image_height)
{
$set_thumb_width = 90;
$set_thumb_height = 60;
$config['image_library'] = 'gd2';
$config['source_image'] = './uploads/id_card/big/' . $file_name;
$config['new_image'] = './uploads/id_card/' . $file_name;
$config['create_thumb'] = FALSE;
$config['thumb_marker'] ='';
$config['thumb_marker'] = "zodo_thumb";
$config['maintain_ratio'] = TRUE;
if($image_width > $set_thumb_width or $image_height > $set_thumb_height){
$config['width'] = $set_thumb_width;
$config['height'] = $set_thumb_height;
}
//$this->load->library('image_lib', $config);
$this->image_lib->initialize($config);
if(!$this->image_lib->resize()){
$error = array('message' => 'failed at create_thumbnail',
'error' => $this->upload->display_errors());
echo json_encode($error);
return;
}
//unset($config);
$this->image_lib->clear();
return TRUE;
}
我们是通过私有方法调用的,需要时改下 试试!!!!!!:P 好{:1_1:} 正需要,谢谢! 我用过一个类,生成的缩略图挺不错,哈哈 我就是照这样写的为什么不能实现调整图像大小啊:'(
页:
[1]