Codeigniter上传库获取拇指文件名

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Codeigniter上传库获取拇指文件名相关的知识,希望对你有一定的参考价值。

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

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

编辑:

  • 对不起,我有点错过了关于获取拇指文件的文件名的问题,我编辑了我的帖子和代码:)

希望能帮助到你 :)

以上是关于Codeigniter上传库获取拇指文件名的主要内容,如果未能解决你的问题,请参考以下文章

将表单文件附加到电子邮件,Codeigniter

如何在codeigniter中获取上传文件的完整路径?

获取 CodeIgniter 4 上传文件的文件名的方法

如何在codeigniter中使用ajax上传图片?

Codeigniter 上传图片 - 上传路径似乎无效

滚动时在另一个片段视频视图中重叠一个片段的视频拇指