Codeigniter image resize?

前端 未结 6 1423
故里飘歌
故里飘歌 2020-11-28 14:43

I have problem with image class in CI?

This is example

On controller:

$this->load->library( array(\'image_lib\') );

O

6条回答
  •  情歌与酒
    2020-11-28 15:16

    If the destination image contains a different aspect ratio than the source image, then the source image will be cropped in the same ratio of the destination image have.

    public function resize_image($image_data){
    $this->load->library('image_lib');
    $w = $image_data['image_width']; // original image's width
    $h = $image_data['image_height']; // original images's height
    
    $n_w = 273; // destination image's width
    $n_h = 246; // destination image's height
    
    $source_ratio = $w / $h;
    $new_ratio = $n_w / $n_h;
    if($source_ratio != $new_ratio){
    
        $config['image_library'] = 'gd2';
        $config['source_image'] = './uploads/uploaded_image.jpg';
        $config['maintain_ratio'] = FALSE;
        if($new_ratio > $source_ratio || (($new_ratio == 1) && ($source_ratio < 1))){
            $config['width'] = $w;
            $config['height'] = round($w/$new_ratio);
            $config['y_axis'] = round(($h - $config['height'])/2);
            $config['x_axis'] = 0;
    
        } else {
    
            $config['width'] = round($h * $new_ratio);
            $config['height'] = $h;
            $size_config['x_axis'] = round(($w - $config['width'])/2);
            $size_config['y_axis'] = 0;
    
        }
    
        $this->image_lib->initialize($config);
        $this->image_lib->crop();
        $this->image_lib->clear();
    }
    $config['image_library'] = 'gd2';
    $config['source_image'] = './uploads/uploaded_image.jpg';
    $config['new_image'] = './uploads/new/resized_image.jpg';
    $config['maintain_ratio'] = TRUE;
    $config['width'] = $n_w;
    $config['height'] = $n_h;
    $this->image_lib->initialize($config);
    
    if (!$this->image_lib->resize()){
    
        echo $this->image_lib->display_errors();
    
    } else {
    
        echo "done";
    
    }}
    

    I have prepared a step by step guide on how to resize image in codeigniter without loosing quality

提交回复
热议问题