ImageResize Class
Alleen als ik de class aanroep doet die niks... maar ik zie geen fout dus kan iemand mij even helpen met wat ik fout doe of met verbeteringen en tips.
dit is mijn code waarmee ik hem aanroep:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
$new_width = 250; // In pixels
$new_height = 200; //In pixels
$new_width_groot = 800;
$new_height_groot = 500;
$image_name = $_FILES['Foto_Preview']['name']; //$_FILES['Foto_Preview']["name"];
$uploaded_file = $_FILES['Foto_Preview']['tmp_name']; //$_FILES['Foto_Preview']['tmp_name'];
$objClsResizeImg = new ClsResizeImg($_POST['Projectnaam'],$new_width,$new_height,$new_width_groot,$new_height_groot,$image_name,$uploaded_file);
echo $objClsResizeImg->Image();
?>
$new_width = 250; // In pixels
$new_height = 200; //In pixels
$new_width_groot = 800;
$new_height_groot = 500;
$image_name = $_FILES['Foto_Preview']['name']; //$_FILES['Foto_Preview']["name"];
$uploaded_file = $_FILES['Foto_Preview']['tmp_name']; //$_FILES['Foto_Preview']['tmp_name'];
$objClsResizeImg = new ClsResizeImg($_POST['Projectnaam'],$new_width,$new_height,$new_width_groot,$new_height_groot,$image_name,$uploaded_file);
echo $objClsResizeImg->Image();
?>
En dit is de classe
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
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
<?php
class ClsResizeImg
{
private $project_naam;
private $width;
private $height;
private $new_width;
private $new_height;
private $new_width_groot;
private $new_height_groot;
private $image_name;
private $uploaded_file;
private $filename;
private $filename_groot;
private $extension;
private $error;
private $src;
private $tmp;
private $tmp_groot;
function __construct($project_naam,$new_width,$new_height,$new_width_groot,$new_height_groot,$image_name,$uploaded_file)
{
$this->project_naam = $project_naam;
$this->new_width = $new_width;
$this->new_height = $new_height;
$this->new_width_groot = $new_width_groot;
$this->new_height_groot = $new_height_groot;
$this->image_name = $image_name;
$this->uploaded_file = $uploaded_file;
$this->filename = stripslashes($image_name);
$this->extension = explode(".", $image_name);
$this->filesize = filesize($uploaded_file);
}
function ExtensionCheck()
{
if (($this->extension[1] == "jpg") || ($this->extension[1] == "jpeg") || ($this->extension[1] == "png") || ($this->extension[1] == "gif"))
{
return true;
}
else
{
$this->error = 'Het bestand is niet toegestaan.';
return $this->error;
}
}
function SizeCheck()
{
if ($this->filesize < 10*1024*1024)
{
return true;
}
else
{
$this->error = 'Het geuploade bestand is te groot.';
return $this->error;
}
}
function ImageCreate()
{
if($this->extension[1] == "jpg" || $this->extension[1] == "jpeg" )
{
$this->src = imagecreatefromjpeg($this->uploaded_file);
}
elseif($this->extension[1] == "png")
{
$this->src = imagecreatefrompng($this->uploaded_file);
}
elseif($this->extension[1] == "gif")
{
$this->src = imagecreatefromgif($this->uploaded_file);
}
else
{
$this->error = 'Het bestand kon niet gemaakt worden.';
return $this->error;
}
}
function SetImageSize()
{
list($this->width,$this->height) = getimagesize($this->uploaded_file);
$this->tmp = imagecreatetruecolor($this->new_width, $this->new_height);
$this->tmp_groot = imagecreatetruecolor($this->new_width_groot, $this->new_height_groot);
}
function ImageCopy()
{
imagecopyresampled($this->tmp,$this->src,0,0,0,0,$this->new_width,$this->new_height,$this->width,$this->height);
imagecopyresampled($this->tmp_groot,$this->src,0,0,0,0,$this->new_width_groot,$this->new_height_groot, $this->width,$this->height);
}
function SetFilename()
{
$this->filename = '../../Images/Portfolio/'.$this->project_naam.'/'.$this->image_name;
$this->filename_groot = '../../Images/Portfolio/'.$this->project_naam.'/Groot/'.$this->image_name;
}
function CreateImg()
{
if($this->extension[1] == "jpg" || $this->extension[1] == "jpeg" )
{
imagejpeg($this->tmp,$this->filename,100);
imagejpeg($this->tmp_groot,$this->filename_groot,100);
}
elseif($this->extension[1] == "png")
{
imagepng($this->tmp,$this->filename);
imagepng($this->tmp_groot,$this->filename_groot);
}
elseif ($this->extension[1] == "gif")
{
imagegif($this->tmp,$this->filename,100);
imagegif($this->tmp_groot,$this->filename_groot,100);
}
else
{
$this->error = 'Het bestand kon niet aangemaakt worden.';
return $this->error;
}
}
function Image()
{
if ($this->ExtensionCheck() === true)
{
if ($this->SizeCheck() === true)
{
if ($this->ImageCreate() === true)
{
$this->SetImageSize();
$this->ImageCopy();
$this->SetFilename();
if ($this->CreateImg() === true)
{
return 'Het bestand is succesvol aangemaakt.';
}
else
{
return 'Het bestand kon niet aangemaakt worden.';
}
}
else
{
return $this->error;
}
}
else
{
return $this->error;
}
}
else
{
return $this->error;
}
}
?>
class ClsResizeImg
{
private $project_naam;
private $width;
private $height;
private $new_width;
private $new_height;
private $new_width_groot;
private $new_height_groot;
private $image_name;
private $uploaded_file;
private $filename;
private $filename_groot;
private $extension;
private $error;
private $src;
private $tmp;
private $tmp_groot;
function __construct($project_naam,$new_width,$new_height,$new_width_groot,$new_height_groot,$image_name,$uploaded_file)
{
$this->project_naam = $project_naam;
$this->new_width = $new_width;
$this->new_height = $new_height;
$this->new_width_groot = $new_width_groot;
$this->new_height_groot = $new_height_groot;
$this->image_name = $image_name;
$this->uploaded_file = $uploaded_file;
$this->filename = stripslashes($image_name);
$this->extension = explode(".", $image_name);
$this->filesize = filesize($uploaded_file);
}
function ExtensionCheck()
{
if (($this->extension[1] == "jpg") || ($this->extension[1] == "jpeg") || ($this->extension[1] == "png") || ($this->extension[1] == "gif"))
{
return true;
}
else
{
$this->error = 'Het bestand is niet toegestaan.';
return $this->error;
}
}
function SizeCheck()
{
if ($this->filesize < 10*1024*1024)
{
return true;
}
else
{
$this->error = 'Het geuploade bestand is te groot.';
return $this->error;
}
}
function ImageCreate()
{
if($this->extension[1] == "jpg" || $this->extension[1] == "jpeg" )
{
$this->src = imagecreatefromjpeg($this->uploaded_file);
}
elseif($this->extension[1] == "png")
{
$this->src = imagecreatefrompng($this->uploaded_file);
}
elseif($this->extension[1] == "gif")
{
$this->src = imagecreatefromgif($this->uploaded_file);
}
else
{
$this->error = 'Het bestand kon niet gemaakt worden.';
return $this->error;
}
}
function SetImageSize()
{
list($this->width,$this->height) = getimagesize($this->uploaded_file);
$this->tmp = imagecreatetruecolor($this->new_width, $this->new_height);
$this->tmp_groot = imagecreatetruecolor($this->new_width_groot, $this->new_height_groot);
}
function ImageCopy()
{
imagecopyresampled($this->tmp,$this->src,0,0,0,0,$this->new_width,$this->new_height,$this->width,$this->height);
imagecopyresampled($this->tmp_groot,$this->src,0,0,0,0,$this->new_width_groot,$this->new_height_groot, $this->width,$this->height);
}
function SetFilename()
{
$this->filename = '../../Images/Portfolio/'.$this->project_naam.'/'.$this->image_name;
$this->filename_groot = '../../Images/Portfolio/'.$this->project_naam.'/Groot/'.$this->image_name;
}
function CreateImg()
{
if($this->extension[1] == "jpg" || $this->extension[1] == "jpeg" )
{
imagejpeg($this->tmp,$this->filename,100);
imagejpeg($this->tmp_groot,$this->filename_groot,100);
}
elseif($this->extension[1] == "png")
{
imagepng($this->tmp,$this->filename);
imagepng($this->tmp_groot,$this->filename_groot);
}
elseif ($this->extension[1] == "gif")
{
imagegif($this->tmp,$this->filename,100);
imagegif($this->tmp_groot,$this->filename_groot,100);
}
else
{
$this->error = 'Het bestand kon niet aangemaakt worden.';
return $this->error;
}
}
function Image()
{
if ($this->ExtensionCheck() === true)
{
if ($this->SizeCheck() === true)
{
if ($this->ImageCreate() === true)
{
$this->SetImageSize();
$this->ImageCopy();
$this->SetFilename();
if ($this->CreateImg() === true)
{
return 'Het bestand is succesvol aangemaakt.';
}
else
{
return 'Het bestand kon niet aangemaakt worden.';
}
}
else
{
return $this->error;
}
}
else
{
return $this->error;
}
}
else
{
return $this->error;
}
}
?>
Sorry als het veel code is maar ben alles nodig dus:P
Gewijzigd op 24/05/2011 14:25:29 door Dennis meijer
bump?:P
Bijvoorbeeld: if ($this->ImageCreate() == true)
Maar de functie ImageCreate returnt alleen maar iets als er iets fout gaat. In alle andere gevallen wordt er niks gereturned.
Hier gaat het volgens mij ook niet goed:
if ($this->ExtensionCheck() == true)
ExtensionCheck() returnt in de "if" true... maar in de "else" wordt ook een waarde gereturned die ook true terug zal geven. Je moet dan werken met ===
if ($this->ExtensionCheck() === true)
Daarnaast return je variabeles die je tegelijkertijd probeert te setten. Dat lijkt me ook niet kloppen.
return $this->error = 'Het bestand kon niet aangemaakt worden.';
moet worden
$this->error = 'Het bestand kon niet aangemaakt worden.';
return $this->error;
Al deze bovengenoemde dingen komen meerdere keren voor, dus ff goed alles nalopen.
Ik heb de dingen die je hebt gezegd veranderd en heb het bovenstaande script geupdated alleen hij werkt nog steeds niet?
Nog meer suggesties?
laat je hele code nog eens ff zien...
Ik heb hem geupdated dus die code die er nu staat is de relevante code.