【问题标题】:Resizing an image from the source and setting the upload path with Codeigniter从源调整图像大小并使用 Codeigniter 设置上传路径
【发布时间】:2012-01-16 16:24:42
【问题描述】:

我正在使用 Codeigniter 构建一个图片库,到目前为止一切顺利,但我有一个关于调整图片大小的小问题。

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

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

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

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

【问题讨论】:

    标签: php codeigniter image-manipulation codeigniter-2


    【解决方案1】:

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

    $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

    【讨论】:

      猜你喜欢
      • 2015-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-04-26
      • 2012-01-10
      • 2017-03-06
      • 2015-07-30
      • 2012-06-26
      相关资源
      最近更新 更多