Webstatt.org - Community seit 2006 - 2012 (2024?)

Bilder verkleinern

user-307
02.05.2007 13:37

Hi!

Ich hab hier eine Funktion, die Bilder verkleinert.

Aufgabe des Scripts:
So lange das Bild beim verkleinern proportional bleibt, wird es ganz normal auf die blibige Größe geschrupft. Wenn aber die proportion nicht eingehalten wird, wird ein Teil aus dem Bild geschnitten, der die gewünscht Größe hat.

Das ganz Script ist nicht wirklich elegant geschrieben und hat auch noch so seine Macken.

Größtes problem: In manchen (bzw. vielen) Fällen haben die Bilder einen 1px großen Balken rechts und/oder unten!

function shrinkImage($image, $_newWidth = 0, $_newHeight = 0, $quality = 75) {
$size = getimagesize($image);
$width = $size[0];
$height = $size[1];
$newWidth = ($_newWidth == 0) ? intval($width*$_newHeight/$height) : $_newWidth;
$newHeight = ($_newHeight == 0) ? intval($height*$_newWidth/$width) : $_newHeight;
$startTop = 0;
$startLeft = 0;

if($_newWidth != 0 && $_newHeight != 0) {
$tmp_w = intval(($newWidth/$width)*100);
$tmp_h = intval(($newHeight/$height)*100);
/* Wenn das Bild nicht mehr proportional sein wird */
if($tmp_w != $tmp_h) { //Abweichungen von 1-2 Pixel zulassen?!
/* Bildgröße minus 5-40% und daraus einen Ausschnitt in gewünschter Größe */
$multiplier1 = $height/$newHeight;
$width_cut1 = round($newWidth*$multiplier1);
$height_cut1 = $height;
$multiplier2 = $width/$newWidth;
$width_cut2 = $width;
$height_cut2 = round($newHeight*$multiplier2);
if($width_cut1 <= $width && $height_cut1 <= $height) {
$width_cut = $width_cut1;
$height_cut = $height_cut1;
} else {
$width_cut = $width_cut2;
$height_cut = $height_cut2;
}
$percent = rand(5, 40);
$width_cut -= round(($width_cut/100)*$percent);
$height_cut -= round(($height_cut/100)*$percent);
$maxTop = $width-$width_cut;
$maxLeft = $height-$height_cut;
$startTop = rand(0, $maxTop);
$startLeft = rand(0, $maxLeft);
$width = $width_cut;
$height = $height_cut;
}
}

if($size[2] == 1) { //.gif
$oldPicture = ImageCreateFromGIF($image);
$newPicture = ImageCreate($newWidth, $newHeight);
ImageCopyResized($newPicture, $oldPicture, 0, 0, $startTop, $startLeft, $newWidth, $newHeight, $width, $height);
ImageGIF($newPicture, $image);
} else if($size[2] == 2) { //.jpg
$oldPicture = ImageCreateFromJPEG($image);
$newPicture = ImageCreateTrueColor($newWidth, $newHeight);
ImageCopyResized($newPicture, $oldPicture, 0, 0, $startTop, $startLeft, $newWidth, $newHeight, $width, $height);
ImageJPEG($newPicture, $image, $quality);
} else if($size[2] == 3) { //.png
$oldPicture = ImageCreateFromPNG($image);
$newPicture = ImageCreateTrueColor($newWidth, $newHeight);
ImageCopyResized($newPicture, $oldPicture, 0, 0, $startTop, $startLeft, $newWidth, $newHeight, $width, $height);
ImagePNG($newPicture, $image);
}
}

Ich benutze zum Testen Bilder verschiedenster größen und schrumpfe sie einmal auf 530px Breite (shrinkImage('bild.jpg', 530)) und auf 170x145 (shrinkImage('bild.jpg', 170, 145)).

Vielleicht hat ja der Eine oder Andere noch ein paar Vorschläge....??

.., Spark