S
S
semki0962016-03-21 17:57:08
CodeIgniter
semki096, 2016-03-21 17:57:08

How to combine crop and resize function?

How to reduce and crop a photo (Codeigniter) - combining the crop and resize functions. Now I just resize the photo

$config['image_library'] = 'gd2';
                        $config['source_image'] = $this->upload->upload_path . $this->upload->file_name;
                        $config['new_image'] = './uploads/avatar_mini/';
                        $config['maintain_ratio'] = TRUE; 
                        $config['create_thumb'] = false;
                        $config['width'] = 50; 
                        $config['height'] = 50;
                        
                        $this->image_lib->initialize($config);
                        $this->image_lib->resize();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Svet Valentin, 2017-02-11
@walyk

function your_function() { 

$this->upload->initialize($config);    
$this->load->library('upload');
$this->load->library('image_lib');

if(!$this->upload->do_upload())
{
   $error = array('error' => $this->upload->display_errors());
   $this->load->view('submit', $error);
}    
else 
{
   $data['upload_data'] = array('upload_data' => $this->upload->data());
   $file_name = $this->upload->file_name;

   list($image_width, $image_height) = getimagesize($_SERVER['DOCUMENT_ROOT'].'/website/uploads/original/'.$file_name);

    $this->image_resize('115', '181', 'small', $file_name, $image_width, $image_height);    
    $this->image_resize('300', '400', 'medium', $file_name, $image_width, $image_height);
    $this->image_resize('600', '500', 'large', $file_name, $image_width, $image_height);        
}
}

private function image_resize($height, $width, $path, $file_name, $image_width, $image_height) 
{
    // Resize image settings
    $config['image_library'] = 'GD2';
    $config['source_image'] = $_SERVER['DOCUMENT_ROOT'].'/website/uploads/original/'.$file_name;
    $config['new_image'] = $_SERVER['DOCUMENT_ROOT']."/website/uploads/$path/$file_name";
    $config['maintain_ratio'] = TRUE;
    $config['width'] = $width;
    $config['height'] = $height;
    $config['master_dim'] = 'width';

    $this->image_lib->initialize($config);

    if($image_width >= $config['width'] AND $image_height >= $config['height'])
    {
        if (!$this->image_lib->resize())
        {
            echo $this->image_lib->display_errors();
        } else {
            if(file_exists($_SERVER['DOCUMENT_ROOT']."/website/uploads/$path/$file_name")) 
            {
                list($image_width, $image_height) = getimagesize($_SERVER['DOCUMENT_ROOT']."/website/uploads/$path$file_name");
                if($image_height > '115')
                { 
                    $config['source_image'] = $_SERVER['DOCUMENT_ROOT'].'/website/uploads/small/'.$file_name;
                    $y_axis = $image_height - 115;
                    $config['y_axis'] = $y_axis;
                    $config['x_axis'] = 181;
                    $this->image_lib->initialize($config);
                    if (!$this->image_lib->crop()){
                      echo $this->image_lib->display_errors();
                    } else {
                      echo "cropped";    
                    }
                }
            }       
        }
    }
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question