2

我找不到任何关于此的文档。我正在上传图片并使用

$config['create_thumb'] = TRUE; 

创建拇指并保留原始图像。

有没有办法获取拇指图像的文件名?_thumb会自动添加到缩略图的名称中,但没有提取全名的功能。

4

4 回答 4

9

确实有一种更简单的方法可以做到这一点:

if ($this->upload->do_upload())  // If file was uploaded
{           
    $data = $this->upload->data(); // Returns information about your uploaded file.
    $thumbnail = $data['raw_name'].'_thumb'.$data['file_ext']; // Here it is
}
于 2012-07-10T08:35:36.300 回答
3

CodeIgniter 不提供任何函数来提取缩略图名称。它将在您的文件名中添加_thumb。如果您想编写自定义函数来获取缩略图名称,请使用它。

function generate_thumb($filename, $path = '')
{
    // if path is not given use default path //
    if (!$path) {
        $path = FCPATH . 'somedir' . DIRECTORY_SEPARATOR . 'images' . DIRECTORY_SEPARATOR;
    }

    $config['image_library'] = 'gd2';
    $config['source_image'] = $path . $filename;
    $config['create_thumb'] = TRUE;
    $config['maintain_ratio'] = TRUE;
    $config['width'] = 75;
    $config['height'] = 50;

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

    if (!$this->image_lib->resize()) {
        echo $this->image_lib->display_errors();
        return FALSE;
    }
    // get file extension //
    preg_match('/(?<extension>\.\w+)$/im', $filename, $matches);
    $extension = $matches['extension'];
    // thumbnail //
    $thumbnail = preg_replace('/(\.\w+)$/im', '', $filename) . '_thumb' . $extension;
    return $thumbnail;
}

输入 :

echo generate_thumb('someimage.jpg');
echo generate_thumb('other_image.png', FCPATH . 'dirname' . DIRECTORY_SEPARATOR);

输出 :

someimage_thumb.jpg
other_image_thumb.png

希望这对您有所帮助。谢谢!!

于 2012-07-10T06:13:41.770 回答
1
function general_thumbnail_image($source_image_path, $width, $height){

    $thumbnail_config['image_library'] = 'gd2';
    $thumbnail_config['source_image'] = $source_image_path;
    $thumbnail_config['thumb_marker'] = '_'.$width.'x'.$height;
    $thumbnail_config['create_thumb'] = TRUE;
    $thumbnail_config['maintain_ratio'] = TRUE;
    $thumbnail_config['width'] = $width;
    $thumbnail_config['height'] = $height;


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

    if($this->image_lib->resize()){
        $result['status'] = True;

        //If you need complete base path of the thumbnail.
        $result['thumbnail_base_path'] = $this->image_lib->full_dst_path;

        //If you just need name of the thumbnail.
        $source_image_name = $this->image_lib->source_image;
        $extension = strrchr($source_image_name , '.');
        $name = substr($source_image_name , 0, -strlen($extension));
        $result['thumbnail_image_name'] = $name.$config['thumb_marker'].$extension;

        //If you need path similar to source image path.
        $source_image_path = $source_image_path;
        $extension = strrchr($source_image_path , '.');
        $name = substr($source_image_path , 0, -strlen($extension));
        $result['thumbnail_image_path'] = $name.$config['thumb_marker'].$extension;

    }
    else{
        $result['status'] = false;
        $result['error'] = $this->image_lib->display_errors('', '');
    }
    return $result;
}

调用函数。

$thumbnail_result = $this->generate_thumbnail_image('./assests/images/apple.jpg', 180, 180);
print_r($thumbnail_result);

输出将如下所示:

    Array
   (
       [status] => 1
       [thumbnail_base_path] => D:/xampp/htdocs/project_name/assets/images/apple_180x180.jpg
       [thumbnail_image_name] => apple_180x180.jpg
       [thumbnail_image_path] => ./assets/images/apple_180x180.jpg 
   )
于 2017-10-05T08:56:48.750 回答
0

我认为您应该创建一个函数来生成拇指图像,如下所示:

    function generateThumb($fileName) {
    $config['image_library'] = 'gd2';
    $config['source_image'] = './upload/images/clca/' . $fileName;
    $config['create_thumb'] = TRUE;
    $config['maintain_ratio'] = TRUE;
    $config['width'] = 90;
    $config['height'] = 90;
    var file = "";
    $this->load->library('image_lib', $config);
    if(!$this->image_lib->resize()) {
        echo $this->image_lib->display_errors();
    } else {
        // Get File Name
        var file = $fileName."_thumb";
    }        
}

要调用该函数,首先您必须上传文件,获取文件名并调用该函数->$this->generateThumb($file_resp['file_name']);

在您上传的文件夹中,将有 2 张图像(原始图像和生成的带有 _thumb 后缀的拇指图像)

为了获取文件名,我使用“if”条件来确保生成拇指图像没有任何错误,然后加入$fileName+“_thumb”。我使用这种方式是因为我检查了$this->image_lib对象,但没有任何对生成的拇指图像文件名的引用。

编辑:

  • 抱歉,我有点想念你关于获取拇指文件的文件名的问题,我已经编辑了我的帖子和代码:)

希望能帮助到你 :)

于 2012-07-10T03:10:27.077 回答