-
-
Save igor822/4415fb0f152720acb526 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Thumb | |
{ | |
private $proportional; | |
public function make(UploadedFile $file, $path , $name, $maxWidth, $maxHeight) | |
{ | |
if (!File::isDirectory(public_path() . $path)) { | |
File::makeDirectory(public_path() . $path, $mode = 0777, true, true); | |
} | |
$image = Image::make($file->getRealpath()); | |
$extension = $file->getClientOriginalExtension(); | |
$proportional = $this->getProportional($image, $maxWidth, $maxHeight); | |
$newWidth = $proportional['width']; | |
$newHeight = $proportional['height']; | |
return $image->resize($newWidth, $newHeight)->save(public_path() . $path . '/' . $name . '.' . $extension); | |
} | |
public function getProportional(Image $image, $maxWidth, $maxHeight = 0) | |
{ | |
$size = []; | |
$ratio = $image->width() * 1 / $image->height(); | |
$widthOversized = ($image->width() > $maxWidth); | |
$heightOversized = ( $image->height() > $maxHeight); | |
if ($widthOversized || $heightOversized) { | |
$size['width'] = min($maxWidth, $ratio * $maxHeight); | |
$size['height'] = min($maxHeight, $maxWidth / $ratio); | |
} | |
return $size; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment