/**
  * Erzeugt ein verkleinertes Abbild des Originalbildes.
  */
 private function generateResizedImage(CacheFile $cacheFile, $proportion, $thumbnailMode, $bgcolor = 0xff0000)
 {
     static $countGenerateImage;
     if (!isset($countGenerateImage)) {
         $countGenerateImage = 0;
     }
     $countGenerateImage++;
     if ($thumbnailMode) {
         $minWidth = 50;
         // Wert siehe mosimage.xml => thumb_width => min
     } else {
         $minWidth = 200;
         // Wert siehe mosimage.xml => full_width => min
     }
     $origw = $cacheFile->origWidth();
     $origh = $cacheFile->origHeight();
     $displayWidth = $cacheFile->displayWidth();
     $displayHeight = $cacheFile->displayHeight();
     $src_img =& $this->loadImageFromFile($cacheFile);
     // Behandlung kleiner Bilder
     // - Die original-Höhe ist kleiner als die Zielhöhe. Zielhöhe kann nie 0 sein.
     // - Es bleibt diese Höhe erhalten und wir schneiden ggf. etwas von der Breite weg.
     // - Bei Auto-Breite wird die Breite ggf. auf ein Seitenverhältnis von 2:1 begrenzt, damit
     // das Bild nicht übermäßig breit wird
     if ($origh < $displayHeight) {
         // Im Auto-Modus wird das Seitenverhältnis aus dem Original genmmen
         if ($displayWidth == 0) {
             $displayWidth = intval($origw / $origh * $displayHeight);
             if ($displayWidth > $displayHeight * 2) {
                 // Seitenverhältnis ggf. auf 2:1 reduzieren
                 $displayWidth = $displayHeight * 2;
             }
         }
         // Bild ist auch so schmal, das es reinpasst
         if ($origw < $displayWidth) {
             $offset_w = ($displayWidth - $origw) / 2;
             $offset_h = $this->calcPropertion($displayHeight, $origh, $proportion);
             $src_w = $origw;
             $src_h = $origh;
         } else {
             // ansonsten schneiden wir es rechts ab
             $offset_w = 0;
             $offset_h = $this->calcPropertion($displayHeight, $origh, $proportion);
             $src_w = $displayWidth;
             $src_h = $origh;
         }
         if ($displayWidth < $minWidth) {
             $this->debug($cacheFile, "[Fix auto-Breite] display: [{$displayWidth} x {$displayHeight}] offset: [{$offset_w} x {$offset_h}]");
             $offset_w = ($minWidth - $displayWidth) / 2;
             $displayWidth = $minWidth;
         }
         $cacheFile->setImageOffset($offset_w, $offset_h);
         $cacheFile->setDisplaySize($displayWidth, $displayHeight);
         $this->debug($cacheFile, "[kleine Höhe ] display: [{$displayWidth} x {$displayHeight}] offsetW: [{$offset_w}] offsetH: [{$offset_h}]");
         if ($countGenerateImage > $this->config->getMaxResizedImagePerRequest()) {
             return $this->generatePlaceHolder($cacheFile, $this->config->getScrambleFilename());
         }
         $dst_img = ImageCreateTrueColor($displayWidth, $displayHeight);
         imagefill($dst_img, 0, 0, $bgcolor);
         imagecopy($dst_img, $src_img, $offset_w, $offset_h, 0, 0, $src_w, $src_h);
         $this->watermarkCreator->writeWatermarkInfo($dst_img, $thumbnailMode, $cacheFile);
         $this->writeImageToFile($dst_img, $cacheFile);
         imagedestroy($src_img);
         imagedestroy($dst_img);
         return $cacheFile;
     }
     // Behandlung kleiner Bilder
     // - Die original-Breite ist kleiner als die ZielBreite, Zielbreite ist ungleich 0
     // - Es bleibt diese Breite erhalten und wir schneiden ggf. etwas von der Höhe weg.
     if ($origw < $displayWidth && $displayWidth != 0) {
         // Bild ist auch so hoch, das es reinpasst, das Bild wird dabei zentiert
         if ($origh < $displayHeight) {
             $offset_w = ($displayWidth - $origw) / 2;
             $offset_h = ($displayHeight - $origh) / 2;
             $src_w = $origw;
             $src_h = $origh;
         } else {
             // ansonsten schneiden wir es unten ab
             $offset_w = ($displayWidth - $origw) / 2;
             $offset_h = 0;
             $src_w = $origw;
             $src_h = $displayHeight;
         }
         $cacheFile->setImageOffset($offset_w, $offset_h);
         $this->debug($cacheFile, "[schmal] display: [{$displayWidth} x {$displayHeight}] offsetW: [{$offset_w}] offsetH: [{$offset_h}]");
         if ($countGenerateImage > $this->config->getMaxResizedImagePerRequest()) {
             return $this->generatePlaceHolder($cacheFile, $this->config->getScrambleFilename());
         }
         $dst_img = ImageCreateTrueColor($displayWidth, $displayHeight);
         imagefill($dst_img, 0, 0, $bgcolor);
         imagecopy($dst_img, $src_img, $offset_w, $offset_h, 0, 0, $src_w, $src_h);
         $this->writeImageToFile($dst_img, $cacheFile);
         imagedestroy($src_img);
         imagedestroy($dst_img);
         return $cacheFile;
     }
     // Breite automatisch berechnen, Seitenverhältnis beibehalten, wenn sinnvoll.
     if ($displayWidth == 0) {
         $displayWidth = intval($origw / $origh * $displayHeight);
         $offset_w = 0;
         $offset_h = 0;
         $w = $displayWidth;
         $h = $displayHeight;
         if ($displayWidth < $minWidth) {
             $this->debug($cacheFile, "[Fix auto-Breite] display: [{$displayWidth} x {$displayHeight}] offset: [{$offset_w} x {$offset_h}]");
             $offset_w = ($minWidth - $displayWidth) / 2;
             $displayWidth = $minWidth;
         }
         $cacheFile->setImageOffset($offset_w, $offset_h);
         $cacheFile->setDisplaySize($displayWidth, $displayHeight);
         $this->debug($cacheFile, "[auto-Breite] display: [{$displayWidth} x {$displayHeight}] offset: [{$offset_w} x {$offset_h}]");
         if ($countGenerateImage > $this->config->getMaxResizedImagePerRequest()) {
             return $this->generatePlaceHolder($cacheFile, $this->config->getScrambleFilename());
         }
         $dst_img = ImageCreateTrueColor($displayWidth, $displayHeight);
         imagefill($dst_img, 0, 0, $bgcolor);
         imagecopyresampled($dst_img, $src_img, $offset_w, $offset_h, 0, 0, $w, $h, $origw, $origh);
         $this->watermarkCreator->writeWatermarkInfo($dst_img, $thumbnailMode, $cacheFile);
         $this->writeImageToFile($dst_img, $cacheFile);
         imagedestroy($src_img);
         imagedestroy($dst_img);
         return $cacheFile;
     }
     // - Zielbreite ist bekannt und
     // - Original-Bilder ist größer als Zielbild
     if ($displayWidth != 0 && $displayHeight != 0) {
         if ($origw > $origh) {
             // landscape
             if ($displayWidth > $displayHeight) {
                 $w = $origw / $origh * $displayHeight;
                 $h = $displayHeight;
                 $offset_w = ($displayWidth - $w) / 2;
                 $offset_h = 0;
             } else {
                 $w = $displayWidth;
                 $h = $origh / $origw * $displayWidth;
                 $offset_w = 0;
                 $offset_h = $this->calcPropertion($displayHeight, $h, $proportion);
             }
         } else {
             // portrait
             if ($displayWidth >= $displayHeight) {
                 // Zielbild ist Landcape
                 $w = $origw / $origh * $displayHeight;
                 $h = $displayHeight;
                 $offset_w = ($displayWidth - $w) / 2;
                 $offset_h = 0;
             } else {
                 $w = $displayWidth;
                 $h = $origh / $origw * $displayWidth;
                 $offset_w = 0;
                 $offset_h = $this->calcPropertion($displayHeight, $h, $proportion);
             }
         }
         $cacheFile->setImageOffset($offset_w, $offset_h);
         $cacheFile->setDisplaySize($displayWidth, $displayHeight);
         $this->debug($cacheFile, "[normal] display: [{$displayWidth} x {$displayHeight}]  content [{$w} x {$h}] offset: [{$offset_w} x {$offset_h}]");
         if ($countGenerateImage > $this->config->getMaxResizedImagePerRequest()) {
             return $this->generatePlaceHolder($cacheFile, $this->config->getScrambleFilename());
         }
         $dst_img = ImageCreateTrueColor($displayWidth, $displayHeight);
         imagefill($dst_img, 0, 0, $bgcolor);
         imagecopyresampled($dst_img, $src_img, $offset_w, $offset_h, 0, 0, $w, $h, $origw, $origh);
         $this->watermarkCreator->writeWatermarkInfo($dst_img, $thumbnailMode, $cacheFile);
         $this->writeImageToFile($dst_img, $cacheFile);
         imagedestroy($src_img);
         imagedestroy($dst_img);
         return $cacheFile;
     }
 }