Skip to content

Instantly share code, notes, and snippets.

@thomaspiedade
Last active August 29, 2015 14:18
Show Gist options
  • Save thomaspiedade/8feeb3c0eb8732d262a4 to your computer and use it in GitHub Desktop.
Save thomaspiedade/8feeb3c0eb8732d262a4 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->proportional($image, $maxWidth, $maxHeight);
$newWidth = $proportional->width();
$newHeight = $proportional->height();
return $image->resize($newWidth, $newHeight)->save(public_path() . $path . '/' . $name . '.' . $extension);
}
public function proportional( Image $image, $maxX, $maxY)
{
$this->proportional['width'] = $image->width();
$this->proportional['height'] = $image->height();
$ratio = $this->proportional['width'] * 1.0 / $this->proportional['height'];
$xOversized = ($this->proportional['width'] > $maxX);
$yOversized = ($this->proportional['height'] > $maxY);
if ($xOversized || $yOversized) {
$this->proportional['width'] = min($maxX, $ratio * $maxY);
$this->proportional['height'] = min($maxY, $maxX / $ratio);
}
return $this;
}
public function height()
{
return $this->proportional['height'];
}
public function width()
{
return $this->proportional['width'];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment