image-class
Code (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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
<?php
//class image handler
class image
{
//only for image handlers
var $supportim = array(1,2,3); //1 = gif / 2 = jpg / 3 = png
//check for file realy exists
//$file = file that schould be resized / what ever
function _check_file(){
if(file_exists($this->file)){
//file does realy exists
return true;
}else{
return false;
}
}
//recive image size and check if file is realy a image
function _get_image_size(){
if($this->dimension = getimagesize($this->file)){
//we got a image;
return true;
}else{
return false;
}
}
//check if we got a support for those images
function _check_image_support(){
if(empty($this->dimension)){
//trigger a error;
trigger_error('No file specified please run get_image_size first',E_WARNING_ERROR);
}else{
if(in_array($this->dimension[2],$this->supportim)){
return true;
}else{
return false;
}
}
}
function read_file(){
if($this->dimension[2] == 1){
if(!$this->image_source = imagecreatefromgif($this->file)){
trigger_error('Failed to open a stream',E_USER_WARNING);
return false;
}else{
return true;
}
}elseif($this->dimension[2] == 2){
if(!$this->image_source = imagecreatefromjpeg($this->file)){
trigger_error('Failed to open a stream',E_USER_WARNING);
return false;
}else{
return true;
}
}elseif($this->dimension[2] == 3){
if(!$this->image_source = imagecreatefrompng($this->file)){
trigger_error('Failed to open a stream',E_USER_WARNING);
return false;
}else{
return true;
}
}else{
trigger_error('Filetype is not supported',E_USER_WARNING);
return false;
}
}
//check image support
//output:
//1 = support for resize / flip
//2 = Image but no support: But it is at least a image so avaible to upload.
//3 = No image only upload avialble Please check correct files if you want
//4 = file does not exits;
function check_support(){
if($this->_check_file()){
if($this->_get_image_size()){
if($this->_check_image_support()){
//read file for gd stream;
if($this->read_file()){
return 1;
}
}else{
return 2;
}
}else{
return 3;
}
}else{
return 4;
}
}
//resize image
function resize($width,$height){
if($this->dimension[0] > $this->dimension[1]){
$ratio = $width / $this->dimension[0];
$this->newdimension[0] = $width;
$this->newdimension[1] = round($this->dimension[1] * $ratio);
}else{
$ratio = $height / $this->dimension[0];
$this->newdimension[1] = $height;
$this->newdimension[0] = round($this->dimension[0] * $ratio);
}
//create new image
if(!$this->image = @imagecreatetruecolor($this->newdimension[0],$this->newdimension[1])){
trigger_error('Failed open a new stream',E_USER_WARNING);return false;
}else{
if(!@imagecopyresampled($this->image, $this->image_source, 0, 0, 0, 0, $this->newdimension[0], $this->newdimension[1], $this->dimension[0], $this->dimension[1])){
trigger_error('Failed to resample',E_USER_WARNING);
return false;
}else{
return true;
}
}
}
//give extension
function getextension($bestand)
{
return strtolower(end(explode('.',$bestand)));
}
function getlastdir($bestand)
{
return strtolower(end(explode('/',$bestand)));
}
//create image
function write_file($dir,$file){
if(!file_exists($dir.$file)){
if($this->dimension[2] == 1){
if(@!imagegif($this->image,$dir.$file)){
trigger_error('Failed to write to file',E_USER_WARNING);
return false;
}else{
return true;
}
}elseif($this->dimension[2] == 2){
if(@!imagejpeg($this->image,$dir.$file)){
trigger_error('Failed to write to file',E_USER_WARNING);
return false;
}else{
return true;
}
}elseif($this->dimension[2] == 3){
if(!@imagepng($this->image,$dir.$file)){
trigger_error('Failed to write to file',E_USER_WARNING);
return false;
}else{
return true;
}
}else{
return false;
}
}else{
return false;
}
}
//upload file
function move_uploaded_file($dir,$file){
if(is_uploaded_file($this->file)){
//file is geupload
if(@move_uploaded_file($this->file,$dir.$file)){
return true;
}else{
return false;
trigger_error('Non such file or not enough access',E_USER_WARNING);
}
}else{
trigger_error('File is not uploaded',E_USER_WARNING);
return false;
}
}
function read_dir_array($directory){
if(@$dir = opendir($directory))
{
while (false !== ($file = readdir($dir)))
{
if ($file != "." && $file != "..")
{
if(is_file($directory.$file))
{
$extensie = $this->getextension($file);
$extensies = array('png','jpg','jpeg','gif');
if(in_array($extensie,$extensies))
{
$bestanden[] = $file;
}
}
if(is_dir($directory.$file))
{
if($file != 'thumb')
{
$directories[] = $file;
}
}
}
}
closedir($dir);
$files = array();
if(!empty($bestanden)){
$files['images'] = $bestanden;
}
if(!empty($directories)){
$files['dirs'] = $directories;
}
return $files;
}else{
return false;
}
}
}
/*$file = './schipbreukeling/images/msc.png';
$image = new image;
$image->file = $file;
if($image->check_support() == 1){
$image->move_uploaded_file('./','test.png');
}else{
echo 'No support';
}*/
?>
//class image handler
class image
{
//only for image handlers
var $supportim = array(1,2,3); //1 = gif / 2 = jpg / 3 = png
//check for file realy exists
//$file = file that schould be resized / what ever
function _check_file(){
if(file_exists($this->file)){
//file does realy exists
return true;
}else{
return false;
}
}
//recive image size and check if file is realy a image
function _get_image_size(){
if($this->dimension = getimagesize($this->file)){
//we got a image;
return true;
}else{
return false;
}
}
//check if we got a support for those images
function _check_image_support(){
if(empty($this->dimension)){
//trigger a error;
trigger_error('No file specified please run get_image_size first',E_WARNING_ERROR);
}else{
if(in_array($this->dimension[2],$this->supportim)){
return true;
}else{
return false;
}
}
}
function read_file(){
if($this->dimension[2] == 1){
if(!$this->image_source = imagecreatefromgif($this->file)){
trigger_error('Failed to open a stream',E_USER_WARNING);
return false;
}else{
return true;
}
}elseif($this->dimension[2] == 2){
if(!$this->image_source = imagecreatefromjpeg($this->file)){
trigger_error('Failed to open a stream',E_USER_WARNING);
return false;
}else{
return true;
}
}elseif($this->dimension[2] == 3){
if(!$this->image_source = imagecreatefrompng($this->file)){
trigger_error('Failed to open a stream',E_USER_WARNING);
return false;
}else{
return true;
}
}else{
trigger_error('Filetype is not supported',E_USER_WARNING);
return false;
}
}
//check image support
//output:
//1 = support for resize / flip
//2 = Image but no support: But it is at least a image so avaible to upload.
//3 = No image only upload avialble Please check correct files if you want
//4 = file does not exits;
function check_support(){
if($this->_check_file()){
if($this->_get_image_size()){
if($this->_check_image_support()){
//read file for gd stream;
if($this->read_file()){
return 1;
}
}else{
return 2;
}
}else{
return 3;
}
}else{
return 4;
}
}
//resize image
function resize($width,$height){
if($this->dimension[0] > $this->dimension[1]){
$ratio = $width / $this->dimension[0];
$this->newdimension[0] = $width;
$this->newdimension[1] = round($this->dimension[1] * $ratio);
}else{
$ratio = $height / $this->dimension[0];
$this->newdimension[1] = $height;
$this->newdimension[0] = round($this->dimension[0] * $ratio);
}
//create new image
if(!$this->image = @imagecreatetruecolor($this->newdimension[0],$this->newdimension[1])){
trigger_error('Failed open a new stream',E_USER_WARNING);return false;
}else{
if(!@imagecopyresampled($this->image, $this->image_source, 0, 0, 0, 0, $this->newdimension[0], $this->newdimension[1], $this->dimension[0], $this->dimension[1])){
trigger_error('Failed to resample',E_USER_WARNING);
return false;
}else{
return true;
}
}
}
//give extension
function getextension($bestand)
{
return strtolower(end(explode('.',$bestand)));
}
function getlastdir($bestand)
{
return strtolower(end(explode('/',$bestand)));
}
//create image
function write_file($dir,$file){
if(!file_exists($dir.$file)){
if($this->dimension[2] == 1){
if(@!imagegif($this->image,$dir.$file)){
trigger_error('Failed to write to file',E_USER_WARNING);
return false;
}else{
return true;
}
}elseif($this->dimension[2] == 2){
if(@!imagejpeg($this->image,$dir.$file)){
trigger_error('Failed to write to file',E_USER_WARNING);
return false;
}else{
return true;
}
}elseif($this->dimension[2] == 3){
if(!@imagepng($this->image,$dir.$file)){
trigger_error('Failed to write to file',E_USER_WARNING);
return false;
}else{
return true;
}
}else{
return false;
}
}else{
return false;
}
}
//upload file
function move_uploaded_file($dir,$file){
if(is_uploaded_file($this->file)){
//file is geupload
if(@move_uploaded_file($this->file,$dir.$file)){
return true;
}else{
return false;
trigger_error('Non such file or not enough access',E_USER_WARNING);
}
}else{
trigger_error('File is not uploaded',E_USER_WARNING);
return false;
}
}
function read_dir_array($directory){
if(@$dir = opendir($directory))
{
while (false !== ($file = readdir($dir)))
{
if ($file != "." && $file != "..")
{
if(is_file($directory.$file))
{
$extensie = $this->getextension($file);
$extensies = array('png','jpg','jpeg','gif');
if(in_array($extensie,$extensies))
{
$bestanden[] = $file;
}
}
if(is_dir($directory.$file))
{
if($file != 'thumb')
{
$directories[] = $file;
}
}
}
}
closedir($dir);
$files = array();
if(!empty($bestanden)){
$files['images'] = $bestanden;
}
if(!empty($directories)){
$files['dirs'] = $directories;
}
return $files;
}else{
return false;
}
}
}
/*$file = './schipbreukeling/images/msc.png';
$image = new image;
$image->file = $file;
if($image->check_support() == 1){
$image->move_uploaded_file('./','test.png');
}else{
echo 'No support';
}*/
?>