如何使用CI3.1.2创建缩略图并将其存储在另一个文件夹中?

时间:2016-11-28 08:48:28

标签: codeigniter

我正在使用CI版本3.1.2来处理图像并使用Windows 8.我的问题是每次处理图像并尝试创建缩略图时,我都在反击消息“无法保存图像请确保图像和文件目录是可写的“。尝试将缩略图副本保存到其他位置时会发生这种情况。以下是处理图像的代码:

$this->original_path = realpath(APPPATH.'../assets/galery/full');

// Uploading image
$config = [
  'allowed_types'   => 'jpg|jpeg|gif|png',
  'upload_path'     => $this->original_path,
  'max_size'        => 2048,
  'maintain_ratio'  => true,
  'file_name'       => 'img_'.strtotime(date('d-m-y-H-i-s')), // miss type
  'overwrite'       => true,
  'remove_spaces'   => true
];
$this->load->library('upload', $config);
$this->upload->do_upload('galery');
$file_name = $this->upload->file_name;

// Processing image, success processing image
$this->load->library('image_lib');
$img = [
  'image_library'   => 'gd2',
  'source_image'    => $this->original_path.'/'.$file_name,
  'maintain_ratio'  => true,
  'width'           => 600
];
$this->image_lib->initialize($img);
$this->image_lib->resize();

// This is fail, with message "Unable to save the image... using(var_dump())
$thumb = [
  'source_image'    => $this->original_path.'/'.$file_name,
  'new_image'       => '/assets/galery/small',
  'maintain_ratio'  => true,
  'width'           => 200
];

$this->image_lib->initialize($thumb);
$this->image_lib->resize();

我尝试了不同的配置,比如使用realpath(APPPATH.'../assets/galery/small'),但仍然没有运气。顺便说一下,使用'create_thumb'创建配置是成功的。 但我的目的是将该缩略图保存在另一个文件夹中。我怎样才能做到这一点?

更新

这种变化似乎运作良好。我将realpath(APPPATH.'../assets/galery/full')更改为realpath(APPPATH.'../assets/galery')并进行了一些更改:

...
'upload_path' => $this->original_path.'/full'
...
'source_image' => $this->original_path.'/full/'.$file_name
...
'new_image' => $this->original_path.'/small/thumb_'.$file_name

到目前为止,我仍然试图让我的代码变得简单并将其放在MY_Model中。感谢所有评论。如果有人可能有更好的解决方案,那么分享一下会很高兴。

0 个答案:

没有答案