无法从文件中读取图像.SVG(干预/图像) [英] Unable to read image from file .SVG (intervention/image)

查看:546
本文介绍了无法从文件中读取图像.SVG(干预/图像)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在制作一个图像上传器,我想制作缩略图但也支持svg,因为GD不支持svg类型我首先尝试在config / image.php文件中切换到想象但是没有'改变任何东西。

So I'm making an image uploader where I want to make thumbnails but also support svg as well, since GD doesn't support svg types I first tried switching to imagick in the config/image.php file but that didn't change anything.

我不确定,因为它说它支持它,我错过了必须安装的必需软件包吗?如果是这样的话?。

Which I am uncertain about as it does state it supports it, am I missing a required package that I have to install? If so which one?.

但话说回来,当我尝试上传svg图片时,它说明:

But with that said, when I try to upload an svg image it states:

NotReadableException in Decoder.php line 20:
Unable to read image from file (D:\xampp\htdocs\laravel\public\up/2017/01/07000323-logoSep.svg).

我首先尝试使用简单的IF结构解决这个问题,因为我真的不需要SVG的缩略图图像,通过使用 - > mime(),但它只是说图像无法打开/读取。

I first tried tackling this with a simple IF structure as I don't really need a thumbnail for SVG images, by using ->mime() but then it just said the image couldn't be opened/read either.

$image = $request->file('file');
$imageName = date("dHis-").$image->getClientOriginalName();
$uploadPath = public_path('up/').date("Y/m");
$image->move($uploadPath,$imageName);
$imageMime = Image::make($uploadPath.'/'.$imageName);
if($imageMime->mime() != "image/svg+xml"){}

我首先认为这是由权限问题引起的,所以我确保我的所有文件都是可读写的,但这并没有改变问题。

With that I first thought this was caused by a permission issue so I made sure all my files are read and writable, but that didn't change the issue.

所以我试着根据实际的扩展而不是mime类型,这在我的情况下有点起作用如下:

So I tried to base myself on the actual extension rather than the mime type which does somewhat work in my case as the following:

public function dropzoneStore(Request $request){
    $image = $request->file('file');
    $imageName = date("dHis-").$image->getClientOriginalName();
    $uploadPath = public_path('up/').date("Y/m");
    $image->move($uploadPath,$imageName);
    if($image->getClientOriginalExtension() != 'svg'){
        $imageThmb = Image::make($uploadPath.'/'.$imageName);
        $imageThmb->fit(300,300,function($constraint){$constraint->upsize();})->save($uploadPath.'/thm_'.$imageName,80);
    }
    return response()->json(['success'=>$imageName]);
}

但我发现这是一种相当hackish的做法。是不是有更好的方法来过滤掉或支持整个干预/图像包的svg类型?

But I find this to be a rather hackish approach. Isn't there a better way to filter out or support svg types with the whole intervention/image package?

提前感谢您提供更多信息!

Thanks in advance for further information!

推荐答案

所以通过进一步的实验,并试图让一些东西与干预/图像库保持一致,但没有将svg转换为任何我去过的东西以下解决方案:

So with further experimentation and trying to get something to work with staying to the intervention/image library but without converting the svg to anything I've gone with the following solution:

public function dropzoneStore(Request $request){
    $image = $request->file('file');
    $imageName = date("dHis-").preg_replace("/[^a-zA-Z0-9.]/","",$image->getClientOriginalName());
    $uploadPath = public_path('up/').date("Y/m");
    $image->move($uploadPath,$imageName);
    //Thumbnail Creation
    $thumbPath = $uploadPath.'/thumbs/';
    File::isDirectory($thumbPath) or File::makeDirectory($thumbPath,0775,true,true);
    if($image->getClientOriginalExtension() != 'svg'){
        $imageThmb = Image::make($uploadPath.'/'.$imageName);
        $imageThmb->fit(300,300,function($constraint){$constraint->upsize();})->save($uploadPath.'/thumbs/thm_'.$imageName,80);
    }else{
        File::copy($uploadPath.'/'.$imageName,$uploadPath.'/thumbs/thm_'.$imageName);
    }
    return response()->json(['success'=>$imageName]);
}

虽然我眼中有点牵强和黑客的做法,但仍然存在似乎与我的文件系统一起工作,需要每个图像的拇指。

Which while a bit far fetched and a hacky approach in my eyes, still does seem to do the trick to work alongside with my file system that requires a thumb for every image.

当我进一步扩展我的网站使用以最终将SVG图像转换为缩略图时,我可能会调查它。但是现在这样做了,因为.svg不是用于网站开发的,我也可以在负载方面放心。

While I might look into it anyway when I further expand the use of my website to eventually do convert the SVG images to thumbnails. But for now this will do and as .svg's aren't that used yet for website development I can be at ease as well in terms of load.

无论如何我感谢每个试图帮助我解决这个问题的人!

Either way I thank everyone who tried to assist me with this matter!

这篇关于无法从文件中读取图像.SVG(干预/图像)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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