Suchen
PHP-Funktionen
Bildklasse für die GD-Bibliothek
Diese Klasse erzeugt Vorschaubilder (Thumbnails) aus Bildateien mittels der GD-Bildbibleothek.
Autor: Oliver Gueffroy
1 class image_thumbnail 2 { 3 var $source_file; 4 var $target_path; 5 var $target_file; 6 var $file_info = array(); 7 8 function image_thumbnail($source_file, $target_path, $max_width, $max_height, $keep_ratio = true, $type = NULL) 9 { 10 $this->file_info['error'] = 0; 11 $this->source_file = $source_file; 12 $this->target_path = $target_path; 13 $this->set_size($max_width, $max_height, $keep_ratio); 14 15 if($type == NULL) 16 $this->file_info['type'] = $this->get_file_extensition($source_file); 17 else 18 $this->file_info['type'] = $type; 19 } 20 21 function set_size($max_width, $max_height, $keep_ratio) 22 { 23 $file_info = @getimagesize($this->source_file); 24 25 if(intval($max_width) <= 0) 26 $max_width = $file_info && $file_info[0] > 0 ? $file_info[0] : 1; 27 28 if(intval($max_height) <= 0) 29 $max_height = $file_info && $file_info[1] > 0 ? $file_info[1] : 1; 30 31 if($keep_ratio && $file_info) 32 { 33 if($file_info[0] / $max_width < $file_info[1] / $max_height) 34 { 35 $this->file_info['width'] = round($max_height * ($file_info[0] / $file_info[1])); 36 $this->file_info['height'] = $max_height; 37 } 38 else 39 { 40 $this->file_info['width'] = $max_width; 41 $this->file_info['height'] = round($max_width * ($file_info[1] / $file_info[0])); 42 } 43 } 44 else 45 { 46 $this->file_info['width'] = $max_width; 47 $this->file_info['height'] = $max_height; 48 } 49 } 50 51 function has_error() 52 { 53 return $this->file_info['error'] != 0; 54 } 55 56 function get_error() 57 { 58 switch($this->file_info['error']) 59 { 60 case 1: 61 return 'Die Quelldatei konnte nicht gefunden werden oder war nicht lesbar.'; 62 63 case 2: 64 return 'Das Zielverzeichnis existiert nicht und konnte nicht angelegt werden.'; 65 66 case 3: 67 return 'Das Zielverzeichnis ist schreibgeschützt.'; 68 69 case 4: 70 return 'Der gewünschte Bildtyp für die Zieldatei wird von der verwendeten Bildbibliothek nicht unterstützt.'; 71 72 case 5: 73 return 'Die Quelldatei konnte nicht geöffnet werden, der Bildtyp wird von der verwendeten Bildbibliothek nicht unterstützt.'; 74 75 case 6: 76 return 'Das Thumbnail konnte nicht geschrieben werden, eventuell wird der gewünschte Bildtyp von der verwendeten Bildbibliothek nicht unterstützt oder es konnte nicht in das Zielverzeichnis geshcrieben werden.'; 77 78 case 99: 79 return 'Die GD-Bildbiblieothek wurde nicht gefunden.'; 80 } 81 } 82 83 function get_file_extensition($filename) 84 { 85 if(strpos($filename, '.', 0) === false) 86 return substr($filename, -3); 87 else 88 return strtolower(preg_replace("/^(.*?)\./", "", $filename)); 89 } 90 91 function get_image_types() 92 { 93 $image_types = array(); 94 95 if(function_exists('imagetypes')) 96 { 97 if(imagetypes() & IMG_GIF) 98 $image_types['gif'] = 'gif'; 99 100 if(imagetypes() & IMG_JPEG) 101 $image_types['jpg'] = 'jpg'; 102 103 if(imagetypes() & IMG_PNG) 104 $image_types['png'] = 'png'; 105 106 if(imagetypes() & IMG_WBMP) 107 $image_types['wbmp'] = 'wbmp'; 108 } 109 110 return $image_types; 111 } 112 113 function generate_thumbnail() 114 { 115 if(!file_exists($this->source_file) || !is_readable($this->source_file)) 116 { 117 $this->file_info['error'] = 1; 118 return false; 119 } 120 121 /* Try to create a sub dir in image directory, if not exist. The user 122 * will get a warning, if this operation fails. 123 */ 124 if(!file_exists($this->target_path) && !@mkdir($this->target_path, 0755)) 125 { 126 $this->file_info['error'] = 2; 127 return false; 128 } 129 130 if(!is_writable($this->target_path) || !@is_dir($this->target_path)) 131 { 132 $this->file_info['error'] = 3; 133 return false; 134 } 135 136 if(!in_array($this->file_info['type'], $this->get_image_types())) 137 { 138 $this->file_info['error'] = 4; 139 return false; 140 } 141 142 $this->target_file = md5(microtime()) . '.' . $this->file_info['type']; 143 $this->file_info['extenstion'] = $this->get_file_extensition($this->source_file); 144 145 // check weather the gd-image module is installed or not 146 if(function_exists('imagetypes')) 147 { 148 $source_image = false; 149 150 switch($this->file_info['extenstion']) 151 { 152 case 'gif': 153 if(function_exists('imagecreatefromgif')) 154 $source_image = @imagecreatefromgif($this->source_file); 155 break; 156 157 case 'jpg': 158 case 'jpeg': 159 case 'pjpg': 160 case 'pjpeg': 161 if(function_exists('imagecreatefromjpeg')) 162 $source_image = @imagecreatefromjpeg($this->source_file); 163 break; 164 165 case 'png': 166 if(function_exists('imagecreatefrompng')) 167 $source_image = @imagecreatefrompng($this->source_file); 168 break; 169 170 case 'wbmp': 171 if(function_exists('imagecreatefromwbmp')) 172 $source_image = @imagecreatefromwbmp($this->source_file); 173 break; 174 } 175 176 if(!$source_image) 177 { 178 $this->file_info['error'] = 5; 179 return false; 180 } 181 182 $source_image_width = imagesx($source_image); 183 $source_image_height = imagesy($source_image); 184 185 /* Try to use gd 2.0 and the better resampling method. 186 */ 187 if(function_exists('imagecreatetruecolor') && function_exists('imagecopyresampled')) 188 { 189 $target_image = @imagecreatetruecolor($this->file_info['width'], $this->file_info['height']); 190 191 if($target_image) 192 imagecopyresampled($target_image, $source_image, 0,0, 0,0, $this->file_info['width']+1, $this->file_info['height']+1, $source_image_width, $source_image_height); 193 } 194 195 /* If gd 2.0 is not installed or the php version is to old use the old 196 * resizing method. 197 */ 198 if(!$target_image) 199 { 200 $target_image = @imagecreate($this->file_info['width'], $this->file_info['height']); 201 202 if($target_image) 203 imagecopyresized($target_image, $source_image, 0,0, 0,0, $this->file_info['width']+1, $this->file_info['height']+1, $source_image_width, $source_image_height); 204 } 205 206 $created_image = false; 207 208 switch($this->file_info['type']) 209 { 210 case 'gif': 211 if(function_exists('imagegif')) 212 $created_image = imagegif($target_image, $this->target_path . $this->target_file); 213 break; 214 215 case 'jpg': 216 if(function_exists('imagejpeg')) 217 $created_image = imagejpeg($target_image, $this->target_path . $this->target_file); 218 break; 219 220 case 'png': 221 if(function_exists('imagepng')) 222 $created_image = imagepng($target_image, $this->target_path . $this->target_file); 223 break; 224 225 case 'wbmp': 226 if(function_exists('imagewbmp')) 227 $created_image = imagewbmp($target_image, $this->target_path . $this->target_file); 228 break; 229 } 230 231 imagedestroy($source_image); 232 imagedestroy($target_image); 233 234 if(!$created_image) 235 { 236 $this->file_info['error'] = 6; 237 return false; 238 } 239 } 240 else 241 { 242 $this->file_info['error'] = 99; 243 return false; 244 } 245 246 $this->file_info['size'] = @filesize($this->target_path . $this->target_file); 247 248 return true; 249 } 250 251 function get_file_info() 252 { 253 $this->file_info['target_file'] = $this->target_file; 254 $this->file_info['target_path'] = $this->target_path; 255 256 return $this->file_info; 257 } 258 }
Der erste Parameter ist das Quellbild, der zweite gibt das Zielverzeichnis an (dies muss für den Webserver schreibbar sein). Die maximale Bildbreite und Bildhöhe für das neue Thumbnail wird im dritten und vierten Parameter angegeben. Der fünfte Parameter gibt an, ob das Seitenverhältnis beibehalten werden soll. Im fünften Parameter kann der Dateityp des Thumbnails angegeben werden, gültige Werte sind 'jpg', 'gif', 'png' und 'wbmp'. 'gif' wird nicht von jeder Version der GD-Bildbiblieothek unterstützt.
1 $thumb = new image_thumbnail('image.jpg', './', '400', '400'); 2 3 if($thumb->generate_thumbnail()) 4 echo 'Das Thumbnail wurde generiert: <a href="' . $thumb->target_file . '">Anzeigen</a>'; 5 else 6 die($thumb->get_error());
Kommentare (0 Stück)
Du musst registriert und eingeloggt sein, um Kommentare erstellen zu dürfen!