Upload Resize Image CI - php

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'];
}

Related

Resize images in codeginiter

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.

Codeigniter image compression not working

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';

Conflict when uploading images with multiple file field in codeigniter

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.

codeigniter image resize only working with px on $config_img

I'm trying to upload an image and everything is working fine, except the width and heigh of the resize. the strange part is, if I add px on the $config_img['width'] and $config_img['height'] I got an error saing:
"A non well formed numeric value encountered
Filename: libraries/Image_lib.php"
What is obvious because the px on the value, but this way the image do resize.
Here is my upload image function:
function upload_foto_acompanhe_sua_obra($field) {
$dir = realpath('assets/uploads/acompanhe_sua_obra');
$config['upload_path'] = $dir;
$config['allowed_types'] = 'gif|jpg|png';
$config['encrypt_name'] = TRUE;
$config['max_size'] = '500000';
$config['max_width'] = '10024';
$config['max_height'] = '7068';
$this->load->library('upload', $config);
$this->upload->initialize($config);
$field_name = $field;
if ($this->upload->do_upload($field_name)) {
$dados = $this->upload->data();
$this->load->library('image_lib');
$this->image_lib->clear();
$size = getimagesize($dados['full_path']);
$config_img['image_library'] = 'GD2';
$config_img['source_image'] = $dados['full_path'];
$config_img['create_thumb'] = FALSE;
$config_img['maintain_ratio'] = TRUE;
$config_img['encrypt_name'] = TRUE;
$config_img['width'] = '1092px';
$config_img['height'] = '295px';
$this->image_lib->initialize($config_img);
$this->image_lib->resize();
// Returns the photo name
return $dados['file_name'];
} else {
$error = array('error' => $this->upload->display_errors());
return $error;
}
}
That's it, any help will be appreciated! Thanks!

codeigniter thumbnail created but original image problem

I have the code below, if i don't use watermark it's fine but when i use watermark, both the original pics and thumbnail become very small and , watermark happens on thumbnail, I want watermark on the original image, pls help
//UPLOAD IMAGE
//some $config vars for image
$config['upload_path'] = './images/blog';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '0';
$config['remove_spaces'] = true;
$config['overwrite'] = false;
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
//upload main image
if(!$this->upload->do_upload('photo')){
$e = $this->upload->display_errors();
print_r($e);
}
$image = $this->upload->data();
//thumbnail creation start
$config1['image_library'] = 'gd2';
$config1['source_image'] = $image['full_path'];
$config1['create_thumb'] = TRUE;
$config1['maintain_ratio'] = TRUE;
$config1['width'] = 75;
$config1['height'] = 50;
$this->load->library('image_lib', $config1);
$this->image_lib->resize();
//thumbnail creation ends
$this->image_lib->clear();
//$image = $this->upload->data();
//start watermarking
$config2['source_image'] = $image['full_path'];
$config2['wm_text'] = 'Copyright 2011 myself';
$config2['wm_type'] = 'text';
$config2['wm_font_path'] = './system/fonts/FromWhereYouAre.ttf';
$config2['wm_font_size'] = '16';
$config2['wm_font_color'] = 'ffffff';
$config2['wm_vrt_alignment'] = 'middle';
$config2['wm_hor_alignment'] = 'center';
$config2['wm_padding'] = '20';
$this->load->library('image_lib');
$this->image_lib->initialize($config);
$this->image_lib->watermark();
//end watermarking
please try adding
$config2['new_image'] = '';
in the watermarking code

Categories