从mimetype生成图像扩展名 [英] Generate image extension from mimetype

查看:113
本文介绍了从mimetype生成图像扩展名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试在Laravel 5中上传图像(通过laravelcollective/forms生成的上传,并使用Intervention Image库进行了处理). 我想做的是,当用户上传任何照片时,我想根据其模仿类型来设置扩展名.应该进行一些基本检查,以防止虚假数据注入.

I have been trying image uploading in Laravel 5 (upload generated through laravelcollective/forms, and processed using Intervention Image library). What I wanna do is when user uploads any photo, I want to set the extension based on its mimetype. There should be some basic check to protect against spurious data injection.

$file_profile_image->getClientMimeType();

为此,我是否应该像这样进行映射?

To do that, should I simply be mapping like so ?

['image/jpeg' => 'jpg', 'image/gif'=> 'gif']

推荐答案

我将使用Intervention软件包检查您是否正在加载有效的图像并从中获取MIME.

I would use the Intervention package to check if you're loading a valid image and get the mime from there.

类似这样的东西:

/**
 * Store a file
 *
 * @return Response
 */
public function store(Filesystem $filesystem)
{
    // check if file was posted

    $uploadedFile = Request::file('file');

    // other checks here, ->isValid() && filesize

    try {
        $image = Image::make(\File::get($uploadedFile));
    } catch (\Intervention\Image\Exception\NotReadableException $e) {
        \Log::error('Unsupported filetype');
        dd('Unsupported filetype');
        // return proper error here
    }

    // mime as returned by Intervention
    $mime = $image->mime();

    // other stuff
    // store @ fs
}

这篇关于从mimetype生成图像扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆