Created
July 29, 2013 19:09
-
-
Save Sigmus/6106869 to your computer and use it in GitHub Desktop.
This file contains 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 | |
class CropFoto extends Eloquent { | |
private $cropper; | |
private $upload; | |
private $ratio; | |
private $scale; | |
private $config; | |
public function edicao() | |
{ | |
return $this->belongs_to('Edicao'); | |
} | |
public static function make($fotoId) | |
{ | |
$instance = new self; | |
$instance->upload = UploadFoto::find($fotoId); | |
$instance->eloquent_id = $instance->upload->eloquent_id; | |
$data = Input::all('data'); | |
$instance->tipo = $data['tipo']; | |
$instance->ratio = $instance->getRatio(); | |
$instance->scale = $data['scaled_width'] / $instance->upload->width; | |
$instance->cropper = $data['cropData']; | |
$instance->crop(); | |
$instance->save(); | |
return $instance; | |
} | |
private function crop() | |
{ | |
$dest = $this->getNewImage(); | |
$isOk = imagecopyresampled( | |
$dest, | |
imagecreatefromjpeg($this->upload->getCaminho()), | |
0, | |
0, | |
$this->cropper['x'] / $this->scale, | |
$this->cropper['y'] / $this->scale, | |
imagesx($dest), | |
imagesy($dest), | |
$this->cropper['w'] / $this->scale, | |
$this->cropper['h'] / $this->scale | |
); | |
if (! $isOk) { | |
die('deu problema!!!'); | |
} | |
$this->createUniqueFilename(); | |
imagejpeg($dest, $this->getCaminho()); | |
} | |
public function getCaminho() | |
{ | |
return Config::get('foto.crop_dir') . $this->arquivo; | |
} | |
private function createUniqueFilename() | |
{ | |
$this->arquivo = $this->upload->eloquent_name . '-' . str_pad( | |
(string) $this->eloquent_id, 6, '0', STR_PAD_LEFT | |
) . '-' . $this->tipo . '-' . time() . '.' . File::extension($this->upload->arquivo); | |
} | |
private function getNewImage() | |
{ | |
return imagecreatetruecolor( | |
(int) $this->cropper['w'] / $this->scale, | |
(int) $this->cropper['h'] / $this->scale | |
); | |
} | |
private function getRatio() | |
{ | |
$ratios = Config::get('foto.ratios'); | |
return $ratios[$this->tipo]; | |
} | |
public function size() | |
{ | |
$sizes = Config::get('foto.sizes'); | |
return $sizes[$this->tipo][0]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment