下面是代码,当我尝试使用宽度小于1000px的图像时工作正常,但是当我尝试使用大于1000px的图像时,它无法正常工作。
$img_array = array();
$img_array['image_library'] = 'gd2';
$img_array['maintain_ratio'] = TRUE;
$img_array['create_thumb'] = TRUE;
$thumb_name = $img_array['new_image'] = './public/image/thumb/' . $file;
//you need this setting to tell the image lib which image to process
$img_array['source_image'] = $path;
$img_array['width'] = 180;
$img_array['height'] = 250;
$this->image_lib->clear(); // added this line
$this->image_lib->initialize($img_array); // added this line
if (!$this->image_lib->resize())
{
echo $this->image_lib->display_errors(); exit;
}
return $thumb_name;
}
答案 0 :(得分:0)
在Models文件夹中将模型名称创建为Image_model将此代码放入模型中
class Image_model extends CI_Model {
public function __construct() {
parent::__construct();
$this->load->helper('url');
$this->load->library('upload');
$this->load->library('image_lib');
}
public function do_resize($filename)
{
$source_path = 'uploads/' . $filename;
//create a directory thumb in uploads set as target path
$target_path = 'uploads/thumb/thumb_'.$filename;
$config_manip = array(
'image_library' => 'gd2',
'source_image' => $source_path,
'new_image' => $target_path,
'maintain_ratio' => TRUE,
'width' => 180,
'height' => 250
);
$this->image_lib->initialize($config_manip);
$this->load->library('image_lib', $config_manip);
if (!$this->image_lib->resize()) {
echo $this->image_lib->display_errors();
die();
}
// clear //
//$this->image_lib->clear();
}
public function img_upload()
{
$config = array(
'upload_path' => "uploads",
'allowed_types' => "*",
'overwrite' => TRUE,
'max_size' => "5048000", // Can be set to particular file size , here it is 2 MB(2048 Kb)
'max_height' => "3000",
'max_width' => "3000"
);
$this->upload->initialize($config);
$this->load->library('upload', $config);
if($this->upload->do_upload()) {
$response = array('upload_data' => $this->upload->data());
//here calling the function do_resize
$this->do_resize($response['upload_data']['file_name']);
//return $response;
}
/*else{
$error = array('error'=>$this->upload->display_errors());
//print_r($error);die();
}*/
}
}
并像这样调用功能控制器这里是控制器
class your_controller extends CI_Controller {
function __construct(){
parent::__construct();
$this->load->model('image_model');
}
// here the function to save name in database
public function add() {
$data = array();
$config = array();
if(isset($_FILES)){
$config = $this->image_model->img_upload();
$file_data = $this->upload->data();
$data['image'] = $file_data['file_name'];
}
else
$data['image'] = "no-image";
// here the function in your model to save the name of image in your database
$last_id = $this->your_model->save($data);
$this->load->view('your_view');
}