Skip to content

Instantly share code, notes, and snippets.

@leonardopinho
Last active June 19, 2022 11:42
Show Gist options
  • Save leonardopinho/285660d4e8456fcd4a98cb619d34e916 to your computer and use it in GitHub Desktop.
Save leonardopinho/285660d4e8456fcd4a98cb619d34e916 to your computer and use it in GitHub Desktop.
Laravel: Convert base64 to file and resize the final size.
/**
* 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;
}
@ayamba-coder
Copy link

Thanks alot for this code

@Yondalne
Copy link

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