多个图像在Codeigniter中一次调整大小,将不工作 [英] Multiple Image Resizes at once in Codeigniter , won't work

查看:84
本文介绍了多个图像在Codeigniter中一次调整大小,将不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在同一功能中上传图片并在不同尺寸重新调整大小。但是会发生什么,只有一个重新调整大小工作,其他的不。我的代码是:

  function do_upload()
{
$ this_user = $ this-> auth - > info; if(!is_dir('./ uploads /'.$ this_user-> username)){
mkdir('./ uploads /'.$ this_user-> username);
mkdir('./ uploads /'.$ this_user-> username。'/ photos');
mkdir('./ uploads /'.$ this_user-> username。'/ photos / master');
mkdir('./ uploads /'.$ this_user-> username。'/ photos / small');
mkdir('./ uploads /'.$ this_user-> username。'/ photos / medium');
mkdir('./ uploads /'.$ this_user-> username。'/ photos / large');
mkdir('./ uploads /'.$ this_user-> username。'/ photos / xlarge');
}
$ config ['upload_path'] ='./uploads/'.$this_user->username.'/photos/master/';
$ config ['allowed_types'] ='gif | jpg';

$ title = $ this-> input-> post('title');
$ this-> load-> library('upload',$ config);
if(!$ this-> upload-> do_upload())
{
echo'< div id =status> error< / div>';
echo'< div id =message>'。 $ this-> upload-> display_errors()。'< / div>';
}
else
{
$ data = array('upload_data'=> $ this-> upload-> data());
//调整大小开始
$ image_width = $ data ['upload_data'] ['image_width'];
$ image_height = $ data ['upload_data'] ['image_height'];
$ full_path = $ data ['upload_data'] ['full_path'];
//检查宽度
if($ image_width> 5000){
$ config ['image_library'] ='gd2';
$ config ['source_image'] = $ full_path;
// $ config ['create_thumb'] = TRUE;
$ config ['maintain_ratio'] = TRUE;
$ config ['width'] = 5000;
// $ config ['height'] = 50;
$ config ['new_image'] ='./uploads/'.$this_user->username.'/photos/xlarge';
$ this-> load-> library('image_lib',$ config);
$ this-> image_lib-> resize();
}
if($ image_width> = 4500){
$ config ['image_library'] ='gd2';
$ config ['source_image'] = $ full_path;
// $ config ['create_thumb'] = TRUE;
$ config ['maintain_ratio'] = TRUE;
$ config ['width'] = 4500;
// $ config ['height'] = 50;
$ config ['new_image'] ='./uploads/'.$this_user->username.'/photos/large';
$ this-> load-> library('image_lib',$ config);
$ this-> image_lib-> resize();
}
if($ image_width> = 2000){
$ config ['image_library'] ='gd2';
$ config ['source_image'] = $ full_path;
// $ config ['create_thumb'] = TRUE;
$ config ['maintain_ratio'] = TRUE;
$ config ['width'] = 2000;
// $ config ['height'] = 50;
$ config ['new_image'] ='./uploads/'.$this_user->username.'/photos/medium';
$ this-> load-> library('image_lib',$ config);
$ this-> image_lib-> resize();
}
if($ image_width> = 800){
$ config ['image_library'] ='gd2';
$ config ['source_image'] = $ full_path;
// $ config ['create_thumb'] = TRUE;
$ config ['maintain_ratio'] = TRUE;
$ config ['width'] = 800;
// $ config ['height'] = 50;
$ config ['new_image'] ='./uploads/'.$this_user->username.'/photos/small';
$ this-> load-> library('image_lib',$ config);
$ this-> image_lib-> resize();
}
// resizing ends
echo'< div id =status> success< / div>';
//然后输出您的消息(可选)
echo'< div id =message>'。 $ data ['upload_data'] ['file_name']。$ this-> input-> post('type')。已成功上传。< / div>
//将数据传递给js
echo'< div id =upload_data>'。 json_encode($ data)。 '< / div>';

}
}

解决方案

您需要确保调用 $ this-> image_lib-> clear (); ,因为它重置了图像处理类的初始化。请参阅: http://codeigniter.com/user_guide/libraries/image_lib.html p>

I am trying to upload images and re-size them in different dimensions within the same function. but what happens is that only one re-size works and the others don't . My code is:

    function do_upload()
{
    $this_user = $this->auth->info;if(!is_dir('./uploads/'.$this_user->username)){
        mkdir('./uploads/'.$this_user->username);
        mkdir('./uploads/'.$this_user->username.'/photos');
        mkdir('./uploads/'.$this_user->username.'/photos/master');
        mkdir('./uploads/'.$this_user->username.'/photos/small');
        mkdir('./uploads/'.$this_user->username.'/photos/medium');
        mkdir('./uploads/'.$this_user->username.'/photos/large');
        mkdir('./uploads/'.$this_user->username.'/photos/xlarge');
    }
    $config['upload_path'] = './uploads/'.$this_user->username.'/photos/master/';
    $config['allowed_types'] = 'gif|jpg';

    $title = $this->input->post('title');
    $this->load->library('upload', $config);
    if ( ! $this->upload->do_upload())
    {
        echo '<div id="status">error</div>';
        echo '<div id="message">'. $this->upload->display_errors() .'</div>';
    }
    else
    {
        $data = array('upload_data' => $this->upload->data());
        //resizing begins
            $image_width = $data['upload_data']['image_width'];
            $image_height = $data['upload_data']['image_height'];
            $full_path = $data['upload_data']['full_path'];
            //checking for width
            if($image_width>5000){
                $config['image_library'] = 'gd2';
                $config['source_image'] = $full_path;
                //$config['create_thumb'] = TRUE;
                $config['maintain_ratio'] = TRUE;
                $config['width'] = 5000;
                //$config['height'] = 50;
                $config['new_image'] = './uploads/'.$this_user->username.'/photos/xlarge';
                $this->load->library('image_lib', $config);
                $this->image_lib->resize();
            }
            if($image_width>=4500){
                $config['image_library'] = 'gd2';
                $config['source_image'] = $full_path;
                //$config['create_thumb'] = TRUE;
                $config['maintain_ratio'] = TRUE;
                $config['width'] = 4500;
                //$config['height'] = 50;
                $config['new_image'] = './uploads/'.$this_user->username.'/photos/large';
                $this->load->library('image_lib', $config);
                $this->image_lib->resize();
            }
            if($image_width>=2000){
                $config['image_library'] = 'gd2';
                $config['source_image'] = $full_path;
                //$config['create_thumb'] = TRUE;
                $config['maintain_ratio'] = TRUE;
                $config['width'] = 2000;
                //$config['height'] = 50;
                $config['new_image'] = './uploads/'.$this_user->username.'/photos/medium';
                $this->load->library('image_lib', $config);
                $this->image_lib->resize();
            }
            if($image_width>=800){
                $config['image_library'] = 'gd2';
                $config['source_image'] = $full_path;
                //$config['create_thumb'] = TRUE;
                $config['maintain_ratio'] = TRUE;
                $config['width'] = 800;
                //$config['height'] = 50;
                $config['new_image'] = './uploads/'.$this_user->username.'/photos/small';
                $this->load->library('image_lib', $config);
                $this->image_lib->resize();
            }
        //resizing ends
        echo '<div id="status">success</div>';
        //then output your message (optional)
        echo '<div id="message">'. $data['upload_data']['file_name'].$this->input->post('type').' Successfully uploaded.</div>';
        //pass the data to js
        echo '<div id="upload_data">'. json_encode($data) . '</div>';

    }
}

What Am I doing wrong here?

解决方案

You will need to make sure that you call $this->image_lib->clear(); as it resets the initialization of the image manipulation class. See: http://codeigniter.com/user_guide/libraries/image_lib.html

这篇关于多个图像在Codeigniter中一次调整大小,将不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆