%PDF- %PDF-
Direktori : /var/www/html/news/app/Repositories/Media/ |
Current File : /var/www/html/news/app/Repositories/Media/MediaEloquentRepository.php |
<?php namespace App\Repositories\Media; use App\Models\Media; use App\Repositories\Base\BaseEloquentRepository; use Illuminate\Container\Container; use Illuminate\Support\Collection; use Intervention\Image\Facades\Image; class MediaEloquentRepository extends BaseEloquentRepository implements MediaRepository { public function __construct(Container $container, Collection $collection) { parent::__construct($container, $collection); } public function model() { return Media::class; } /** * @param $directory * @param $encodedImage * @return bool * @throws \Exception */ public function createImage($directory, $encodedImage) { try { $this->createPath($directory); $img = Image::cache(function ($image) use ($encodedImage) { return $image->make($encodedImage); }, 10, true); $fileName = $this->generateUniqueString('file_name') . '.' . last(explode('/', $img->mime())); $avatarPath = $directory . $fileName; if (!file_exists($avatarPath)) { $img->save($avatarPath); } return $fileName; } catch (\Exception $e) { throw $e; } } /** * @param $path * @return bool */ private function createPath($path) { if (!is_dir($path)) { return mkdir($path, 0777, true); } return true; } }