笨调整图像和创建缩略图(codeigniter resize image and create th

2019-06-26 07:21发布

喜根据CI文件,你可以调整与image_lib图像,并有一些建议,我们可以从图像创建额外的缩略图选项

create_thumb    FALSE   TRUE/FALSE (boolean)    Tells the image processing function to create a thumb.  R
thumb_marker    _thumb  None    Specifies the thumbnail indicator. It will be inserted just before the file extension, so mypic.jpg would become mypic_thumb.jpg    R

所以这里是我的代码

        $config_manip = array(
            'image_library' => 'gd2',
            'source_image'  => "./uploads/avatar/tmp/{$this->input->post('new_val')}",
            'new_image'     => "./uploads/avatar/{$this->input->post('new_val')}",
            'maintain_ratio'=> TRUE ,
            'create_thumb'  => TRUE ,
            'thumb_marker'  => '_thumb' ,
            'width'         => 150,
            'height'        => 150 
        );

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

我会承担这个代码重新调整我的形象,也创建缩略图,但我只得到一个图像与指定的尺寸和_tump后缀

我也一直在努力,将此代码添加到手动创建第二图像,但仍然不工作,我得到的只有一个图像

            $this->image_lib->clear();

$config_manip['new_image'] = 
"./uploads/avatar/thumbnail_{$this->input->post('new_val')}";

            $config_manip['width']     = 30 ;
            $config_manip['height']    = 30 ;
            $this->load->library('image_lib', $config_manip);
            $this->image_lib->resize();

Answer 1:

看来路径是在你的代码的问题。 我修改和测试自己它的工作原理。

public function do_resize()
{
    $filename = $this->input->post('new_val');
    $source_path = $_SERVER['DOCUMENT_ROOT'] . '/uploads/avatar/tmp/' . $filename;
    $target_path = $_SERVER['DOCUMENT_ROOT'] . '/uploads/avatar/';
    $config_manip = array(
        'image_library' => 'gd2',
        'source_image' => $source_path,
        'new_image' => $target_path,
        'maintain_ratio' => TRUE,
        'create_thumb' => TRUE,
        'thumb_marker' => '_thumb',
        'width' => 150,
        'height' => 150
    );
    $this->load->library('image_lib', $config_manip);
    if (!$this->image_lib->resize()) {
        echo $this->image_lib->display_errors();
    }
    // clear //
    $this->image_lib->clear();
}

希望这可以帮助你。 谢谢!!



Answer 2:

您的代码是好的,但你需要做一个小的变化。

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


Answer 3:

一个简单的方法来创建缩略图。

function _create_thumbnail($fileName, $width, $height) 
{
    $this->load->library('image_lib');
    $config['image_library']  = 'gd2';
    $config['source_image']   = $_SERVER['DOCUMENT_ROOT']. $fileName;       
    $config['create_thumb']   = TRUE;
    $config['maintain_ratio'] = TRUE;
    $config['width']          = $width;
    $config['height']         = $height;
    $config['new_image']      = $_SERVER['DOCUMENT_ROOT']. $fileName;               
    $this->image_lib->initialize($config);
    if (! $this->image_lib->resize()) { 
        echo $this->image_lib->display_errors();
    }        
}


Answer 4:

如果你想创建使用一个以上的图像resize()方法,你需要调用$this->image_lib->initialize($config); 每次尝试调整大小。

本教程解决它,我上传图片和创建笨多个缩略图尺寸



文章来源: codeigniter resize image and create thumbnail