PDA

Archiv verlassen und diese Seite im Standarddesign anzeigen : Ergänzung Thumbnail Script


littleMonster
15.06.2011, 21:13
Hallo Andreas,

ich war so frei und habe dein Thumbnail-Script (http://www.phpbuddy.eu/thumbnail-funktion.html) etwas angepasst. Die Erstellung des quadratischen Thumbs habe ich so angepasst, das auch fest vorgegebene Seitenlängen möglich sind.
Hierzu habe ich einen Bildfaktor eingeführt, der dies steuert.

Möchte ich wie gehabt ein quadratisches Thumb erhalten, rufe ich die Funktion z.B. mit 160, 160 und true auf.
makeThumb( beispielbild.jpg, 160, 160, true, 90, beispielbild_thumb.jpg );

Bei einem rechteckigen Seitenverhältnis:
makeThumb( beispielbild.jpg, 160, 120, true, 90, beispielbild_thumb.jpg );

Das proportionale Verhalten ist gleich geblieben, meine Änderungen beziehen sich nur auf den Part ab else if ($quadratisch === true).
Getestet mit hochformatigen und querformatigen Bildern.


function makeThumb( $bild, $maxbreite = 100, $maxhoehe = 100, $quadratisch = false, $qualitaet = 80, $speichern = NULL )

{

// Bilddaten auslesen

list( $original_breite, $original_hoehe, $typ, $imgtag, $bits, $channels, $mimetype ) = @getimagesize( $bild );



switch ($typ)

{

case '1': $originalbild = imagecreatefromgif( $bild ); break;

case '2': $originalbild = imagecreatefromjpeg( $bild ); break;

case '3': $originalbild = imagecreatefrompng( $bild ); break;

default :

header( 'Content-Type: text/html; charset=utf-8' );

die( '<h3>Die übergebene Datei ist keine Grafik!</h3>' );

break;

}



if ($quadratisch === false)

{

// Höhe und Breite für proportionales Thumbnail berechnen

if ($original_breite > $maxbreite || $original_hoehe > $maxhoehe)

{

$thumb_breite = $maxbreite;

$thumb_hoehe = $maxhoehe;

if ($thumb_breite / $original_breite * $original_hoehe > $thumb_hoehe)

{

$thumb_breite = round( $thumb_hoehe * $original_breite / $original_hoehe );

}

else

{

$thumb_hoehe = round( $thumb_breite * $original_hoehe / $original_breite );

}

}

else

{

$thumb_breite = $original_breite;

$thumb_hoehe = $original_hoehe;

}

// Thumbnail oder Image erstellen

$thumb = imagecreatetruecolor( $thumb_breite, $thumb_hoehe );

imagecopyresampled( $thumb, $originalbild, 0, 0, 0, 0, $thumb_breite, $thumb_hoehe, $original_breite, $original_hoehe );

}

else if ($quadratisch === true)

{

// Kantenlänge für quadratisches Thumbnail ermitteln

$originalkantenlaenge = $original_breite < $original_hoehe ? $original_breite : $original_hoehe;
// Bildfaktor, Seitenverhaeltnis des Thumbs ermitteln

$bildfaktor = $maxbreite / $maxhoehe;
$tmpbild = imagecreatetruecolor( round($originalkantenlaenge*$bildfaktor), $originalkantenlaenge );

if ($original_breite > $original_hoehe)

{
// Kantenlaenge fuer querformatige Bilder anpassen
$originalkantenlaenge = round($original_hoehe*$bildfaktor);

imagecopy( $tmpbild, $originalbild, 0, 0, round($original_breite-$originalkantenlaenge)/2, 0, $original_breite, $original_hoehe );

}

else if ($original_breite <= $original_hoehe )

{
// Kantenlaenge fuer hochformatige Bilder anpassen
$originalkantenlaenge = round($original_breite/$bildfaktor);

imagecopy( $tmpbild, $originalbild, 0, 0, 0, round($original_hoehe-$originalkantenlaenge)/2, $original_breite, $original_hoehe );

}

// Thumbnail oder Image erstellen

$thumb = imagecreatetruecolor( $maxbreite, $maxhoehe );
if ($original_breite > $original_hoehe)

{

imagecopyresampled( $thumb, $tmpbild, 0, 0, 0, 0, $maxbreite, $maxhoehe, round($original_hoehe*$bildfaktor), $original_hoehe );
}
else if ($original_breite <= $original_hoehe )

{
imagecopyresampled( $thumb, $tmpbild, 0, 0, 0, 0, $maxbreite, $maxhoehe, $original_breite, round($original_breite/$bildfaktor) );
}
}



// Korrekten Image Header senden, wenn nicht gespeichert wird

if (!$speichern) { header( 'Content-Type: ' . $mimetype ); }

switch ($typ)

{

case '1': imagegif( $thumb, $speichern ); break;

case '2': imagejpeg( $thumb, $speichern, $qualitaet ); break;

case '3': imagepng( $thumb, $speichern ); break;

default : imagejpeg( $thumb, $speichern, $qualitaet ); break;

}

// Speicher freigeben

imagedestroy( $thumb );

}

P.S. Mit deinen zahlreichen hervorragenden Tutorials, Codeschnipsel habe ich mir nun ein komplettes passwortgeschütztes Uploadscript mit Thumbnail Erstellung geschrieben (oder besser zusammen gebastelt http://www.phpbuddy.eu/forum/images/icons/icon10.gif ).
Yeah - ich php Dummie habe das wirklich geschaft ... xD