在codeigniter中生成图像缩略图

时间:2012-05-24 19:46:50

标签: php codeigniter

我对codeigniter很新,只是用它构建我的第一个应用程序,但是当我从图像生成缩略图时,我很难过。

图片上传正确,但拇指没有生成,我没有错误:(

我希望有人可以伸出援助之手,我很有可能只是一只山雀,而且它很简单,就像拼写错误一样。

以下是我的图像模型的代码:

<?php

class Image_model extends CI_Model {

        var $image_path;

        function Image_model(){

            parent::__construct();

            $this->image_path = realpath(APPPATH.'../'.$this->config->item('dir_dynamic_images'));

        }

        function do_upload(){

            $config = array(
                    'allowed_types' => "jpeg|gif|jpg|png",
                    'upload_path' => $this->image_path,
                    'max_size' => 2000
            );

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

            $this->upload->do_upload();
            $image_data = $this->upload->data();



            $config = array(
                'source_image' => $image_data['full_path'],
                'new_image' => $this->image_path . '/thumbs',
                'maintain_ratio' => true,
                'width' => 200,
                'height' => 200
            );

            echo $config['new_image'];

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

        }               

    }
?>

2 个答案:

答案 0 :(得分:1)

我认为您需要将create_thumb参数设置为true并指定图像库 -

 $config = array(
                'image_library' => 'gd2',
                'source_image' => $image_data['full_path'],
                'create_thumb' => true,
                'new_image' => $this->image_path . '/thumbs',
                'maintain_ratio' => true,
                'width' => 200,
                'height' => 200
            );

尝试使用 -

查找错误
if(!$this->image_lib->resize()) 
{ 
   echo $this->image_lib->display_errors(); 
} 

答案 1 :(得分:0)

这段代码对我有用。希望它对你有用。您还可以在codeigniter中检查图像处理助手。别忘了初始化配置。

 $config['image_library'] = 'imagemagick';
    $config['library_path'] = '/usr/X11R6/bin/';
    $config['source_image'] = '/path/to/image/mypic.jpg';
    $config['x_axis'] = '100';
    $config['y_axis'] = '60';

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

    if ( ! $this->image_lib->crop())
    {
        echo $this->image_lib->display_errors();
    }