Last active
March 28, 2016 17:36
-
-
Save marcoszf/fba4a3ed4e6769f83361 to your computer and use it in GitHub Desktop.
mod images - emidcms - Laravel - Repository pattern
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
| <?php namespace App; | |
| use Illuminate\Database\Eloquent\Model; | |
| class Foto extends Model { | |
| protected $table = "tb_fotos"; | |
| protected $primaryKey = "fot_id"; | |
| public $timestamps = false; | |
| public function scopeCapa($query, $id, $modName){ | |
| return $this->select('fot_titulo') | |
| ->where(['fot_vinculo' => $id, 'fot_modulo' => $modName, 'fot_capa' => 1]); | |
| } | |
| } |
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
| <?php | |
| namespace App\Repositories; | |
| interface ImagesRepositoryInterface{ | |
| public function imgCrop(); | |
| public function capeById($id, $modName); | |
| } |
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
| <?php | |
| namespace App\Services; | |
| use App\Repositories\ImagesRepositoryInterface; | |
| use Image; | |
| use Config; | |
| use File; | |
| class ImagesService | |
| { | |
| private $imagesRepository; | |
| public function __construct(ImagesRepositoryInterface $imagesRepository) | |
| { | |
| $this->imagesRepository = $imagesRepository; | |
| } | |
| public function imgCrop($idRow, $modName, $x, $y){ | |
| $imgCapa = $this->imagesRepository->capeById($idRow, $modName); | |
| if( !$imgCapa ){ | |
| $imgToSave = Image::make('arquivos/img-indisponivel/foto_indisponivel.png'); | |
| $imgToCrop = 'arquivos/img-indisponivel/' . $x .'-'. $y .'foto_indisponivel.png'; | |
| if (!File::exists($imgToCrop)) { | |
| $imgToSave->fit($x, $y)->save($imgToCrop); | |
| $imgToSave->destroy(); | |
| } | |
| return Config::get('constants.base_url') .$imgToCrop; | |
| }else{ | |
| $pathImgsCrop = sprintf(Config::get('constants.path_images_crop'), $modName, $idRow); | |
| $imgToSave = Image::make( sprintf(Config::get('constants.path_images'), $modName, $idRow, 'original', $imgCapa->fot_titulo)); | |
| $imgToCrop = $pathImgsCrop . $x .'-'. $y .$imgCapa->fot_titulo; | |
| if (!File::exists($imgToCrop)) { | |
| if (!File::exists($pathImgsCrop)) { | |
| File::makeDirectory($pathImgsCrop, 0775, true); | |
| } | |
| $imgToSave->fit($x, $y)->save($imgToCrop); | |
| $imgToSave->destroy(); | |
| } | |
| return Config::get('constants.base_url') . sprintf(Config::get('constants.path_images_crop'), $modName, $idRow). $x .'-'. $y .$imgCapa->fot_titulo; | |
| } | |
| } | |
| } |
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
| <?php | |
| namespace App\Repositories\Eloquent; | |
| use App\Repositories\ImagesRepositoryInterface; | |
| use App\Foto; | |
| class MysqlImagesRepository implements ImagesRepositoryInterface | |
| { | |
| public function imgCrop(){ | |
| return Foto::ativo()->get(); | |
| } | |
| public function capeById($id, $modName){ | |
| return Foto::capa($id, $modName)->first(); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment