繁体   English   中英

Codeigniter上传库获取拇指文件名

[英]Codeigniter upload library get thumb file name

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

$config['create_thumb'] = TRUE; 

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

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

确实有一种更简单的方法:

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
}

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

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

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 
   )

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

    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对象,但是没有任何对生成的拇指图像的文件名的引用。

编辑:

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

希望能帮助到你 :)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM