无法从文件.SVG中读取图像(干预/图像)

时间:2017-01-07 00:13:40

标签: php image laravel svg intervention

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

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

但话虽如此,当我尝试上传svg图片时,它声明:

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()但是它只是说图像无法打开/读或。

$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"){}

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

所以我尝试将自己建立在实际的扩展上,而不是mime类型,这在我的情况下有点起作用如下:

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]);
}

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

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

1 个答案:

答案 0 :(得分:2)

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

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]);
}

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

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

无论哪种方式,我都感谢所有试图帮助我解决这个问题的人!