Skip to content

Instantly share code, notes, and snippets.

@igor822
Forked from thomaspiedade/Thumb.php
Last active August 29, 2015 14:18
Show Gist options
  • Save igor822/4415fb0f152720acb526 to your computer and use it in GitHub Desktop.
Save igor822/4415fb0f152720acb526 to your computer and use it in GitHub Desktop.
<?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