Hallo,
in einer Bildergalerie oder ähnlichem ist es oft notwendig, eine Vorschau für die vorhandenen Bilder zu erstellen, um geringe Ladezeiten garantieren zu können.
Ich habe einen kleinen Code-Schnipsel, der diese Vorschaubilder erstellen soll.
Meine Bitte ist, dass Ihr ihn Euch mal zu Gemüte führt und mir ein kleines Feedback gebt, was noch angepasst und erweitert werden könnte / sollte.
Danke
Grüße
Der Schnipsel
in einer Bildergalerie oder ähnlichem ist es oft notwendig, eine Vorschau für die vorhandenen Bilder zu erstellen, um geringe Ladezeiten garantieren zu können.
Ich habe einen kleinen Code-Schnipsel, der diese Vorschaubilder erstellen soll.
Meine Bitte ist, dass Ihr ihn Euch mal zu Gemüte führt und mir ein kleines Feedback gebt, was noch angepasst und erweitert werden könnte / sollte.
Danke

Grüße
Der Schnipsel
PHP:
<?php
/*
What does this to any image?
This file creates a thumbnail of any via GET['src'] specified JPEG-Image
Width and height of the new thumbnail has to be specified via GET['width'] and GET['height']
You can also specify a background color via GET['bgcolor']
*/
$dflt_width = 100; // default width, only needed if GET['width'] is not specified
$dflt_height = 100; // default height, only needed if GET['height'] is not specified
$dflt_bgcolor = "#000000"; // default bgcolor, if GET['bgcolor'] is not specified
/*
transforms any specified hex-color into rgb and returns an array of r, g, b
parameter:
- color: hex-code
*/
function html2rgb($color)
{
if ($color[0] == '#')
$color = substr($color, 1);
if (strlen($color) == 6)
list($r, $g, $b) = array($color[0].$color[1],
$color[2].$color[3],
$color[4].$color[5]);
elseif (strlen($color) == 3)
list($r, $g, $b) = array($color[0].$color[0], $color[1].$color[1], $color[2].$color[2]);
else
return false;
$r = hexdec($r); $g = hexdec($g); $b = hexdec($b);
return array($r, $g, $b);
}
header("Content-Type: image/jpeg"); // send header
$thmbnl_width = isset($_GET["width"]) ? $_GET["width"] : $dflt_width; // get thumbnail width
$thmbnl_height = isset($_GET["height"]) ? $_GET["height"] : $dftl_height; // get thumbnail height
$thmbnl_bgcolor = isset($_GET["bgcolor"]) ? html2rgb($_GET["bgcolor"]) : $dflt_bgcolor; // get thumbnail background-color
$src_filename = isset($_GET["src"]) ? $_GET["src"] : null; // get filename of specified image
$src_size = @getimagesize($src_filename); // get size of specified image
$src_width = is_array($src_size) ? $src_size[0] : $dflt_width; // width
$src_height = is_array($src_size) ? $src_size[1] : $dfl_height; // height
$src = @imagecreatefromjpeg($src_filename); // load image
$thumbnail = imagecreatetruecolor($thmbnl_width, $thmbnl_height); // create new true color image
$bgcolor = imagecolorallocate($thumbnail, $thmbnl_bgcolor[0], $thmbnl_bgcolor[1], $thmbnl_bgcolor[2]); // create image background-color
imagefill($thumbnail, 0, 0, $bgcolor); // fill image with specified color
$src_props = $src_width / $src_height; // get image proportions
$thmbnl_props = $thmbnl_width / $thmbnl_height; // get thumbnail proportions
if($src_width > $thmbnl_width or $src_height > $thmbnl_height) // image dimensions greater than thumbnail dimensions?
{
if($src_width >= $src_height and $thmbnl_props <= $src_props) // width >= height, thumbnail proportions <= image proportions?
{
$width = $thmbnl_width; // use thumbnail width
$height = ($thmbnl_width / $src_width) * $src_height; // calc height (percentage)
}
else
{
$height = $thmbnl_height; // use thumbnail height
$width = ($thmbnl_height / $src_height) * $src_width; // calc width (percentage)
}
}
else // use image dimensions
{
$width = $src_width;
$height = $src_height;
}
imagecopyresized($thumbnail, $src, ($thmbnl_width - $width) / 2, ($thmbnl_height - $height) / 2, 0, 0, $width, $height, $src_width, $src_height); // resize image and copy to thumbnail
imagejpeg($thumbnail); // ouput thumbnail
?>