Codeigniter image compression not working - php

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

Related

How to flip image using php in CodeIgniter

I am trying to do flip operation in CodeIgniter
I have found a code for image flip in php using codeigniter libraries
First I uploaded image to a path, here is the coding of controller for this purpose:
$config['upload_path'] = 'D:/xampp/htdocs/ImageTools/assets/images/pages/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('image_lib');
$this->image_lib->initialize($config);
$this->image_lib->clear();
$this->load->library('upload',$config);
if(!$this->upload->do_upload()){
$errors = array('error' => $this->upload->display_errors());
$post_image='noimage.jpg';
}else{
$file_data = $this->upload->data();
$data['img'] = base_url().'assets/images/pages/'.$file_data['file_name'];
$this->load->view('pages/inverted',$data);
I tried to do the invert operation here but couldn't succeeded then tried to invert the uploaded image, done the coding in view file by passing this uploaded image to view:
<?php $img;?>
<?php
$this->image_lib->clear();
$config=array();
$config['image_library'] = 'gd2';
$config['source_image'] = $img;
$config['create_thumb'] = TRUE;
$config['rotation_angle'] = 'hor';
$this->load->library('image_lib');
$this->image_lib->initialize($config);
$this->load->library('upload',$config);
$this->image_lib->rotate();
if ( ! $this->image_lib->rotate())
{
echo $this->image_lib->display_errors();
}
if(!$this->upload->do_upload()){
$errors = array('error' => $this->upload->display_errors());
$post_image='noimage.jpg';
}
else{
$file_data = $this->upload->data();
$data = base_url().'/assets/images/pages/'.$file_data['file_name'];
}
?>
First off D:/xampp/htdocs/ImageTools/assets/images/pages/ will never be valid in a server environment. There is a way to make it valid for all environments like so:
$config['upload_path'] = './ImageTools/assets/images/pages/';
(there are other ways but this is the one in the CI docs)
If the folder ImageTools is actually imagetools then that needs to be used in the above example; Linux is case sensitive.
Therefore, following the docs, you can do:
$upload['upload_path'] = './ImageTools/assets/images/pages/';
$upload['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $upload);
if (!$this->upload->do_upload('userfile')) {
show_error($this->upload->display_errors());
}
$this->load->library('image_lib');
$config['image_library'] = 'gd2';
$config['source_image'] = $this->upload->data('full_path');
$config['rotation_angle'] = 'hor'; // or whatever
$this->image_lib->initialize($config);
if (!$this->image_lib->rotate()) {
show_error($this->image_lib->display_errors());
}
$img_path = base_url() . '/assets/images/pages/' . $this->upload->data('file_name');
echo "<img src='{$img_path}' width='auto' height='200'>";

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.

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 file resizing on upload

I've been trying to upload and resize an image. The upload seems to be finally be working, but it's not resizing.
In my constructor I am loading the library
$this->load->library('image_lib');
and then in my function I'm calling the the resize and upload
$this->require_auth();
$img = $this->input->post('photo1');
$config['upload_path'] = './HTML/files/';
$config['file_name'] = 'photo1.jpg';
$config['file_path'] = './HTML/files/photo1.jpg';
$config['max-size'] = 2000;
$config['image_library'] = 'gd2';
$config['source_image'] = $config['file_path'];
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = FALSE;
$config['width'] = 549;
$config['height'] = 549;
$config['overwrite'] = TRUE;
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['new_image'] = $config['file_path'];
$this->load->library('image_lib', $config);
$this->image_lib->resize();
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('photo1')){
$error = array('error' => $this->upload->display_errors());
var_dump($error);
}else{
$this->blog_model->editServiceImg1($config['file_path']);
redirect('/blog');
}
I have also tried this
$this->require_auth();
$img = $this->input->post('service_photo1');
$config['upload_path'] = './HTML/files/';
$config['file_name'] = 'service_photo1.jpg';
$config['file_path'] = './HTML/files/service_photo1.jpg';
$config['max-size'] = 2000;
$config['overwrite'] = TRUE;
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('service_photo1')){
$error = array('error' => $this->upload->display_errors());
var_dump($error);
}else{
$config['image_library'] = 'gd2';
$config['source_image'] = $config['file_path'];
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = FALSE;
$config['width'] = 549;
$config['height'] = 549;
$config['new_image'] = $config['file_path'];
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->blog_model->editServiceImg1($config['file_path']);
redirect('/blog');
}
Is there something I'm missing? I've tried doing the resize after the initial upload as well and that seemed to do nothing as well. Any help would be much appreciated.
$this->load->library('image_lib');
$config['image_library'] = 'gd2';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['upload_path'] = './HTML/files/';
$config['file_path'] = './HTML/files/photo1.jpg';
$config['source_image'] = $config['file_path'];
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 75;
$config['height'] = 50;
$this->image_lib->clear();
$this->image_lib->initialize($config);
$this->image_lib->resize();
If the above code won't work then please check the folder permissions where the images are upload.
You can also give 777 permission to the folder in following manner :-
sudo chmod -R 777 path
Solution will surely help you
I went through this code many times and finally resolved this issue. It seems that when you have
$config['create_thumb'] = TRUE;
in your code it will create a thumbnail with the resized proportions and if you do not tell it where to go it will hide it in a folder. So the resizing was working, just not on the right image.
To resolve this, I changed the above to.
$config['create_thumb'] = FALSE;

CodeIgniter: Making thumbs

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();

Categories