Last active
June 19, 2022 11:42
-
-
Save leonardopinho/285660d4e8456fcd4a98cb619d34e916 to your computer and use it in GitHub Desktop.
Laravel: Convert base64 to file and resize the final size.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* base64ToFile | |
* @param $base64 | |
* @param $path | |
* @param int $width | |
* @param int $height | |
* @return string | |
* @info usage 'Image' => Intervention\Image\Facades\Image::class | |
*/ | |
public static function base64ToFile($base64, $path, $width = 400, $height = 400) | |
{ | |
$image = str_replace('data:image/png;base64,', '', $base64); | |
$image = str_replace(' ', '+', $image); | |
$imageName = md5(rand(11111, 99999)) . '_' . time() . '.jpg'; | |
$path = $path . $imageName; | |
$input = File::put($path, base64_decode($image)); | |
$image = Image::make($path)->resize($width, $height); | |
$result = $image->save($path); | |
return $imageName; | |
} |
Hi
can you explain me why we have to replace all space by + ?
and this code is for which version of laravel ?
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thanks alot for this code