Skip to content

Commit

Permalink
Adiciona primeira versão de upload de imagens
Browse files Browse the repository at this point in the history
Closes: #7
  • Loading branch information
jprodrigues70 committed Jun 6, 2016
1 parent 59c0408 commit e9717ba
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 10 deletions.
46 changes: 46 additions & 0 deletions helpers/image.php
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);
}
}
7 changes: 4 additions & 3 deletions helpers/session.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Session extends Session_controller{
*/
public static function is_onLine() {
if (!isset($_SESSION['on'])) {
header('Location: ../controllers/session_controller?logout');
header('Location:../controllers/session_controller?logout');
}
}

Expand All @@ -33,11 +33,12 @@ public static function msg() {
* @return boolean
*/
public static function is_active($page) {
if (array_pop(explode('/', $_SERVER['REQUEST_URI'])) == $page) {
$uri = explode('/', $_SERVER['REQUEST_URI']);
if (array_pop($uri) == $page) {
echo ' class="active"';
}
}
}

session_start();
Session::is_onLine();
// Session::is_onLine();
25 changes: 23 additions & 2 deletions lib/controller/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,14 @@
* - Model: User
* - Correspondent controller: User_controller
*/

namespace Controller;

class Base {

require_once('../helpers/image.php');
use \Helper;


class Base extends \Helper\Image {

/**
* Stores the address to redirect user after action
Expand Down Expand Up @@ -82,6 +86,22 @@ public function is_valid() {
* @return void
*/
public function store() {
$image = is_uploaded_file($_FILES['picture']['tmp_name']);
if ($image && !self::validateType($_FILES['picture']['type'])) {
header('location: ../views/insert_clients.php');
break;
}
/*Caso haja alguma imagem prepara-a para inserção*/
if ($image) {
$path = '../uploads/'.lcfirst($this->model).'/';
$image = new \Helper\Image($_FILES['picture']);
$image->resize(350);
$image->save($path);
$_REQUEST['picture'] = $path.'/'.$image->name.'.'.$image->type;
} else {
$_POST['picture'] = 'logo.png';
}

if (self::is_valid()) {
$obj = new $this->model($_REQUEST);
try {
Expand Down Expand Up @@ -129,6 +149,7 @@ public function login() {
$_SESSION['id'] = $got->id;
$_SESSION['name'] = $got->name;
$_SESSION['level'] = $got->level;
$_SESSION['picture'] = $got->picture;
$_SESSION['on'] = true;
$got = null;
header('Location: ../views/home');
Expand Down
11 changes: 6 additions & 5 deletions lib/model/base.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ abstract class Base {
*/
function __construct($attributes) {
$this->id = isset($attributes['id']) ? $attributes['id'] : null;
isset($attributes['password']) ? static::encryptPass($attributes['password']) : true;
(!empty($attributes['password'])) ? static::encryptPass($attributes['password']) : true;
foreach ($attributes as $key => $value) {
if(in_array($key, $this->fillable)) {
$this->$key = $value;
Expand All @@ -48,7 +48,7 @@ function __construct($attributes) {
* @return object \PDO
*/
protected static function connect() {
$pdo = new \PDO('mysql:host=localhost;dbname=default', 'root', '');
$pdo = new \PDO('mysql:host=localhost;dbname=produtora', 'root', '');
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
return $pdo;
}
Expand Down Expand Up @@ -95,9 +95,10 @@ public function login() {
* @param string $order [Sets the ORDER BY param and sorting to SQL query.]
* @return object
*/
public static function all($order = 'id ASC') {
public static function all($order = 'id ASC', $limit = null) {
if (!is_null($limit)) $limit = ' LIMIT '.$limit;
$connect = self::connect();
$stm = $connect->query('SELECT * FROM `'.self::entity().'` ORDER BY '.$order);
$stm = $connect->query('SELECT * FROM `'.self::entity().'` ORDER BY '.$order.$limit);
$stm->execute();
return $stm->fetchAll(PDO::FETCH_OBJ);
}
Expand All @@ -110,7 +111,7 @@ public static function all($order = 'id ASC') {
*/
public static function one($id = null) {
$connect = self::connect();
isset($_GET['id']) ? $id = $_GET['id'] : true;
if (!$id) $id = $_GET['id'];
$stm = $connect->prepare('SELECT * FROM `'.self::entity().'` WHERE id = '.$id.' LIMIT 1');
$stm->bindParam(":id", $id, PDO::PARAM_INT);
$stm->execute();
Expand Down

0 comments on commit e9717ba

Please sign in to comment.