/**
* 画像保存
* @param string $base64Context
* @param string $storage
* @param string $dir
* @return App\Models\Image
*/
public function storeImage($base64Context, $storage, $dir)
{
try {
preg_match('/data:image\/(\w+);base64,/', $base64Context, $matches);
$extension = $matches[1];
$img = preg_replace('/^data:image.*base64,/', '', $base64Context);
$img = str_replace(' ', '+', $img);
$fileData = base64_decode($img);
$dir = rtrim($dir, '/').'/';
$fileName = md5($img);
$path = $dir.$fileName.'.'.$extension;
Storage::disk($storage)->put($path, $fileData);
return $path;
} catch (Exception $e) {
Log::error($e);
return null;
}
}
More than 3 years have passed since last update.
Register as a new user and use Qiita more conveniently
- You get articles that match your needs
- You can efficiently read back useful information
- You can use dark theme