Codeigniter图片大小调整?

6

我有一个在CI中使用图像类的问题?

这是一个例子

在控制器上:

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

在视图中,我有这个:

    foreach($results as $row)
        {

$config['image_library'] = 'gd2';
$config['source_image'] = '/img/proizvodi/'.$row->proizvodid.'.jpg';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width']     = 75;
$config['height']   = 50;


$this->load->library('image_lib', $config); 
$this->image_lib->resize();
}

但我没有看到错误,为什么缩略图没有被创建呢?我不知道哪里出了问题。

1
使用此函数检查Image_lib错误:echo $this->image_lib->display_errors(); - Ts8060
7个回答

26

您不应该在foreach中加载image_lib。请尝试使用以下代码:

$this->load->library('image_lib');
foreach($results as $row) {
    $config['image_library'] = 'gd2';
    $config['source_image'] = '/img/proizvodi/'.$row->proizvodid.'.jpg';
    $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();
}

如果不起作用 - 检查上传文件夹的权限。


1
我已经尝试了下面的代码,请检查。
if (isset($_FILES['image']) && !empty($_FILES['image']['name'])) {
    $upload_path="uploads/foldername/";
    $newFileName = explode(".",$_FILES['image']['name']);
    $filename = time()."-".rand(00,99).".".end($newFileName);
    $filename_new = time()."-".rand(00,99)."_new.".end($filename);
    $config['file_name'] = $filename;
    $config['upload_path'] = $upload_path;
    $config['allowed_types'] = 'gif|jpg|png|jpeg'; 
    $this->load->library('upload', $config);

    if ($this->upload->do_upload('image')) 
    { 
    //Image Resizing 
    $config1['source_image'] = $this->upload->upload_path.$this->upload->file_name;
    $config1['new_image'] =  'uploads/foldername/'.$filename_new;
    $config1['maintain_ratio'] = FALSE;
    $config1['width'] = 181;
    $config1['height'] = 181; 
    $this->load->library('image_lib', $config1); 
    if ( ! $this->image_lib->resize()){ 
    $this->session->set_flashdata('message', $this->image_lib->display_errors('', ''));

    }
    $post_data['image'] = $filename;
    }
    else
    { 
    $this->session->set_flashdata('msg',$this->upload->display_errors());
    }
}

1
如果目标图像的宽高比与源图像不同,则源图像将按照与目标图像相同的比例进行裁剪。
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";

}}

我已经准备好了一份关于如何在CodeIgniter中调整图像大小而不失去质量的逐步指南。


1
function uploadimage()
    {

        $file_name                  = $this->input->post('fileName');
        $imageName                  = 'set_'.file_name($_FILES["file"]['name'],date('Ymd_his'));
        $config['image_library']    = 'gd2';
        $config['source_image']     = $imageName;
        $config['create_thumb']     = TRUE;
        $config['maintain_ratio']   = TRUE;
        $config['master_dim']       = 'width'; 
        upload_resize('file','settings', $imageName );
        $config['width']            = 200;
        $config['height']           = 62;

        $this->load->library('image_lib', $config);
        $this->image_lib->initialize($config);
        $this->image_lib->resize();
        $this->image_lib->clear();
        $json_data = array(
                'error'     =>false,
                'tmp_name'  =>$file_name,
                'file_name' =>base_url().'assets/uploads/settings/'.$imageName.'?dummy='.rand(0,1000),
                'file_path' =>$imageName
        );

        echo json_encode($json_data);
    }
}

这段代码会在保存到数据库之前将图片上传到文件夹中。

1
$config['upload_path']          = './berkas/content/';
$config['allowed_types']        = 'gif|jpg|png';
$config['max_size']             = 0;
$config['max_width']            = 1024000;
$config['max_height']           = 7680000;
$config['encrypt_name']           = TRUE;
$config['overwrite']           = FALSE;

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

        $upload_data = $this->upload->data();
        $file_name = $upload_data['file_name'];            
        $params['gambar'] = $file_name;
        $this->load->library('image_lib');
        $config['image_library'] = 'gd2';
        $config['source_image'] = './berkas/content/'.$file_name;
        $config['create_thumb'] = FALSE;
        $config['maintain_ratio'] = TRUE;
        $config['width']     = 550;
        $config['height']   = 350;

$params = array('gambar' => $file_name);

0
问题在于你正在循环内部加载库,这是错误的。如果你编辑10张图片,你将会加载10次库,这是一个大错误。库必须在构造函数中或循环外部加载。
错误的。
    foreach($results as $row)
        {

$config['image_library'] = 'gd2';
$config['source_image'] = '/img/proizvodi/'.$row->proizvodid.'.jpg';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width']     = 75;
$config['height']   = 50;


$this->load->library('image_lib', $config); 
$this->image_lib->resize();
}

正确

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

foreach($results as $row)
        {

$config['image_library'] = 'gd2';
$config['source_image'] = '/img/proizvodi/'.$row->proizvodid.'.jpg';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width']     = 75;
$config['height']   = 50;    


$this->image_lib->initialize($config);
$this->image_lib->resize();
 $this->image_lib->clear();
}

0

希望这个函数能帮助到某些人

function compressimg($gbr,$path){
            $wd =$gbr['image_width']*0.4; //0.4 mean compress to 40% image_size
            $hh =$gbr['image_height']*0.4; //modify yours
            //Compress Image
            $config['image_library']    = 'gd2';
            $config['source_image']     = $path.$gbr['file_name'];
            $config['create_thumb']     = FALSE;
            $config['maintain_ratio']   = FALSE;
            $config['quality']          = '50%';
            $config['width']            = round($wd);
            $config['height']           = round($hh);
            $config['new_image']        = $path.$gbr['file_name'];

            $this->load->library('image_lib', $config);
            $this->image_lib->resize();
            
            return $gbr['full_path'];

            // Put This On Uploader , by user4xn
            // return $this->compressimg($this->upload->data(),$config['upload_path']);
}

虽然这段代码可能回答了问题,但提供有关它如何以及/或为什么解决问题的附加上下文将改善答案的长期价值。 - Vickel

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接