调整源图像的大小,并使用Codeigniter设置上传路径


Resizing an image from the source and setting the upload path with Codeigniter

我正在用Codeigniter构建一个图像库,到目前为止一切都很好,但我有一个关于图像大小调整的快速问题。

基本上,我上传源图像,创建一个id,然后将其存储在该路径/uploads/$id/source.jpg中。然后,我尝试创建源图像的两个新图像,一个缩略图和一个带水印的中间图像。

当使用Codeigniter的图像操作类时,你可以设置文件名和上传路径吗?当你给它源图像时,它是修改源图像还是复制?

public function generateThumnail($source, $screenid) {
        $config['image_library'] = 'gd2';
        $config['source_image'] = "$source";
        $config['maintain_ratio'] = TRUE;
        $config['width'] = 75;
        $config['height'] = 50;
        //Not sure how I set the upload path and file name.
        $this->load->library('image_lib', $config); 
        $this->image_lib->resize();
        //TODO: Code to be added later
        $this->image_lib->clear();
}

上传路径和文件名不包括在内,因为我不知道如何添加它们。有什么方法可以添加它们吗?

是的,您可以指定上传路径,但可以使用文件上传类,而不是图像操作类。

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

http://codeigniter.com/user_guide/libraries/file_uploading.html

CodeIgniter将创建一个图像的副本,如果你告诉它。

创建副本

调整大小功能将创建图像文件的副本(以及保留原始文件名),如果使用此偏好:

$config['new_image'] = '/path/to/new_image.jpg';

关于此偏好的注意事项:

  • 如果只指定了新的图像名称,它将被放置在与原始图像相同的文件夹中
  • 如果只指定了路径,则新图像将以与原始图像相同的名称放置在目标中
  • 如果同时指定了路径和图像名称,则它将被放置在自己的目标中,并被赋予新名称

http://codeigniter.com/user_guide/libraries/image_lib.html