upload image PHP

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Sasa

sasa

25/02/2007 12:08:00
Quote Anchor link
Hallo,

Omdat ik zelf niet zo sterk ben in programmeren, en toch graag een "afbeeldingen-uploader" had gemaakt, ben ik op zoek gegaan naar scripts.
Ik heb zo dit gevonden...


UPLOAD.PHP:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
include('imagehandler.php');

if(!empty($_FILES['file'])){
    $error = upload($_FILES['file']['tmp_name'],'images/',$_FILES['file']['name'],800,800);
    if($error[0] == true){
        echo 'Upload is gelukt<p>';
    echo '<img src="images/$_FILES[\'file\'][\'name\']">';
    }
else{
        echo 'Error: '.$error[2];
    }
}
else{
    echo '
    <form enctype="multipart/form-data" method="post" action="upload.php">    
<table border="0"><tr><td>File:</td><td><input type="file" name="file" /></td></tr>

<tr><td>&nbsp;</td><td><input type="submit" value="Upload" /></td></tr>
</table>
  </form>'
;
}

?>



IMAGEHANDLER.PHP:


Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<?php
//function to resize / upload a photo save it to a dir
function upload ($tempfile,$dir,$file,$height,$width){
    //$tempfile = filename generated by the server when uploading a file
    //$dir = directory with the final location
    //$file = new filename
    //$height / $width explain enough i think
    
    //check if the file is realy uploaded

    if(!is_uploaded_file($tempfile)){
        $error[0] = false;
        $error[1] = -1;
        $error[2] = 'file is not uploaded';
    return $error;
    }
else{
    //get the dimensions of the file
    if(!$dim = getimagesize($tempfile)){
        $error[0] = false;
        $error[1] = -2;
        $error[2] = 'No image file';
    return $error;
    }
else{
    
    //0 = width
    //1 = height
    //2 = type
    //we want to calculte if the bigger then the maxsize if not keep it easy --> upload it

    if($dim[0] < $width && $dim[1] < $height){
        //move upload file
        if(!file_exists($dir.$file)){
        if(@move_uploaded_file($tempfile,$dir.$file)){
        $error[0] = true;
        $error[1] = 1;
        $error[2] = 'File uploaded';
        return $error;
        }
else{
        $error[0] = false;
        $error[1] = -3;
        $error[2] = 'Error during move upload file';
        return $error;    
        }
        }
else{
        $error[0] = false;
        $error[1] = -4;
        $error[2] = 'File allready exsits';
        return $error;    
        }        
    }
else{
        //we have to resize :(
        if($dim[0] > $dim[1]){
            $prop = $width / $dim[0];
            $dims[0] = $width;
            $dims[1] = round($dim[1] * $prop);
        }
else{
            $prop = $height / $dim[1];
            $dims[1] = $height;
            $dims[0] = round($dim[0] * $prop);
        }

        //we know the new size
        if($dim[2] == 2){
        if(!$mimage = @imagecreatefromjpeg($tempfile)){
        $error[0] = false;
        $error[1] = -4;
        $error[2] = 'Error';
        return $error;
        }
           }

           //we know the new size
        if($dim[2] == 1){
        if(!$mimage = @imagecreatefromgif($tempfile)){
        $error[0] = false;
        $error[1] = -4;
        $error[2] = 'Error';
        return $error;
        }
           }

           //we know the new size
        if($dim[2] == 3){
        if(!$mimage = @imagecreatefrompng($tempfile)){
        $error[0] = false;
        $error[1] = -4;
        $error[2] = 'Error';
        return $error;
        }
           }

           $im = @imagecreatetruecolor($dims[0],$dims[1]);
           @
imagecopyresampled($im, $mimage, 0, 0, 0, 0, $dims[0], $dims[1], $dim[0], $dim[1]);
          
         if(!file_exists($dir.$file)){
           if($dim[2] == 2){
        if(!@imagejpeg($im,$dir.$file,100)){
        $error[0] = false;
        $error[1] = -5;
        $error[2] = 'Imagecreate error';
        return $error;
        }
else{
        $error[0] = true;
        $error[1] = 2;
        $error[2] = 'Resized';
        return $error;    
        }    
        }
    
        if($dim[2] == 1){
        if(!@imagegif($im,$dir.$file)){
        $error[0] = false;
        $error[1] = -5;
        $error[2] = 'Imagecreate error';
        return $error;
        }
else{
        $error[0] = true;
        $error[1] = 2;
        $error[2] = 'Resized';
        return $error;    
        }    
        }

        
        if($dim[2] == 3){
        if(!@imagepng($im,$dir.$file)){
        $error[0] = false;
        $error[1] = -5;
        $error[2] = 'Imagecreate error';
        return $error;
        }
else{
        $error[0] = true;
        $error[1] = 2;
        $error[2] = 'Resized';
        return $error;      
        }    
           }
        }
else{
        $error[0] = false;
        $error[1] = -4;
        $error[2] = 'File allready exsits';
        return $error;        
        }

        imagedestroy($im);
        imagedestroy($mimage);  
           //end resize
        }

    }
    }
}

?>



De afbeelding is nu wel goed upgeload, maar hoe kan ik nu, als ik een random upgeloade afbeelding ergens nodig heb, die gebruiken m.a.w. wat is de source van deze afbeelding, zoals

Code (php)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
5
<?php

 echo 'Upload is gelukt<p>';
    echo '<img src="images/$_FILES[\'file\'][\'name\']">';
?>


Alvast bedankt!
 
Er zijn nog geen reacties op dit bericht.



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.