currently I'm working with codeigniter framework but I'm confused how to resize the image. I have followed the documentation but I'm still confused. This is my code
public function create(){
//Check login
if(!$this->session->userdata('logged_in')){
redirect('users/login');
}
$data['title'] = 'Create Post';
$data['categories'] = $this->BlogModel->get_categories();
$this->form_validation->set_rules('title','title','required');
$this->form_validation->set_rules('content','content','required');
if($this->form_validation->run() === FALSE){
$this->load->view('frontend/header');
$this->load->view('frontend/navbar');
$this->load->view('frontend/blog/create',$data);
$this->load->view('frontend/footer');
} else{
//Upload Image
$config['upload_path'] = './assets/images/posts/blog';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
if(!$this->upload->do_upload()){
$errors = array('error' => $this->upload->display_errors());
$post_image = 'noimage.jpg';
}else{
$data = array('upload_data' => $this->upload->data());
$post_image = $_FILES['userfile']['name'];
}
$this->BlogModel->create_post($post_image);
//set message
$this->session->set_flashdata('post_created','Your post has been created');
redirect('blog');
}
}
Please advice how to resize the image.
Thanks!
If you want to do different things like resize the original image directly, rather than creating a thumb set create_thumb to false, and do not use new_image. The only difference between create_thumb and new_image is that new_image allows you to specify your own path and name. Whereas the most you can do with create_thumb is change the thumb_marker. Which is all in the docs...
//Upload Image
$config['upload_path'] = './assets/images/posts/blog';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '2048';
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
//$errors = array('error' => $this->upload->display_errors());
exit($this->upload->display_errors());
$post_image = 'noimage.jpg';
} else {
$up_data = $this->upload->data();
$post_image = $up_data['name'];
$config = array(); // clear prev config
$config['image_library'] = 'gd2';
$config['source_image'] = $up_data['full_path'];
$config['create_thumb'] = TRUE;
$config['thumb_marker'] = '_thumbnail';
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;
$this->load->library('image_lib', $config);
if(!$this->image_lib->resize()) {
exit($this->image_lib->display_errors());
}
$thumb_path = $up_data['file_path'] . $up_data['raw_name'] . $config['thumb_marker'] . $up_data['file_ext'];
}
The exits you can remove later, but leave them for debugging until you figure out a way to properly message your errors (I suggest session vars). These functions should also be moved to a model.
Related
I'm setting up the upload file before add resize code. And its normally process, but after adding resize code my controller isn't working. Can you help me with this? I'm trying but it didn't work.
And my another problem, I can't upload image file size more than 2MB.
This for my work on online shop.
public function submit_image()
{
$input_name = $_POST['input_name'];
$input_email = $_POST['input_email'];
$input_code_transaction = $_POST['input_code_transaction'];
$config['file_name'] = $input_code_transaction;
$config['overwrite'] = TRUE;
$config['upload_path'] = './img/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = 50000000;
$config['max_width'] = 6000;
$config['max_height'] = 4000;
$this->upload->initialize($config);
if(!empty($_FILES['filefoto']['name'])) {
if ($this->upload->do_upload('doc')) {
$gbr = $this->upload->data();
//Compress Image
$config['image_library']='gd2';
$config['source_image']='./img/'.$gbr['file_name'];
$config['create_thumb']= FALSE;
$config['maintain_ratio']= FALSE;
$config['quality']= '50%';
$config['width']= 1280;
$config['height']= 720;
$config['new_image']= './img/'.$gbr['file_name'];
$this->load->library('upload', $config);
$this->upload->resize();
$data = $upload_data = $this->upload->data();
$input_picture = $this->upload->do_upload('doc');
}
}
}
The upload library has no inbuilt features for image resizing.
This whole section:
$config['image_library']='gd2';
$config['source_image']='./img/'.$gbr['file_name'];
$config['create_thumb']= FALSE;
$config['maintain_ratio']= FALSE;
$config['quality']= '50%';
$config['width']= 1280;
$config['height']= 720;
$config['new_image']= './img/'.$gbr['file_name'];
$this->load->library('upload', $config);
$this->upload->resize();
$data = $upload_data = $this->upload->data();
$input_gambar = $this->upload->do_upload('doc');
needs to reference image_lib the image resizer codeigniter uses: https://www.codeigniter.com/user_guide/libraries/image_lib.html
So:
if ($this->upload->do_upload('doc')) {
$gbr = $this->upload->data();
//Compress Image
$imlib['image_library'] = 'gd2';
$imlib['source_image'] = './img/' . $gbr['file_name'];
$imlib['create_thumb'] = FALSE;
$imlib['maintain_ratio'] = FALSE;
$imlib['quality'] = '50%';
$imlib['width'] = 1280;
$imlib['height'] = 720;
$imlib['new_image'] = './img/resized_' . $gbr['file_name'];
$this->load->library('image_lib', $imlib);
if (!$this->image_lib->resize()) {
show_error($this->image_lib->display_errors());
}
$yourouputimage = './img/resized_' . $gbr['file_name'];
}
I want to upload large images on my website. I want to reduce the size of those using codeigniter. So I am doing this code
function upload_image($data) {
$config['upload_path'] = './temp/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 10000;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('image')) {
$error = array('error' => $this->upload->display_errors());
pre($error);
} else {
$config = array();
$data = array('upload_data' => $this->upload->data());
$config['image_library'] = 'gd';
$config['source_image'] = './temp/' . $data['upload_data']['file_name'];
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['quality'] = 50;
$config['new_image'] = './temp/' . $data['upload_data']['file_name'];
$this->load->library('image_lib', $config);
$this->image_lib->resize();
pre($data);
}
}
But images are not being compressed. Original and uploaded size are the same. Where am I wrong?
Maybe exists an error. Check for errors:
if (!$this->image_lib->resize()){
echo $this->image_lib->display_errors();
}
Note: Use gd2 as library (default value is gd2):
$config['image_library'] = 'gd2';
Codeigniter: File successfully upload when setting only one field(individual). But when setting both field second file is also uploaded to first's location. How to solve this conflict.
Is there any tutorial for multiple upload field????
My code is given below:
<input type="file" name="filePrdimage" id="filePrdimage" size="20"/>
<input type="file" name="filePrdlogo" id="filePrdlogo" size="20"/>
. // Image uploading codes
$config['upload_path'] = 'assets/images/b2bproduct';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '1000';
$config['max_width'] = '2024';
$config['max_height'] = '1768';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
if (isset($_FILES['filePrdimage']['name'])) {
$config['file_name'] = substr(md5(time()), 0, 28) . $_FILES['filePrdimage']['name'];
}
$this->load->library('upload', $config);
if (!$this->upload->do_upload('filePrdimage')) {
//no file uploaded or failed upload
$error = array('error' => $this->upload->display_errors());
} else {
$dat = array('upload_data' => $this->upload->data());
$this->resize($dat['upload_data']['full_path'], 'assets/images/b2bproduct/thump/'.$dat['upload_data']['file_name'],180,400);
}
if (empty($dat['upload_data']['file_name'])) {
$prdimage = $this->input->post('hdPrdimage');
}
else {
$prdimage = $dat['upload_data']['file_name'];
}
// End Image uploading Codes
// Logo uploading codes
$config['upload_path'] = 'assets/images/b2blogo';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '1000';
$config['max_width'] = '2024';
$config['max_height'] = '1768';
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
if (isset($_FILES['filePrdlogo']['name'])) {
$config['file_name'] = substr(md5(time()), 0, 28) . $_FILES['filePrdlogo']['name'];
}
$this->load->library('upload', $config);
if (!$this->upload->do_upload('filePrdlogo')) {
//no file uploaded or failed upload
$error = array('error' => $this->upload->display_errors());
} else {
$dat = array('upload_data' => $this->upload->data());
$this->resize($dat['upload_data']['full_path'], 'assets/images/b2blogo/'.$dat['upload_data']['file_name'],135,300);
}
if (empty($dat['upload_data']['file_name'])) {
$prdlogo= $this->input->post('hdPrdlogo'); ;
}
else {
$prdlogo=$dat['upload_data']['file_name'];
}
// End Logo uploading Codes
public function resize($source,$destination,$width,$height) {
$config['image_library'] = 'gd2';
$config['source_image'] = $source;
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['width'] = $width;
$config['height'] = $height;
$config['new_image'] = $destination;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
}
I think you should do only one function to upload images that has data array as parameter where you may include the path and other variables.
public function upload(array $data)
{
$config['upload_path'] = isset($data['upload_path']) ? $data['upload_path'] : 'default/path';
...
$this->load->library('upload', $config);
if (!$this->upload->do_upload($data['field')) {
//no file uploaded or failed upload
$error = array('error' => $this->upload->display_errors());
} else {
$dat = array('upload_data' => $this->upload->data());
$this->resize($dat['upload_data']['full_path'], $data['upload_path'].$dat['upload_data']['file_name'],135,300);
}
...
}
When you submit the form, check for all input fields with the $_FILES global and call the upload function when necessary with the upload array $data set for each of them.
My image is not get inserted while uploading using codeigniter:
function add_newblog()
{
$sess_id = $this->session->userdata('id');
$result['query'] = $this->login_model->profile($sess_id);
foreach($result['query'] as $row)
{
$email = $row->blogger_email;
$url = $row->blogger_url;
$author = $row->blogger_name;
if ($this->input->post('submit')) {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$error = array('error' => $this->upload->display_errors());
$media = 'No Image Uploaded';
$title = $this->input->post("title");
$category = $this->input->post('category');
$content = $this->input->post('content');
$this->blog_model->add_newblog($sess_id,$title,$category,$content,$media,$email,$url,$author);
$this->session->set_flashdata('valid', 'Blog Created without Image');
redirect('content_writer/add_blog');
}
else {
$data = array('upload_data' => $this->upload->data());
$data = $this->upload->data();
$media = $data['file_name'];
$title = $this->input->post("title");
$category = $this->input->post('category');
$content = $this->input->post('content');
$this->blog_model->add_newblog($sess_id,$title,$category,$content,$media,$email,$url,$author);
$this->session->set_flashdata('valid', 'Blog Created');
redirect('content_writer/add_blog');
}
}
else{
$this->session->set_flashdata('invalid', 'Invalid');
redirect('content_writer/add_blog');
}
}
}
The else condition always works. The image name does not get saved in the image path.
the problem is here in this line
$config['upload_path'] = './uploads/';
instead use
$config['upload_path'] = 'uploads/<folder name>';
OR incase you want to save the image directly in uploads folder just use
$config['upload_path'] = 'uploads/';
as well as make your that you form in the veiw section is open with form_open_multipart()
hope this will solve your problem
Set your max_size, max_width and max_height. This would be an example below.
$config['max_size'] = '3000';
$config['max_width'] = '1500';
$config['max_height'] = '1500';
You havent passed the image name in $this->upload->do_upload() method.
use your
input type='file' name=myDoc
$this->upload->do_upload("myDoc")
I have a problem making thumbs after an image upload. I can upload images perfectly but it doesn't make thumbs for me. I have a map uploads where the original images are stored and a map thumbs in uploads. It's probably a path issue i guess. Thanks
This is my code in my controller.
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '6000';
$config['max_width'] = '3024';
$config['max_height'] = '3768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
}
$datap = array('upload_data' => $this->upload->data());
$gallery_path = realpath(APPPATH . '../uploads');
$upload_data = $this->upload->data();
$config['image_library'] = 'gd2';
$config['source_image'] = $upload_data['full_path'];
$config['new_image'] = $gallery_path . '/thumbs';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;
$this->load->library('image_lib', $config);
$this->image_lib->resize();