モデルは、モデル
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();
}*/
}
}
にこのコードを置くフォルダにImage_modelとしてモデル名を作成し、このような機能コントローラは、ここで
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');
}
コードを試すコントローラで呼び出し、あなたのために働いているかどうか私に教えてください。 –
ファイルアップロードが正常に動作していますが、サイズを変更するとエラーが発生します。「画像へのパスが正しくありません。サーバーはこのタイプの画像を処理するために必要なGD機能をサポートしていません。 –
サーバのGDライブラリが有効かどうかを調べる\ –