PHP Thumbails

Coach

Aktives Mitglied
Hallo!

Ich möchte verschieden große Thumbnails erstellen zur Vorschau einer Gallerie.
Dabei sollen diese Thmbnails jedoch alle eine einheitliche Größe haben.
Das Problem wenn ich ne feste Größe angebe: Die Bilder sind verzerrt.. sei es in der Länge oder der Breite.

Ist es mit PHP möglich, nur nen Bildausschnitt als Thumbail zu nehmen?
Oder kann mir jmd ne andere Möglichkeit vorschlagen?
 
Ich weiß nicht, ob es in PHP da etwas Vordefiniertes gibt (bin ja nicht so der PHP-Nutzer).

Aber für server-daten habe ich mir das einfach selbst zusammengebaut:

Man kann einen Gesamtrahmen definieren, bsp. 185 Pixel Höhe / 135 Pixel Breite.

Und dann sind das eben Fallunterscheidungen. Etwas Fummelarbeit, aber es geht.

Und man kann dann bsp. einen weißen Streifen dazusetzen, damit alle Bilder mit denselben Maßen rauskommen.
 
Mach dich mal etwas im Internet über Imagemagick schlau.
Besonders über die Befehle crop und scale.

Grüsse, Crazy
 
CODE function resize($image,$width,$height = 0,$scale = false) {
$src = @GetImageSize($image);
$src_w = $src[0];
$src_h = $src[1];
if(is_array($src)) {
$quotient = $src_w / $src_h;

if($height==0) $height = $width * $quotient;
if($width==0) $width = $height * $quotient;

if($scale && $src_w < $width && $src_h < $height) {
$src_w = $src_w + $width;
$src_h = $src_h + $height;
}
if($src_w > $width) {
$dst_w = $width;
$dst_h = $dst_w / $quotient;
}
if($src_h > $height) {
$dst_h = $height;
$dst_w = $dst_h * $quotient;
}

if($src_h == $src_w) {
$dst_h = $src_h;
$dst_w = $src_w;
}
if ( $dst_h > $height || $dst_w > $width) {
$dst_h = $height;
$dst_w = $dst_h * $quotient;
if ($dst_w > $width) {
$dst_w = $width;
$dst_h = $width / $quotient;
}
}
if($src_w < $width && $src_h < $height) {
$dst_w = $src_w;
$dst_h = $src_h;
}
$dst = array("width" => $dst_w, "height" => $dst_h);
}
return $dst;
}

public static function resizeToFile($sourcefile, $dest_x, $dest_y, $targetfile = false, $endung = "jpg", $jpegqual = 90) {
if(!$targetfile) $targetfile = $sourcefile;
/* Get the dimensions of the source picture */
$picsize=getimagesize("$sourcefile");

$source_x = $picsize[0];
$source_y = $picsize[1];
switch($endung) {
case "gif": $source_id = imageCreateFromGIF("$sourcefile"); break;
case "png": $source_id = imageCreateFromPNG("$sourcefile"); break;
default: $source_id = imageCreateFromJPEG("$sourcefile");
}
$targetfile = str_replace($endung, "jpg", $targetfile);

/* Create a new image object (not neccessarily true colour) */

$target_id=imagecreatetruecolor($dest_x, $dest_y);

/* Resize the original picture and copy it into the just created image
object. Because of the lack of space I had to wrap the parameters to
several lines. I recommend putting them in one line in order keep your
code clean and readable */


$target_pic=imagecopyresampled($target_id,$source_id, 0,0,0,0, $dest_x,$dest_y, $source_x,$source_y);

/* Create a jpeg with the quality of "$jpegqual" out of the
image object "$target_pic".
This will be saved as $targetfile */

imagejpeg ($target_id,"$targetfile",$jpegqual);

return $targetfile;
}


mit Resize die Maße fürs Thumbnail berechnen und mit resizeToFile speichern (oder beides kombinieren, je nachdem wie du's brauchst).

Die erste Funktion von mir und die zweite weiß ich nimmer wann die in meine Lib gekommen ist
wink.gif


Viel Erfolg damit
 
Zurück
Oben