Beispiel #1
0
 /**
  * "Умное" создание миниатюр.
  *
  * @param int ширина
  * @param int высота
  * @param float коэффициент превышения.
  * @throws IllegalArgumentException
  * @return AcImage
  */
 public function thumbnail($width, $height, $c = 2)
 {
     if ($c <= 1 || !is_finite($c) || $width <= 0 || $height <= 0) {
         throw new IllegalArgumentException();
     }
     $size = new Size($width, $height);
     if ($this->getSize()->lessThen($size)) {
         $size = $this->getSize();
     }
     $imageSize = $this->getSize();
     $isRotate = false;
     if ($size->getWidth() / $imageSize->getWidth() <= $size->getHeight() / $imageSize->getHeight()) {
         $size->flip();
         $imageSize->flip();
         $isRotate = true;
     }
     $width = $size->getWidth();
     $height = $size->getHeight();
     $imageWidth = $imageSize->getWidth();
     $imageHeight = $imageSize->getHeight();
     $lim = (int) ($c * $height);
     $newHeight = (int) ($imageHeight * $width / $imageWidth);
     if ($imageWidth > $width) {
         if ($newHeight <= $lim) {
             $size = new Size($width, $newHeight);
         } else {
             if ($newHeight <= 2 * $lim) {
                 $size = new Size((int) ($imageWidth * $lim / $imageHeight), $lim);
             } else {
                 $size = new Size((int) ($width / 2), (int) ($imageHeight * $width / $imageWidth));
             }
         }
     } else {
         if ($imageHeight <= $lim) {
             $size = $this->getSize();
         } else {
             if ($imageHeight <= 2 * $lim) {
                 if ($imageWidth * $lim / $imageHeight >= $width / 2) {
                     $size = new Size((int) ($imageWidth * $lim / $imageHeight), $lim);
                 } else {
                     $size = new Size((int) ($width / 2), (int) ($imageHeight * $width / ($imageWidth * 2)));
                 }
             } else {
                 $size = new Size((int) ($width / 2), (int) ($imageHeight * $width / ($imageWidth * 2)));
             }
         }
     }
     if ($isRotate) {
         $size->flip();
         $imageSize->flip();
     }
     return $this->resize($size);
 }