PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, February 12, 2022

[FIXED] Compress image in Codeigniter

 February 12, 2022     codeigniter, php     No comments   

Issue

I am trying to Compress and resize every image uploded. The image is not getting compressed, but its uploading in original dimensions. I want to reduce the size, by setting quality=60 and also set width to 100 to resize the image.

    function save_order()
{
    $data = $this->input->post(array('order_id', 'order_type', 'customer_id', 'order_total', 'payment_received', 'bill_no', 'pickup_date', 'delivery_date', 'pickups', 'designer', 'balance', 'remark', 'bill_img'));
    if($_FILES["bill_img"])
    {
        $ext = pathinfo($_FILES['bill_img']['name'], PATHINFO_EXTENSION);
        $filename_without_extension=explode('.'.$ext,$_FILES['bill_img']['name']);
        $filename1=str_replace(' ','_',$filename_without_extension[0]);
        $final_filename=str_replace('.','_',$filename1);
        $filename=$final_filename.'.'.$ext;
        $folder_name=$data['order_id'];
        $config['upload_path'] ='./uploads/order/'.$folder_name;
        if (!is_dir('./uploads/order/'.$folder_name))
        {mkdir('./uploads/order/'.$folder_name, 0777, TRUE);}
        $config['allowed_types'] = 'jpg|jpeg|png';
        
        $config['file_name']=$filename;
        $config['quality'] = '60%';
        $config['width'] = 100;
        $this->load->library('image_lib', $config);  
        $this->image_lib->resize();
        $this->upload->initialize($config);
       
        $do_upload1 = $this->upload->do_upload('bill_img'); 
        
        if(!empty($do_upload1))
        {$measur1 = 'uploads/order/'.$folder_name.'/'.$filename;
        }
        else{$measur1 = ""; }
    }

    $insert_data = array('order_type' => $data['order_type'], 'customer' => $data['customer_id'], 'pickups' => $data['pickups'],
    'bill_no' => $data['bill_no'], 'pickup_date' => $data['pickup_date'], 'delivery_date' => $data['delivery_date'],
    'designer' => $data['designer'], 'total_amt' => $data['order_total'] + $data['pickups'], 'received_amt' => $data['payment_received'], 'remaining_amt' => $data['balance'], 'remark' => $data['remark'], 'bill_img' => $measur1, 'created_at' => DATE);
    $result = $this->common_model->insert_data('tbl_orders',$insert_data);
    if($result)
    {
        $this->session->set_flashdata('success', 'Order Put Successfully.');
    }
    else{$this->session->set_flashdata('error','Opps.!!, Error While Putting Order.');}
    redirect('manage-orders');
}

Solution

Some things are out of sequence here. First, upload the image, then initialize the resize, then resize

$do_upload1 = $this->upload->do_upload('bill_img'); 
if(!empty($do_upload1)) {
        $measur1 = 'uploads/order/'.$folder_name.'/'.$filename;

        // resize
        $resize_config['image_library'] = 'gd2';
        $resize_config['source_image'] = $do_upload1['file_path']; // full path to the image
        $resize_config['maintain_ratio'] = true;
        $resize_config['width'] = 100;
        $resize_config['quality'] = '60%';
        $this->load->library('image_lib', $resize_config);  
        $this->image_lib->resize();
}


Answered By - Kinglish
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing