image resize script probleempje!
Gewijzigd op 01/01/1970 01:00:00 door R
Gewijzigd op 01/01/1970 01:00:00 door R
Zet eens error_reporting(E_ALL); bovenaan je script.
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
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
<?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;
}
}
}
//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 acces',E_USER_WARNING);
}
}else{
trigger_error('File is not uploaded',E_USER_WARNING);
return false;
}
}
}
//voorbeeld
$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;
}
}
}
//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 acces',E_USER_WARNING);
}
}else{
trigger_error('File is not uploaded',E_USER_WARNING);
return false;
}
}
}
//voorbeeld
$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';
}
?>
Gewijzigd op 01/01/1970 01:00:00 door Eris -
Gewijzigd op 01/01/1970 01:00:00 door R
het is vaak handiger te beginnen met de 'simpele' PHP-tutorials dan te beginnen met een moeilijk script zonder basis.. :)
Ik hoop dat je wel een beetje beginselen van PHP hebt!
Verder include (roep je iets aan) met
Gewijzigd op 01/01/1970 01:00:00 door Harmen
Harmen:
sorry pik maar op zo een gare reactie zit ik niet te wachten,,, je maakt me hier een beetje belachelijk.... maargoed BESTE harmen....
ik denk dat ik maar eens ga "beginnen" met tutorials... dat is het beste wat ik kan doen toch?
dan hoef jij natuurlijk niet te brainstormen over mijn problemen.... daar krijg je zeker hoofdpijn van he?
ja sorry mijn fout om hier om hulp te vragen want daar is php hulp natuuurlijk niet voor.......... toch?
Gewijzigd op 01/01/1970 01:00:00 door R
...