-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adiciona primeira versão de upload de imagens
Closes: #7
- Loading branch information
jprodrigues70
committed
Jun 6, 2016
1 parent
59c0408
commit e9717ba
Showing
4 changed files
with
79 additions
and
10 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
<?php | ||
namespace Helper; | ||
|
||
class Image { | ||
public static $allowedTypes = array( | ||
"image/gif", | ||
"image/jpeg", | ||
"image/png", | ||
); | ||
|
||
public $name; | ||
public $type; | ||
public $image; | ||
|
||
function __construct($image) { | ||
$this->name = md5(sha1(date('d-m-y H:i:s').$image['tmp_name'])); | ||
$this->type = end((explode('/', $image['type']))); | ||
$this->image = imagecreatefromstring(file_get_contents($image['tmp_name'])); | ||
} | ||
|
||
public function resize($width) { | ||
$x = imagesx($this->image); | ||
$y = imagesy($this->image); | ||
$height = $width * $y / $x; | ||
$new = imagecreatetruecolor($width, $height); | ||
// preserve transparency | ||
if($this->type == "gif" || $this->type == "png"){ | ||
imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127)); | ||
imagealphablending($new, false); | ||
imagesavealpha($new, true); | ||
} | ||
|
||
imagecopyresampled($new /*destino*/, $this->image /*origem*/, 0, 0, 0, 0, $width, $height, $x, $y); | ||
$this->image = $new; | ||
} | ||
|
||
public function save($dir) { | ||
$dir .= $this->name.".".$this->type; | ||
$func = 'image'.$this->type; | ||
$func($this->image, $dir); | ||
} | ||
|
||
public static function validateType($type) { | ||
return in_array($type, self::$allowedTypes); | ||
} | ||
} |
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
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
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