-
-
Save bran921007/897bf0945fa9213295a8d8bd14204353 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment