Codeigniter上传库获取拇指文件名

时间:2012-07-10 00:44:30

标签: php codeigniter file-upload

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

$config['create_thumb'] = TRUE; 

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

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

4 个答案:

答案 0 :(得分: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
}

答案 1 :(得分: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

希望这有助于你。谢谢!!

答案 2 :(得分: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 
   )

答案 3 :(得分: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";
    }        
}

要调用该功能,首先必须上传文件,获取文件名和文件名。调用函数 - &gt; $this->generateThumb($file_resp['file_name']);

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

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

已编辑:

  • Sory,我有点错过了关于获取拇指文件的文件名的问题,我编辑了我的帖子&amp;代码:)

希望有所帮助:)