4

我在尝试调整图像大小然后创建拇指时遇到问题。

这两个函数是分开工作的,但是当试图同时调用它们时,只有第一个调用有效。没有创建拇指,为什么?

这是我的控制器:

if($this->upload->do_upload()){

                // Si oui, tout va bien

                //Update DB
                $pictureData = $this->upload->data();
                if($pictureData['file_name']!=''){
                    //On resize l'immage
                        //On charge la librarie
                        $this->load->library('thumbs');

                        $this->thumbs->resize($pictureData['full_path'], 300, 600);

                    $News['image']=$pictureData['file_name'];

                    //On crée un Thumbnail:
                        // on envoi le full path
                        if($this->thumbs->create($pictureData['full_path']))

                        // on stock le path recu
                    $News['thumb'] = $pictureData['raw_name'].'_thumb'.$pictureData['file_ext'];
                }
            }

这是我为这两个函数创建的库:

   <?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Thumbs
{

    function __construct()
    {
        $CI =& get_instance();
        $this->load->library('image_lib');
    }

    public function create($picPath) {

        $CI =& get_instance();

        $config['image_library'] = 'gd2';
        $config['source_image'] = $picPath;
        $config['maintain_ratio'] = TRUE;
        $config['create_thumb'] = TRUE;
        $config['width']     = 100;
        $config['height']   = 100;

        $CI->image_lib->clear();
        $CI->image_lib->initialize($config); 

        if(!$CI->image_lib->resize()){
            $CI->session->set_flashdata('flashError',  $CI->image_lib->display_errors());
            return False;
        }
        $CI->image_lib->clear();
        Return True;

    }

    public function resize($picPath, $x, $y) {

        $CI =& get_instance();

        $config1['image_library'] = 'gd2';
        $config1['source_image']    = $picPath;
        $config1['maintain_ratio'] = TRUE;

        if($x!=0)
            $config1['width']    = $x;

        if($y!=0)
            $config1['height']  = $y;

        $CI->image_lib->clear();
        $CI->image_lib->initialize($config1); 

        if(!$CI->image_lib->resize()){
            $CI->session->set_flashdata('flashError',  $CI->image_lib->display_errors());
            return False;
        }
        $CI->image_lib->clear();
        Return True;
    }
}

因此,如果我只调用一个,它可以正常工作,无论是创建还是调整大小功能。否则,如果我尝试同时调用它们,则只有已调用的第一个函数有效。

编辑:感谢 fccotech,我做到了。这是他带来的解决方案:

<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Thumbs
{

    function __construct()
    {
        $this->CI =& get_instance();
        $this->CI->load->library('image_lib');
    }

    public function create($picPath, $picName) {

        $config =array();

        $config['image_library'] = 'gd2';
        $config['source_image'] = $picPath;
        $config['maintain_ratio'] = TRUE;
        $config['create_thumb'] = TRUE;
        $config['width']     = 100;
        $config['height']   = 100;
        $config['new_image'] =  $picName;

        // $CI->image_lib->clear();
        $this->CI->image_lib->initialize($config); 

        if(!$this->CI->image_lib->resize()){
            $this->CI->session->set_flashdata('flashError',  $this->CI->image_lib->display_errors());
            return False;
        }
        $this->CI->image_lib->clear();
        Return True;

    }

    public function resize($picPath, $x, $y) {

        $config1 = Array();

        $config1['image_library'] = 'gd2';
        $config1['source_image']    = $picPath;
        $config1['maintain_ratio'] = TRUE;

        if($x!=0)
            $config1['width']    = $x;

        if($y!=0)
            $config1['height']  = $y;

        // $CI->image_lib->clear();
        $this->CI->image_lib->initialize($config1); 

        if(!$this->CI->image_lib->resize()){
            $this->CI->session->set_flashdata('flashError',  $this->CI->image_lib->display_errors());
            $this->CI->image_lib->clear();
            return False;
        }
        $this->CI->image_lib->clear();
        Return True;
    }
}
4

1 回答 1

7

调整大小后添加

$this->image_lib->clear();

所以在你做一个新的 $CI->image_lib->initialize($config); 之前也要清楚。

于 2012-10-25T17:07:17.807 回答