-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImageController.php
147 lines (121 loc) · 4.16 KB
/
ImageController.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
<?php
class ImageController
{
/**
* this class is a Basic validation for Form input
* @Name: Validation calss
* @Author : Mohamed h Omar
* @email : [email protected]
*/
/**
* 1-upload image
* 2- check for mime types for different browsers
* 3- check the file size - Note : not added yet
* 4- store errors
* 5- upload / move the file if no error
* 6- resize image for different usage
* 7- display errors
*
* contructor takes 2 params array of file uplaod and file size
* file size is optional param
*/
private $file_tmp;
private $file_properties;
private $file_name;
private $folder_path;
private $ext;
private $file_type;
private $file_width;
private $file_height;
private $need_dim =[];
private $msgs = [];
private $root;
public function __construct( $files_arr, $dim_arr = null) {
if(is_array($files_arr)) {
$this->root = '/var/www/basicAdmin/';
$this->file_tmp = $files_arr['tmp_name'];
$this->file_properties = getimagesize($this->file_tmp);
$this->ext = pathinfo($files_arr['name'], PATHINFO_EXTENSION);
if(NUll != $dim_arr){
$this->need_dim= $dim_arr;
}
$this->file_width= $this->file_properties[0];
$this->file_height= $this->file_properties[1];
$this->file_type = $this->file_properties[2];
$this->file_name = time();
$this->folder_path= $this->root.'images/upload/';
}else{
$this->msgs['general_error'] = "there is no file to upload";
}
}
public function checkImageType(){
if(in_array($this->file_type,[IMAGETYPE_PNG, IMAGETYPE_GIF, IMAGETYPE_JPEG])) {
$this->msgs['type'] = "valid file type ".$this->file_type;
return true;
}else{
$this->msgs['type_error'] = "invalid file type" ;
return false;
}
}
public function processImage () {
switch ($this->file_type) {
case IMAGETYPE_PNG:
$fileSourseId = imagecreatefrompng($this->file_tmp);
$target_layer = $this->imageResize($fileSourseId, $this->file_width, $this->file_height);
imagepng($target_layer , $this->folder_path. $this->file_name. "_thump.".$this->ext);
break;
case IMAGETYPE_GIF:
$fileSourseId = imagecreatefromgif($this->file_tmp);
$target_layer = $this->imageResize($fileSourseId, $this->file_width, $this->file_height);
imagegif($target_layer , $this->folder_path. $this->file_name. "_thump.".$this->ext);
break;
case IMAGETYPE_JPEG:
$fileSourseId = imagecreatefromjpeg($this->file_tmp);
$target_layer = $this->imageResize($fileSourseId, $this->file_width, $this->file_height);
imagejpeg($target_layer , $this->folder_path. $this->file_name. "_thump.".$this->ext);
break;
default:
$this->msgs['type_error'] = "invalid file type" ;
return false;
break;
}
if($this->moveImage()){
return true;
}
}
public function moveImage () {
if(move_uploaded_file($this->file_tmp, $this->folder_path. $this->file_name. ".".$this->ext)){
chmod($this->folder_path. $this->file_name. ".".$this->ext, '775');
$this->msgs['upload'] = "image resized and uploaded successfully" ;
return true;
}else{
$this->msgs['upload'] = "can not move the file image" ;
return true;
}
}
public function imageResize ($sourceId, $width, $height) {
if(!empty($this->need_dim)){
$x_size = $this->need_dim[0];
$y_size = $this->need_dim[1];
}
$x_size = 200;
$y_size = 200;
$targetLayer = imagecreatetruecolor($x_size, $y_size);
imagecopyresampled($targetLayer, $sourceId, 0, 0, 0, 0,$x_size, $y_size, $width, $height );
$this->msgs['resize'] = "image resized successfully";
return $targetLayer;
}
//msgId = [general_error, type, type_error, upload, resize]
public function getMessage($msgId=null){
if(null != $msgId){
echo $msgId. ' : '.$this->msgs[$msgId];
}else{
foreach($this->msgs as $id => $msg){
echo $id .' : '.$msg.'<br />';
}
}
}
public function getImage(){
return $this->file_name. "_thump.".$this->ext;
}
}