Codeignter的图像处理(使用GD库)不会调整某些JPEG图像的大小

时间:2012-05-20 21:49:29

标签: php image codeigniter image-processing file-upload

我有一个上传表格,上面带有图像jpg / gif / etc,我用它上传一些图片(图片格式为Jpg),用于我用于我网站的图库,但遗憾的是它对某些图片效果不佳图像(它使所有东西都变黑,在左上角(0,0)显示调整大小的图像) 我尝试在线调整图像大小,它没有问题,所以我认为问题不在于图像本身,而是在库/配置中。

我的上传和调整图片处理程序代码:

 function upload() {
        //check if admin is logged in.
        if ($this->session->userdata('is_logged') == 1) {
            //loading the configuration for the upload library.
            $config = array();
            $config['upload_path'] = './uploads/';
            $config['allowed_types'] = 'gif|jpg|png';
            $config['max_size'] = '7000';
            $config['max_width'] = '6000';
            $config['max_height'] = '5000';

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

            if (!$this->upload->do_upload()) {
                $data['redirect_msg'] = $this->upload->display_errors() . '<br /> go back to ' . anchor('admin/', 'main page');
                $this->load->view('redirect', $data);
            } else {
                //getting the image's information, in order to resize it.
                $upload_data = $this->upload->data();
                $image_path = $upload_data['full_path'];

                /* loading the configuration for the image manuiplation library. */
                $config['image_library'] = 'gd';
                $config['source_image'] = $image_path;
                $config['width'] = '800';
                $config['height'] = '600';
                $config['maintain_ratio'] = FALSE; //tried setting it to TRUE aswell.
                //loading the library
                $this->load->library('image_lib', $config);
                //starting the resize proccess.
                $this->image_lib->resize();
                //checking if the is an error in the resizing proccess.
                if (!$this->image_lib->resize()) {
                    /* displaying the errors */
                    $data['redirect_msg'] = $this->image_lib->display_errors();
                    $this->load->view('redirect', $data);
                } else {
                    /* show positive message. */
                    $data['redirect_msg'] = 'Upload successful!<br />redirecting to ' . anchor('admin/', 'homepage') . ' in 3 seconds';
                    $this->load->view('redirect', $data);
                    //saving the image name to a database table, so we can retrieve it when needed(for the slideshow).
                    $this->db_model->save_image($upload_data['orig_name']);
                    //doing the redirect.
                    header('refresh:3;url=' . site_url('admin/'));
                }
            }
        } else { //if not logged in, show negative message.
            $data['redirect_msg'] = 'you are not logged in. < br/> login ' . anchor('admin/', 'here');
            $this->load->view('redirect', $data);
        }
    }

1 个答案:

答案 0 :(得分:0)

您正在调整该图像的大小两次:

//starting the resize proccess.
$this->image_lib->resize();
    //checking if the is an error in the resizing proccess.
    if (!$this->image_lib->resize()) {

只做一次:

//starting the resize proccess.            
//checking if the is an error in the resizing proccess.
            if (!$this->image_lib->resize()) {