在codeigniter中上传图片

时间:2011-05-03 11:05:09

标签: php codeigniter

我想上传三张照片。我的文件夹结构如下: 上传/大,上传/原来,上传/拇指。 如何使用codeigniters的“上传”库调整大小到这些文件夹后存储图像。

2 个答案:

答案 0 :(得分:2)

我同意Natrium,你需要接受更多答案。

然而,对于这个问题: 在我看来,您实际上只是上传到原始文件夹,然后在此副本上使用图像库,因此,请尝试以下操作:

$config['upload_path'] = './upload/original';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width']  = '500';
$config['max_height']  = '300';
$this->load->library('upload', $config);

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

$copies = array(
                array('dir' => 'upload/large', 'x' => 1000, 'y' => 600), //note: x&y could be replaced with a percentage or ratio etc.
                array('dir' => 'upload/thumbnail', 'x' => 100, 'y' => 60)
                );

foreach($copies as $copy)
{
                $config['image_library']  = 'ImageMagick';
                $config['library_path']   = '/usr/bin/';
                $config['source_image']   = $upload_data['full_path'];
                $config['new_image']   = $copy['dir'] . $upload_data['file_name'];
                $config['maintain_ratio'] = TRUE;
                $config['width'] = $copy['x'];
                $config['height'] = $copy['y'];
                $config['master_dim'] = 'width';
                $this->image_lib->initialize($config);
                $this->image_lib->resize();
}

希望这有帮助!

答案 1 :(得分:0)

您应该只更改配置数组中的“upload_path”值。

以下是您可以使用的某种代码:

    $config['upload_path'] = './uploads/';

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

参考用户指南http://codeigniter.com/user_guide/libraries/file_uploading.html