JBZoo\Image\Image::thumbnail PHP Метод

thumbnail() публичный Метод

This function attempts to get the image to as close to the provided dimensions as possible, and then crops the remaining overflow (from the center) to get the image to be the size specified. Useful for generating thumbnails.
public thumbnail ( integer $width, integer | null $height = null, boolean $topIsZero = false )
$width integer
$height integer | null If omitted - assumed equal to $width
$topIsZero boolean Force top offset = 0
    public function thumbnail($width, $height = null, $topIsZero = false)
    {
        $width = VarFilter::int($width);
        $height = VarFilter::int($height);
        // Determine height
        $height = $height ?: $width;
        // Determine aspect ratios
        $currentAspectRatio = $this->_height / $this->_width;
        $newAspectRatio = $height / $width;
        // Fit to height/width
        if ($newAspectRatio > $currentAspectRatio) {
            $this->fitToHeight($height);
        } else {
            $this->fitToWidth($width);
        }
        $left = floor($this->_width / 2 - $width / 2);
        $top = floor($this->_height / 2 - $height / 2);
        // Return trimmed image
        $right = $width + $left;
        $bottom = $height + $top;
        if ($topIsZero) {
            $bottom -= $top;
            $top = 0;
        }
        return $this->crop($left, $top, $right, $bottom);
    }

Usage Example

Пример #1
0
 public function testUnsupportedFormat()
 {
     $excepted = Helper::getExpected(__FUNCTION__ . '.png');
     $actual = Helper::getActual(__FUNCTION__ . '.tmp');
     $original = Helper::getOrig('1x1.tmp');
     if (copy($original, $actual)) {
         $img = new Image($actual);
         $info = $img->thumbnail(100, 200)->save()->getInfo();
         is('image/gif', $info['mime']);
         is(100, $info['width']);
         is(200, $info['height']);
         Helper::isFileEq($actual, $excepted);
     } else {
         fail('Can\'t copy original file!');
     }
 }