Bildgröße berechnen

Eine ältere Aufgabe, die ich in BRG_HP hatte. Aufgabenstellung:

In der $data['avatar'] befindet sich evtl. (!) der Link zu dem Avatar eines Users. Ich möchte nun die Breite des Bildes auf maximal 300px stellen. Beachte: Die Breite ist vorgegeben. Die Höhe muss berechnet werden! Voraussetzungen: GD-Bibliothek, PHP5.x, kein file_exists()

$picture = getimagesize($data['avatar']);
$path = $position.'../images/avatars/';

$width1 = $picture[0];
$width2 = 300;
$height1 = $picture[1];

if ($data['avatar'] != "") {
if ($width1 > 300) {
$width = 300;
$height (bzw. $height2) = $height1 / ($width1 / $width2)
} else {
$width = $width1;
$height = $height1;
}
echo '<img src="'.$path.$data['avatar'].'" alt="Avatar" width="'.$width.'" height="'.$height.'" />';
} else {
echo " ";
}

Und das Resultat, das ich daraus erkannt, sind die Formeln, die die Berechnung einer Höhe/Breite durchführen. :-)

height1 = (width1 / width2 ) * height2
width1 = (height1 / height2) * width2
width2 = width1 / (height1 / height2)
height2 = height1 / (width1 / width2)

1 steht für die “alte” Breite/Höhe; 2 für die “neue” Breite/Höhe

Leave a Comment


NOTE - You can use these HTML tags and attributes:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>