NextGEN Gallery – Bilder verkleinern im Seitenverhältnis

Dieser Beitrag ist auch verfügbar in: Englisch

Soooo… nach langem verschieben habe ich mich heute mal beigemacht und das Problem mit dem Seitenverhältnis gelöst.

Das Problem ist folgendes… Gebe ich für meine Bilder einer Größe von 800 px x 600 px (Breite x Höhe) vor, so verkleinert die Software z.B. ein Bild von 1024px x 765px auf dieses Format. Ist mein Bild allerdings 765px x 1024px so wird es nicht auf 600px x 800px verkleinert sondern auf 450px x 600px. Und das ist nicht im Sinne des Erfinders. :)

Also habe ich mich heute mal hingesetzt und bin dem auf den Grund gegangen. Es ist eigentlich simpel. Das Bild wird hochgeladen und auf seine Größe kontrolliert. Die werte werden nach der Vorgabe neu berechnet und es wird aufgepasst das aus Querformat nicht plötzlich ein Hochformat wird und umgekehrt. Vergessen wurde aber leider auch Vorgabewerte anzupassen. Das tun wir nun…

Öffnen Sie die Datei “/wp-content/plugins/nextgen-gallery/admin/wp25/functions.php” in einem Editor und suchen Sie nach der Function “function resizeImages()”. Hier finden Sie folgenes:

function resizeImages($gallery_absfolder, $pictures) {
// ** $gallery_absfolder must contain abspath !!

if(! class_exists(‘ngg_Thumbnail’))
require_once(NGGALLERY_ABSPATH.’/lib/thumbnail.inc.php’);

$ngg_options = get_option(‘ngg_options’);

if (is_array($pictures)) {

$bar = new wpProgressBar(__(‘Running… Please wait’,’nggallery’));
$bar->setHeader(__(‘Resize images’,’nggallery’));
//total number of elements to process
$elements = count($pictures);
// wait a little bit after finished
if ($elements > 5) $bar->setSleepOnFinish(2);
//print the empty bar
$bar->initialize($elements);

foreach($pictures as $picture) {

if (!is_writable($gallery_absfolder.”/”.$picture)) {
$messagetext .= $gallery_absfolder.”/”.$picture.”<br />”;
$bar->increase();
continue;
}

$thumb = new ngg_Thumbnail($gallery_absfolder.”/”.$picture, TRUE);
// echo $thumb->errmsg;
// skip if file is not there
if (!$thumb->error) {
$thumb->resize($ngg_options[‘imgWidth’],$ngg_options[‘imgHeight’], $ngg_options[‘imgResampleMode’]);
if ( $thumb->save($gallery_absfolder.”/”.$picture,$ngg_options[‘imgQuality’]) ) {
// do not flush the buffer with useless messages
if ($elements < 100)
$bar->addNote($picture. __(‘ : Image resized…’,’nggallery’));
} else
$bar->addNote($picture . ” : Error : <strong>”.$thumb->errmsg.”</strong>”);
$bar->increase();
}
$thumb->destruct();
}
}

if(!empty($messagetext)) nggallery::show_error(‘<strong>’.__(‘Some pictures are not writeable :’,’nggallery’).'</strong><br /><ul>’.$messagetext.'</ul>’);
return;
}

Das ist die Function die unseren “normalen” Bildern eine neue Größe gibt. Wir ersetzen diese Funktion nun durch diese neu modifizierte:

function resizeImages($gallery_absfolder, $pictures) {
// ** $gallery_absfolder must contain abspath !!

if(! class_exists(‘ngg_Thumbnail’))
require_once(NGGALLERY_ABSPATH.’/lib/thumbnail.inc.php’);

$ngg_options = get_option(‘ngg_options’);

if (is_array($pictures)) {

$bar = new wpProgressBar(__(‘Running… Please wait’,’nggallery’));
$bar->setHeader(__(‘Resize images’,’nggallery’));
//total number of elements to process
$elements = count($pictures);
// wait a little bit after finished
if ($elements > 5) $bar->setSleepOnFinish(2);
//print the empty bar
$bar->initialize($elements);

foreach($pictures as $picture) {

if (!is_writable($gallery_absfolder.”/”.$picture)) {
$messagetext .= $gallery_absfolder.”/”.$picture.”<br />”;
$bar->increase();
continue;
}

$thumb = new ngg_Thumbnail($gallery_absfolder.”/”.$picture, TRUE);
// echo $thumb->errmsg;
// skip if file is not there

if (!$thumb->error) {
//$thumb->resize($ngg_options[‘imgWidth’], $ngg_options[‘imgHeight’], $ngg_options[‘imgResampleMode’]);

if ($thumb->currentDimensions[‘height’] > $ngg_options[‘imgHeight’] || $thumb->currentDimensions[‘width’] > $ngg_options[‘imgWidth’]) {

// check for portrait format
if ($thumb->currentDimensions[‘height’] > $thumb->currentDimensions[‘width’]) {
//vorgaben checken
if ($ngg_options[‘imgWidth’] > $ngg_options[‘imgHeight’]) {
$y = $ngg_options[‘imgWidth’];
$x = $ngg_options[‘imgHeight’];
} else {
$y = $ngg_options[‘imgHeight’];
$x = $ngg_options[‘imgWidth’];
}
$thumb->resize($x, 0, $ngg_options[‘imgResampleMode’]);
// get optimal y startpos
$ypos = ($thumb->currentDimensions[‘height’] – $y) / 2;
$thumb->crop(0, $ypos, $x,$y,$ngg_options[‘imgResampleMode’]);
} else {
//vorgaben checken
if ($ngg_options[‘imgWidth’] < $ngg_options[‘imgHeight’]) {
$y = $ngg_options[‘imgWidth’];
$x = $ngg_options[‘imgHeight’];
} else {
$y = $ngg_options[‘imgHeight’];
$x = $ngg_options[‘imgWidth’];
}
$thumb->resize(0,$y,$ngg_options[‘imgResampleMode’]);
// get optimal x startpos
$xpos = ($thumb->currentDimensions[‘width’] – $x) / 2;
$thumb->crop($xpos, 0, $x,$y,$ngg_options[‘imgResampleMode’]);
}

} else {
$thumb->resize($ngg_options[‘imgWidth’], $ngg_options[‘imgHeight’], $ngg_options[‘imgResampleMode’]);
}

if ( $thumb->save($gallery_absfolder.”/”.$picture,$ngg_options[‘imgQuality’]) ) {
// do not flush the buffer with useless messages
if ($elements < 100)
$bar->addNote($picture. __(‘ : Image resized…’,’nggallery’));
} else
$bar->addNote($picture . ” : Error : <strong>”.$thumb->errmsg.”</strong>”);
$bar->increase();
}
$thumb->destruct();
}
}

if(!empty($messagetext)) nggallery::show_error(‘<strong>’.__(‘Some pictures are not writeable :’,’nggallery’).'</strong><br /><ul>’.$messagetext.'</ul>’);
return;
}

Was machen wir da nun im Einzelnen?

if ($thumb->currentDimensions[‘height’] > $ngg_options[‘imgHeight’] || $thumb->currentDimensions[‘width’] > $ngg_options[‘imgWidth’]) {

Hier überprüfen wir ob das Hochgeladene Bild überhaupt größer ist als die Vorgabe (hier 800px x 600px). Wenn nicht muss auch nichts verkleinert werden.

if ($thumb->currentDimensions[‘height’] > $thumb->currentDimensions[‘width’])

Hier überprüfen wir in welchem Seitenformat das Bild vorliegt, Quer- oder Hochformat.

if ($ngg_options[‘imgWidth’] > $ngg_options[‘imgHeight’]) {
$y = $ngg_options[‘imgWidth’];
$x = $ngg_options[‘imgHeight’];
} else {
$y = $ngg_options[‘imgHeight’];
$x = $ngg_options[‘imgWidth’];
}

Und das ist im Endeffekt das neue… Nachdem wir nun wissen ob wir es mit einem Quer- oder Hochformat zu tun haben, passen wir die Vorgabewerte dem Format an… 800px x 600px oder 600px x 800px (für mein Beispiel.

Und das war es auch schon. Sieht viel aus, ist aber mehr Erklärung als alles andere :)
Vielleicht ist das nicht das non-plus-ultra, aber es funktioniert. Und darauf kommt es an. Wer es besser machen kann… kann gern seinen Kommentar loswerden ;)

Bitte beachten Sie. Alle Angaben auf dieser Seite sind ohne Gewähr und nur auf eigene Gefahr zu verwenden!

Gruß
Gordon

1 Kommentar

Trackbacks & Pingbacks

  1. […] Etwas stört mich nun aber immernoch. Wenn ich als größe 640px x 480px angebe ist das ja das Querformat. Er erkennt allerdings nicht automatisch ein Hochformat. Also er erkennt es schon… nimmt aber weiter fleißig 480px als höhe an und rechnet die Breite um. Damit wird das Bild aber noch kleiner als gedacht. Schöner wäre es wenn er im Querformat dann einfach 480px x 640px benutzt. Vielleicht werde ich mich da nächste Woche mal ransetzen. Da habe ich dann ja wieder Zeit [UPDATE] Der Artikel ist fertig, das Problem gelöst: Zum Artikel […]

Hinterlasse einen Kommentar

An der Diskussion beteiligen?
Hinterlasse uns deinen Kommentar!

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert

Ich stimme der Datenschutzerklärung zu