Skip to content

Instantly share code, notes, and snippets.

@man-oi
Created August 26, 2010 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save man-oi/551342 to your computer and use it in GitHub Desktop.
Save man-oi/551342 to your computer and use it in GitHub Desktop.
<?php
function create_thumb($img_src, $thumb_width, $thumb_height, $thumb_dir, $id, $setW = false, $setH = false) {
$image_infos = @getimagesize($img_src);
$width = $image_infos[0];
$height = $image_infos[1];
$type = $image_infos[2];
$name = basename($img_src);
if ($setW == true || $setH == true) {
if ($setW == true) {
$new_width = intval($thumb_width); // Zuweisen der neuen Breite
$new_height = ceil($height * $new_width / $width);
} else {
$new_height = intval($thumb_height);
$new_width = ceil($width * $new_height / $height); // Zuweisen der neuen Höhe
}
} else {
if($width < $height) { // Überprüfen ob das Bild Hoch- oder Querformat ist
$new_width = ceil((intval($thumb_width) / $height) * $width);
$new_height = intval($thumb_height); // Zuweisen der neuen Höhe
} else {
$new_height = ceil((intval($thumb_height) / $width) * $height);
$new_width = intval($thumb_width); // Zuweisen der neuen Breite
}
}
switch ($type) {
case 1:
if (imagetypes() & IMG_GIF) { // Überprüfen ob das Bildformat untestützt wird
$orginal = imagecreatefromgif($img_src); // Bild aus dem Orginalbild erstellen
$thumb = imagecreatetruecolor($new_width, $new_height); // Das Thumbnailbild erstellen
imagecopyresampled($thumb, $orginal, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
if (!imagegif($thumb, $thumb_dir.$id."_".$name)) die("no gif"); // Bild speichern
}
break;
case 2:
if (imagetypes() & IMG_JPG) { // Überprüfen ob das Bildformat untestützt wird
$orginal = imagecreatefromjpeg($img_src); // Bild aus dem Orginabild erstellen
$thumb = imagecreatetruecolor($new_width, $new_height); // Das Thumbnailbild erstellen
imagecopyresampled($thumb, $orginal, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
if (!imagejpeg($thumb, $thumb_dir.$id."_".$name)) die("no jpg"); // Bild speichern
}
break;
case 3:
if (imagetypes() & IMG_PNG) { // Überprüfen ob das Bildformat untestützt wird
$orginal = imageCreateFromPNG($img_src); // Bild aus dem Orginalbild erstellen
$thumb = imagecreatetruecolor($new_width, $new_height); // Das Thumbnailbild erstellen
imagecopyresampled($thumb, $orginal, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
if (!imagepng($thumb, $thumb_dir.$id."_".$name)) die("no png"); // Bild speichern
}
break;
default:
error_message('Das Bildformat wird nicht unterstützt'); // Fehlermeldung ausgeben, wenn das Bildformat nicht unterstützt wird
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment