Skip to content

Instantly share code, notes, and snippets.

@bran921007
Forked from leonardopinho/base64ToFile.php
Created October 9, 2020 06:28
Show Gist options
  • Save bran921007/897bf0945fa9213295a8d8bd14204353 to your computer and use it in GitHub Desktop.
Save bran921007/897bf0945fa9213295a8d8bd14204353 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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment