Dies ist eine alte Version des Dokuments!
[PHP] Bild verkleinern
Beschreibung
Dieses kleine PHP-Script erzeugt eine verkleinerte Version eines Bildes. Dabei kann es sich um jeden Dateityp handeln, mit dem die GD-Library umgehen kann.
Benutzung
Die Funktion wird aufgerufen mit:
- dem Pfad zur Bilddatei
- der maximalen Breite, die das Bild nachher haben darf
- der maximalen Höhe, die das Bild nachher haben darf
- optional einen Dateinamen, in dem das verkleinerte Bild gespeichert werden soll (wird keiner angegeben, wird der korrekte Header gesetzt und das Bild an den Browser gesendet)
Wenn die Erzeugung des Bildes nicht funktioniert, wird eine Exception geworfen. Diese sollte man abfangen und entsprechend behandeln.
Beispiel
try { createResizedImage('path/to/source_image.gif', 200, 150, 'thumbnails/thumb_1.png'); } catch (Exception e) { echo 'An error occured during thumbail generation: ', $e->getMessage(); }
Code
/** * Creates a resized version of a given image file. * Can handle all file types GD is able to read. It will attempt to resize the image * so that it fits both given maximum values for width and height. The image never * will be stretched. * If an output file is specified, the new image will be stored in this file. * The output will always be a PNG image! * If no output file is specified, the functions sets the needed headers and directly * send the image to the browser. * * Usage: * try { * createResizedImage('path/to/source_image.gif', * 200, * 150, * 'thumbnails/thumb_1.png'); * } * catch (Exception e) { * echo 'An error occured during thumbail generation: ', $e->getMessage(); * } * * @param path Path to the source image file * @param width Maximum width for the new image * @param height Maximum height for the new image * @param outfile File to which the new image should be written, optional * * @throws If there are any problems reading the file or writing the output file, an * exception will be thrown. */ function createResizedImage($path, $width, $height, $outfile = false) { // Check the given values for errors if (!file_exists($path)) throw new Exception("File '".$path."' doesn't exist!"); if (!$size = @getimagesize($path)) throw new Exception("Could not obtain image dimensions for '".$path."'!"); if (!is_numeric($width) || !is_numeric($height) || ($width < 1) || ($height < 1)) { trigger_error("createResizedImage() expects second and third parameter to be a number greater 1!", E_USER_ERROR); break; } // Load the source image switch ($size[2]) { case 1: $src_img = @imagecreatefromgif($path); break; case 2: $src_img = @imagecreatefromjpeg($path); break; case 3: $src_img = @imagecreatefrompng($path); break; default: throw new Exception("Image '".$path."' is of an unknown type and cannot be read!"); } if (!$src_img) throw new Exception("Image '".$path."' could not be read!"); // Calculate new image dimensions $new_width = $size[0]; $new_height = $size[1]; if (($size[0] > $width) || ($size[1] > $height)) { if (($size[0] - $width) > ($size[1] - $height)) { $new_width = $width; $new_height = $size[1] * ($new_width / $size[0]); } else { $new_height = $height; $new_width = $size[0] * ($new_height / $size[1]); } } // Create new image $new_img = imagecreatetruecolor($new_width, $new_height); if (function_exists('imagecopyresampled')) imagecopyresampled($new_img, $src_img, 0, 0, 0, 0, $new_width, $new_height, $size[0], $size[1]); else imagecopyresized($new_img, $src_img, 0, 0, 0, 0, $new_width, $new_height, $size[0], $size[1]); // Send image to browser or write it to the output file if (!$outfile) { @header("Content-Type: image/png"); imagepng($new_img); } else { if (!is_writeable($outfile)) throw new Exception("The output file '".$outfile."' is not writeable!"); if (!imagepng($new_img, $outfile)) throw new Exception("The new image could not be written to '".$outfile."'!"); } // Clean up imagedestroy($src_img); imagedestroy($new_img); }