Eventviva\ImageResize::resizeToBestFit PHP Méthode

resizeToBestFit() public méthode

Resizes image to best fit inside the given dimensions
public resizeToBestFit ( integer $max_width, integer $max_height, boolean $allow_enlarge = false ) : static
$max_width integer
$max_height integer
$allow_enlarge boolean
Résultat static
    public function resizeToBestFit($max_width, $max_height, $allow_enlarge = false)
    {
        if ($this->getSourceWidth() <= $max_width && $this->getSourceHeight() <= $max_height && $allow_enlarge === false) {
            return $this;
        }
        $ratio = $this->getSourceHeight() / $this->getSourceWidth();
        $width = $max_width;
        $height = $width * $ratio;
        if ($height > $max_height) {
            $height = $max_height;
            $width = $height / $ratio;
        }
        return $this->resize($width, $height, $allow_enlarge);
    }

Usage Example

Exemple #1
0
 protected function makeThumb($file, MediaProvider $media)
 {
     $name = 'thumb-' . $media->file;
     $new = $media->path . '/' . $name;
     $image = new ImageResize($file);
     $image->resizeToBestFit(150, 150);
     $image->save(storage_path() . $new);
     return $name;
 }
All Usage Examples Of Eventviva\ImageResize::resizeToBestFit