Laravel干预无法从WEBP类型文件的给定二进制数据中初始化

时间:2018-09-03 12:02:25

标签: php laravel laravel-5 file-upload intervention

我正在尝试使用Laravel Intervention Image将二进制映像上传到存储设备,但由于无法从给定的二进制数据进行初始化,因此出现了错误。

我正在使用此代码

$image = base64_decode($postData['image']);

$destinationPath = storage_path($destinationFolder);

if (!File::exists($destinationPath)) {
    File::makeDirectory($destinationPath, 0777, true, true);
}

$filename = ($fileName != '') ? $fileName : $folderName . '_' . time() . '.jpg';

$imageResult = Image::make($image)->resize($imageWidth, $imageHeight, function ($constraint) {
    $constraint->aspectRatio();
})->save($destinationPath . $filename, imageQuality($image));

if ($imageResult){
    return '/image/' . $filename;
}

return false;

二进制图像数据是

data:image/webp;base64,UklGRlIFAABXRUJQVlA4IEYFAAAQHACdASpPAHkAPlEQlEojkdHMYDgFBLIAZqA25eJVGRUWLtSnk7iTzHecd0SXUx8+t+zLWt8WJpEWrTTvLe9y9CX9VSAw3YOVEdn4oGm0ZIrnQUIJ7VsI/r+aW0VOJeFoJylth8MmFQHlbPEklNUVbgyVJnINmgXnDbtgj9paOvkDYnVAdv2ErJONHxXyp2eyn7mB6vcVu9AfWuUtDlYxhQG1CEongtTYR0U4jQIbXYTTzN/3G5cAEd0FmVN272q9XHtEAeWzrMHHsZ7YPtpmppalPe8BvhnrwSA+ctaG9iQ6b7pEVBVPAAD+/uy97OjEKfy53WiZp+vshoaLbmP0cVKb4k6hnKsxJdcP+CgaRYQkOjb4FGLJO55Q/c+afu5UzMOW3Tx4pq6YezZD8PSoPz4zk6GAAEL9XDf3c3RwQKG1r2lWKnBonjqCV8/oU/xr4Gv59yLHfFPfneUb6BrG9yoc40NAk+xGkgtKGsIDDanX+uuhaKWGntbgweNVylzqaIqZrCMYGgfkbTo+yPQ0JgHev/+hCnqRe4cEi4VfveeAi+7wBLg2w4tZOj0d7O7gJM6Zj9uaLB6l/3xyvdHwzThmi8na5GMB/v+Y7YAIYCGOV62mQ6XSrBMQHKUoPvIVwUeHV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CH5Y6Thjn0W8zAWnd3VwwB3ZS/Uv/ubq8H7W9He6fvg6Ib/101yfwV8xpmLHwZGmXWgAAAAAAA=

4 个答案:

答案 0 :(得分:1)

除了使用RAUSHAN KUMAR的答案外,您还可以像这样使用InterventionImage

Route::get('test', function() {
    $image = 'data:image/webp;base64,UklGRlIFAABXRUJQVlA4IEYFAAAQHACdASpPAHkAPlEQlEojkdHMYDgFBLIAZqA25eJVGRUWLtSnk7iTzHecd0SXUx8+t+zLWt8WJpEWrTTvLe9y9CX9VSAw3YOVEdn4oGm0ZIrnQUIJ7VsI/r+aW0VOJeFoJylth8MmFQHlbPEklNUVbgyVJnINmgXnDbtgj9paOvkDYnVAdv2ErJONHxXyp2eyn7mB6vcVu9AfWuUtDlYxhQG1CEongtTYR0U4jQIbXYTTzN/3G5cAEd0FmVN272q9XHtEAeWzrMHHsZ7YPtpmppalPe8BvhnrwSA+ctaG9iQ6b7pEVBVPAAD+/uy97OjEKfy53WiZp+vshoaLbmP0cVKb4k6hnKsxJdcP+CgaRYQkOjb4FGLJO55Q/c+afu5UzMOW3Tx4pq6YezZD8PSoPz4zk6GAAEL9XDf3c3RwQKG1r2lWKnBonjqCV8/oU/xr4Gv59yLHfFPfneUb6BrG9yoc40NAk+xGkgtKGsIDDanX+uuhaKWGntbgweNVylzqaIqZrCMYGgfkbTo+yPQ0JgHev/+hCnqRe4cEi4VfveeAi+7wBLg2w4tZOj0d7O7gJM6Zj9uaLB6l/3xyvdHwzThmi8na5GMB/v+Y7YAIYCGOV62mQ6XSrBMQHKUoPvIVwUeHVkJFWnUCt6S7yOMa9RkZxe8//Bphx4NhJ/dXc3x7HQESKmLUu8nofAJKiyg7v46s90BuZWpbVYysGbdPR9Shc9nqgYoEazEu+ik00Mr+VLM+/lS8aCumf4on0FkZ/Dn4SGJSU8pc02nt7ncW0e0XwVKx9DE8RfVww8GDv33+1ib7qkv1gsaGBdn60MpW2PzyI1ZDReCh25f4z4RsG91nEpjDr4MmVOaW40nXwNnAfuYawSt+b05IQx9GGw0seGFDJ4hbb+tTatszMiOSjhQ9HsO19t/hVFojVco/cKoG9XUSorPouBOqFqDrciO3+BVFFo5l5JW3Ka0ZtamSCrzt1AUzOndTy82imvJ+NZ1D+iXF92d3XITYsveniLVxEjp+pQIp8pXJ3p3DuFQPxzuQ44E3xWcPimJ7wuJnIrIm8jyFaM4AHJ1OBc/BG+0iP5zUHWl36LGK5VpoDkw34T+sQs9s0gOpG+tNM0uaKmwmONjo2L0tFqaiC4V4aHLO3JqptxoxSjn3BQVG5x/ga/7bz/hqeycRIoGrTuIqKAdCVVrYLEgr39NID+sKzK6BYzr8j9r3JMPZ3+2T09lEwe0u+S7B4wI3rlSz5L3DZspwtfkWcGtZpUCRqgOiWnFnpdIlor1+zvQK2ksDUpf0UbyRW/c59RQIfouYA6cKJUxsQzPPL5yvuIprzjZLY2HRkKFbemgUbPH1nxw28qtf2EixENC3uGus24PjYW8Jz/G5kZ2ioG2UXp00Aru2Z1Hk4AB0G2RPAzhdp1WQEnbqXb5a35eSSv5SGUYQbRDbaQNnyeODfb80a056/Cz7wmTtn5xvWT1UhTg7J/9J/4cCoBkweCOc1exDC4bBpdmwNyB82TfMOL+HcsO/UR1EDkEBdEl2HLQOA2mSntOQ3dTgMQX/71+MtpetptbThjL/fnRMMfVnpovAe2jd4SXx+s8fMNJXcEkO9ZKxIWFH0EwLvzIp8SPc5z+AzLEwfn4aBvVL835u4BsodI+usLpWnlC7Xdz6JMVxKivRpoWgrcIsAF91564sRuvXd3rwozLc0Fb/at7P3B3B5Jc7wPXQ/CH5Y6Thjn0W8zAWnd3VwwB3ZS/Uv/ubq8H7W9He6fvg6Ib/101yfwV8xpmLHwZGmXWgAAAAAAA=';
    $image = imagecreatefromwebp($image);

    return Image::make($image)->resize(100)->response();
});

通过将路线称为“测试”,您将看到图像。

答案 1 :(得分:0)

由于这是webp类型的图像,因此我需要使用imagecreatefromwebp()上传图像。我已经为此编写了这段代码。

$destinationFolder = 'uploads/';
$folderName = $folder . '_' . $adId;
if ($folderName != '') {
    $folderNames = explode('_', $folderName);
    $folderPath = implode('/', array_map(function ($value) {
        return $value;
    }, $folderNames));
    $destinationFolder .= $folderPath . '/';
}
$destinationPath = storage_path($destinationFolder);
if (!\File::exists($destinationPath)) \File::makeDirectory($destinationPath, 0777, true, true);
$fileName = $folder . '_' . $adId . '_0_' . time() . '.jpg';
$fileName = ($fileName != '') ? $fileName : $folderName . '_' . time() . '.jpg';
$im = imagecreatefromwebp($data);
$imageResult = imagejpeg($im, $destinationPath . $fileName, 100);
imagedestroy($im);
if ($imageResult) return '/image/' . $fileName;
return "/DefaultImage.jpg";

答案 2 :(得分:-1)

如果您仍要使用代码,则可以删除data:image/webp;base64,,然后在删除后使用base64_decode

$image=explode(",",$postData['image']);
$image=base64_decode($image['1']);

答案 3 :(得分:-1)

图像干预可以解码base64图像,您可以尝试

$imageResult = Image::make($postData['image'])->resize($imageWidth, $imageHeight, function ($constraint) {
$constraint->aspectRatio();