1

我有一些乱七八糟的代码,即使我使用 SimpleImage,我知道我可以使用 CodeIgniter 图像类,但是配置有点大,有人可以发布一些优雅和更好的解决方案,这是我现在的代码,我想摆脱 SimpleImage ,并且图像类在控制器中初始化。这是我所拥有的:

// Main config
$config['image_library'] = 'gd2';
$config['maintain_ratio'] = TRUE;
$config['height'] = '1';
$config['master_dim'] = 'width';
$config['overwrite'] = TRUE;

// Resize image with SimpleImage
$novaslika="img/proizvodi/".$last.".jpg";
$image = new SimpleImage();
$image->load($_FILES['slika']['tmp_name']);
$image->resizeToWidth(800);
$image->save($novaslika);

// Create PNG 
$config['source_image'] = $_FILES['maska']['tmp_name'];
$config['width']     = 800;
$config['new_image']    = "./img/proizvodi/".$last."_maska.png";
$this->image_lib->initialize($config);
$this->image_lib->resize(); 

// Create thumb
$config['source_image'] = './img/proizvodi/'.$last.'.jpg';
$config['create_thumb'] = TRUE;
$config['new_image']    = './img/proizvodi/thumbs/'.$last.'_thumb.jpg';
$this->image_lib->initialize($config);
$this->image_lib->resize();
4

1 回答 1

0

你可以这样做:

function index()
{
    $this->load->library('image_lib');

    $a = array(
        'source_image' => 'images/1.jpg',
        'width' => 100,
        'height' => 100,
        'new_image' => 'images/2.jpg',
        'create_thumb' => TRUE,
        'overwrite' => FALSE
    );


    $image = $this->_image_manipulation($a);

    if($image === TRUE)
    {
        echo "IMAGE OK";
    }
    else
    {
        echo $image;
    }

}

private function _image_manipulation($configs = '')
{
    if($configs)
    {
        $config['image_library'] = 'gd2';   //static
        $config['maintain_ratio'] = TRUE;   //static
        $config['master_dim'] = 'width';    //static

        $config['source_image'] = $configs['source_image'];//required

        $config['height']       = (isset($configs['height']))?$configs['height']:NULL;          
        $config['width']        = (isset($configs['width']))?$configs['width']:NULL;

        $config['overwrite']    = (isset($configs['overwrite']))?$configs['overwrite']:NULL;

        $config['new_image']    = (isset($configs['new_image']))?$configs['new_image']:NULL;
        $config['create_thumb'] = (isset($configs['create_thumb']))?$configs['create_thumb']:NULL;

        $this->image_lib->initialize($config);

        if ( ! $this->image_lib->resize())
        {
            return $this->image_lib->display_errors();
        }
        else
        {
            return TRUE;
        }
    }
}

但仍需要 SimpleImage 库转换为 png 的UNLESS,我无法确认,SimpleImage 正在使用 ImageMagick。如果是,则表示它已安装在系统上,您可以更改

$config['image_library'] = 'gd2';

$config['image_library'] = 'ImageMagick';

CodeIgniter 也会为你处理图像转换;您需要做的就是重命名文件:

$a = array(
    'source_image' => 'images/1.jpg',
    'new_image' => 'images/1.png',
);
于 2013-10-07T19:41:51.940 回答