codeigniter调整大小图像并创建缩略图

问题描述:

hi根据ci文档,您可以使用image_lib调整图片大小,并且有一些选项建议我们可以从该图片创建其他缩略图。

hi according to the ci document you can resize images with image_lib and there are options that suggest we can create additional thumbnail from that image

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 postfix

i would assume this code resizes my image and also creates a thumbnail , but i only get one image with specified dimensions and _tump postfix

我也试图添加这个代码来手动创建第二个图像,但仍然无法正常工作,我只得到一个图像

i've also tried to add this code to create second image manually but still it doesn't work and i get only one image

            $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();


似乎path是你的代码中的问题。我修改并测试自己的工作原理。

It seems path is the issue in your code. I modified and tested myself it works.

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();
}

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

Hope this helps you. Thanks!!