PHP Thumbnail eines Bildes mit php erstellen

Clocker 2800+

Lt. Commander
Registriert
Apr. 2007
Beiträge
1.409
Hi.

Wer kann mir eine Funktion schreiben,
die ein Thumbnail-Bild von einem großen (jpg, jpeg, bmp, png, gif) Bild erstellt und als neues Bild abspeichert ?
 
Hallo,

ich verstehe deine Frage nicht ganz - möchtest du eine einfache Online Bildvorschau mit Thumbnails erstellen?
 
Ich möchte von einem großen Bild ein Thumbnail erstellen und es als neues (kleines) Thumbnail-Bild abspeichern.
--> Also als neue Bilddatei.

Der Link ist schon mal nicht schlecht, aber bei allen Dateien (png, jpg, gif) fehlt dann die Transparenz und bei gif-Dateien die Animation.
 
Zuletzt bearbeitet:
Hi, dafür kannst du meine Funktion benutzten:

PHP:
function ImageResize($type,$src,$des,$desx,$desy,$qual) //type = 1 (JPG); 2 (PNG); 2 (GIF)
// Type; Quellpfad, Zielpfad, Zielgröße-X, Zielgröße Y, JPEG Qualität (%)
{
$mw = $desx; // max width
$mh = $desy; // max height
$des.= ".jpg";
if($type === 1)									#JPG
	{
	$src.= ".jpg";
$im = imagecreatefromjpeg( $src );
$ow = imagesx( $im );
$oh = imagesy( $im );
if( $ow > $mw OR $oh > $mh ) {					#Wenn Bild vergrößert werden muss
    if( $ow > $oh ) {							#Wenn Breite größer wie Höhe
        $tnw = $mw;
        $tnh = $tnw * $oh / $ow;
    } elseif( $oh > $ow ) {
        $tnw = $mw;
        $tnh = $tnw * $oh / $ow;
    } else {
        $tnh = $mh;
        $tnw = $tnh * $ow / $oh;
    }
} else {
    $tnw = $ow;
    $tnh = $oh;

if( $ow < $mw OR $oh < $mh) {
	$tnw = $mw;
	$tnh = $mw;
}
}
$imtn = imagecreatetruecolor( $tnw, $tnh );
$originaltransparentcolor = imagecolortransparent( $im );
if(
    $originaltransparentcolor >= 0
    && $originaltransparentcolor < imagecolorstotal( $im )
) {
    $transparentcolor = imagecolorsforindex( $im, $originaltransparentcolor );
    $newtransparentcolor = imagecolorallocate(
        $imtn,
        $transparentcolor['red'],
        $transparentcolor['green'],
        $transparentcolor['blue']
    );
    imagefill( $imtn, 0, 0, $newtransparentcolor );
    imagecolortransparent( $imtn, $newtransparentcolor );
}
	}
elseif($type === 2)									#PNG
	{
	$src.= ".png";
$im = imagecreatefrompng( $src );
$ow = imagesx( $im );
$oh = imagesy( $im );
if( $ow > $mw OR $oh > $mh ) {					#Wenn Bild vergrößert werden muss
    if( $ow > $oh ) {							#Wenn Breite größer wie Höhe
        $tnw = $mw;
        $tnh = $tnw * $oh / $ow;
    } elseif( $oh > $ow ) {
        $tnw = $mw;
        $tnh = $tnw * $oh / $ow;
    } else {
        $tnh = $mh;
        $tnw = $tnh * $ow / $oh;
    }
} else {
    $tnw = $ow;
    $tnh = $oh;

if( $ow < $mw OR $oh < $mh) {
	$tnw = $mw;
	$tnh = $mw;
}
}
$imtn = imagecreatetruecolor( $tnw, $tnh );
$originaltransparentcolor = imagecolortransparent( $im );
if(
    $originaltransparentcolor >= 0
    && $originaltransparentcolor < imagecolorstotal( $im )
) {
    $transparentcolor = imagecolorsforindex( $im, $originaltransparentcolor );
    $newtransparentcolor = imagecolorallocate(
        $imtn,
        $transparentcolor['red'],
        $transparentcolor['green'],
        $transparentcolor['blue']
    );
    imagefill( $imtn, 0, 0, $newtransparentcolor );
    imagecolortransparent( $imtn, $newtransparentcolor );
}
	}
elseif($type === 3)									#GIF
	{
	$src.= ".gif";
	$im = imagecreatefromgif( $src );
$ow = imagesx( $im );
$oh = imagesy( $im );
if( $ow > $mw OR $oh > $mh ) {					#Wenn Bild vergrößert werden muss
    if( $ow > $oh ) {							#Wenn Breite größer wie Höhe
        $tnw = $mw;
        $tnh = $tnw * $oh / $ow;
    } elseif( $oh > $ow ) {
        $tnw = $mw;
        $tnh = $tnw * $oh / $ow;
    } else {
        $tnh = $mh;
        $tnw = $tnh * $ow / $oh;
    }
} else {
    $tnw = $ow;
    $tnh = $oh;

if( $ow < $mw OR $oh < $mh) {
	$tnw = $mw;
	$tnh = $mw;
}
}
$imtn = imagecreatetruecolor( $tnw, $tnh );
$originaltransparentcolor = imagecolortransparent( $im );
if(
    $originaltransparentcolor >= 0
    && $originaltransparentcolor < imagecolorstotal( $im )
) {
    $transparentcolor = imagecolorsforindex( $im, $originaltransparentcolor );
    $newtransparentcolor = imagecolorallocate(
        $imtn,
        $transparentcolor['red'],
        $transparentcolor['green'],
        $transparentcolor['blue']
    );
    imagefill( $imtn, 0, 0, $newtransparentcolor );
    imagecolortransparent( $imtn, $newtransparentcolor );
}
}
imagecopyresized( $imtn, $im, 0, 0, 0, 0, $tnw, $tnh, $ow, $oh );
imagejpeg($imtn,$des,$qual);
imagedestroy( $im );
imagedestroy( $imtn );
	}
 
GIF, JP(E)G, PNG. Aber BMP's nicht.
Würde auch stark abraten, ein BMP Bild mit 1024x768 Pixel ist (viel) zu groß. 2,2 MB.

Da braucht der Skripts relative lange, und Upload sowieso.
 
Ok.

Aber bei gif, png fehlt die Transparenz (wird zu schwarz) und bei gif die Animation.
 
Zuletzt bearbeitet:
Selbstverständlich gehen Animationen und Tranzparenz verloren wenn das Ergebnis, wie bei meinem Skript z.B, immer ein JPG auswirft.

Du musst es halt anpassen.
 
Zurück
Oben