Image resize en thumbnail script

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

Richard

Richard

30/12/2005 18:24:00
Quote Anchor link
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
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
<?php  
    // Image converter & uploader
    function insertImage($upload) {  
      
        /* Insert array image system
      
        Format for array   : ResizeWidth, ResizeHeight, ResizeQuality, FolderLocation
        Example 1          : $image_set[] = array ('320', '240', '95', 'upload/images/resize/');
        Example 1          : $image_set[] = array ('240', '180', '90', 'upload/images/thumb/');
          
        Special format     : Use 'set' for no resize. (normal upload)
        Example            : $image_set[] = array ('set', 'set', 'set', 'upload/images/');
          
        NOTE : Quality settings are 1-100. FolderLocation should end with a slash '/'.
        */  
      
        // Fill this array with thumbnail settings

        $image_set = array ();  
        $image_set[] = array ('set', 'set', 'set', 'screenshots/');  
        $image_set[] = array ('100', '100', '90', 'thumbs/');  
          
        // Retrieve variables of the image file
        $file_type = $_FILES[$upload]['type'];  
        $file_temp = $_FILES[$upload]['tmp_name'];  
        $file_name = str_replace(array ("\\", "'", " "), array ("", "", "_"), strtolower($_FILES[$upload]['name']));  
        $file_ext = end(explode('.',$file_name));  
          
        // Array of ID numbers (only used if you enable MySQL option
        $image_array = array();  
          
        // Check for image type (pjpeg & x-png for IE upload bug)
        if (((($file_type == 'image/jpeg') or ($file_type == 'image/pjpeg')) and (($file_ext == 'jpg') or ($file_ext == 'jpeg'))) or  
            ((($file_type == 'image/png') or ($file_type == 'image/x-png')) and ($file_ext == 'png')) or  
            (($file_type == 'image/gif') and ($file_ext == 'gif'))) {  
          
            // Loops image thumbnail function for each row automatically
            if ($image_set_list = current($image_set)) {  
              
                do {  
                  
                    // Load settings from array row
                    $resize_width = current($image_set_list);  
                    $resize_height = next($image_set_list);  
                    $resize_quality = next($image_set_list);  
                    $file_path = next($image_set_list);  
      
                    // Name check & rename if exists
                    $file_path_check = $file_path.$file_name;  
                    $file_name_check = $file_name;  
                    for ($i=0; (file_exists($file_path_check)); $i++) {  
                        $file_name_check = $i."_".$file_name;  
                        $file_path_check = $file_path.$file_name_check;  
                    }
  
                    $file_name = $file_name_check;  
                    $file_path = $file_path_check;  

                    if ($resize_quality == "set") {      
                      
                        copy($file_temp, $file_path);  
      
                    }
else {  

                        // Different function for each type
                        if ((($file_type == 'image/jpeg') or ($file_type == 'image/pjpeg')) and (($file_ext == 'jpg') or ($file_ext == 'jpeg'))) {  
                            $picture_org = imagecreatefromjpeg($file_temp);  
                        }
else if ((($file_type == 'image/png') or ($file_type == 'image/x-png')) and ($file_ext == 'png')) {  
                            $picture_org = imagecreatefrompng($file_temp);  
                        }
else if (($file_type == 'image/gif') and ($file_ext == 'gif')) {  
                            $picture_org = imagecreatefromgif($file_temp);  
                        }
  
                          
                        // Retrieve original image size
                        $picture_org_w = imagesx($picture_org);  
                        $picture_org_h = imagesy($picture_org);  
                  
                        // Check aspect ratio of thumbnail and original picture
                        $ratio_org = $picture_org_w / $picture_org_h;  
                        $ratio_new = $resize_width / $resize_height;  
                          
                        // Calculate margin space to keep aspect ratio correct
                        if ($ratio_new >= $ratio_org) {  
                            $picture_margin_h = ($picture_org_h - ($picture_org_w / $ratio_new));  
                            $picture_margin_w = 0;  
                        }
else {  
                            $picture_margin_w = ($picture_org_w - ($picture_org_h * $ratio_new));  
                            $picture_margin_h = 0;  
                        }
  
                          
                        // Create thumbnail image
                        $picture_new=imagecreatetruecolor($resize_width, $resize_height);    
                        imagecopyresized($picture_new, $picture_org, 0, 0, $picture_margin_w/2, $picture_margin_h/2, $resize_width, $resize_height, $picture_org_w - $picture_margin_w, $picture_org_h - $picture_margin_h);  
                          
                        // Format thumbnail and move the files
                        if (($file_type == 'image/jpeg') or ($file_type == 'image/pjpeg')) {  
                            // JPEG generator
                            imagejpeg ($picture_new, $file_path, $resize_quality);  
                        }
else if (($file_type == 'image/png') or ($file_type == 'image/x-png')) {  
                            // PNG generator
                            imagepng ($picture_new, $file_path, $resize_quality);  
                        }
else if ($file_type == 'image/gif') {  
                            // GIF generator
                            imagegif ($picture_new, $file_path, $resize_quality);  
                        }
  
                          
                        // Delete temporary images          
                        imagedestroy($picture_new);  
                        imagedestroy($picture_org);  
                          
                        /* Optional : Change  /*  to  //*  to (un)enable this function
                        // Logs EVERY thumbnail, this is useful if you want to keep track of all files.
                        // This function logs the entire filepath (including filename)
                        mysql_query("
                            INSERT INTO images(
                                image_name
                            ) VALUES (
                                '$file_path
                            )                  
                        ") or die(mysql_error());
              
                        // Adds to list of MySQL entries. Format : ID, Name
                        $image_array[] = array (mysql_result(mysql_query("SELECT LAST_INSERT_ID()"), 0), $file_name);//*/
  
                          
                    }  
                      
                }
while ($image_set_list = next($image_set));  
                  
            }
  
              
            /* Optional : Change  /*  to  //*  to (un)enable this function
            // Logs only ONCE, this is useful if you don't have different loose images
            // In the other folders (to prevent duplicate names - auto-renaming)
            mysql_query("
                INSERT INTO images(
                    image_name
                ) VALUES (
                    '$file_name'
                )                  
            ") or die(mysql_error());

            // Adds to list of MySQL entries. Format : ID, Name
            $image_array[] = array (mysql_result(mysql_query("SELECT LAST_INSERT_ID()"), 0), $file_name);//*/  
              
            // Completed message

            echo "\n".'<br>Uw afbeelding is toegevoegd.';  
            return $image_array;  
              
        }
else {  
              
            // Image was not a valid type!
            echo "\n".'<br>Alleen JPG, PNG en GIF bestanden zijn toegestaan.';                      
            return false;  
              
        }  
          
    }  

function
imageForm($formset) {  
          
    echo '<div align="center">';  
    if (($formset == 'form') or ($formset == 'start')) {  
    echo '<form action="" method="post" enctype="multipart/form-data">';  
    }
  
    echo 'Afbeelding:<br>';  
    echo '<input type="file" name="upload"><br>';  
    echo '<input type="submit" name="insertImage" value="Verzenden">';  
    echo '<br>Alleen JPG, PNG en GIF bestanden zijn toegestaan.';  
    if (($formset == 'form') or ($formset == 'end')) {  
    echo '</form>';  
    }
  
    echo '</div>';  
          
    }
  
        
?>


Ik heb de mappen aangemaakt screenshots en thumbs aangemaakt met 775 attributen.
Als ik het probeer krijg ik de error '// Image was not a valid type! '

Wat doe ik fout?
 
PHP hulp

PHP hulp

17/11/2024 00:31:21
 
Jan Koehoorn

Jan Koehoorn

30/12/2005 18:44:00
Quote Anchor link
Voor een uploadscript is het veel te lang, dat kan allemaal veel compacter. Het zou me niets verbazen als dit niet goed gaat:

// Different function for each type
if ((($file_type == 'image/jpeg') or ($file_type == 'image/pjpeg')) and (($file_ext == 'jpg') or ($file_ext == 'jpeg'))) {
$picture_org = imagecreatefromjpeg($file_temp);
} else if ((($file_type == 'image/png') or ($file_type == 'image/x-png')) and ($file_ext == 'png')) {
$picture_org = imagecreatefrompng($file_temp);
} else if (($file_type == 'image/gif') and ($file_ext == 'gif')) {
$picture_org = imagecreatefromgif($file_temp);
}

die check zou ik er even uitgooien om te kijken wat er gebeurt.
 
Richard

Richard

30/12/2005 19:01:00
Quote Anchor link
Dan gebeurt er precies hetzelfde..
 

30/12/2005 20:56:00
Quote Anchor link
Weten jullie dan miss ook een beter script dat wel ongeveer hetzelfde doet?
 



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.