Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Code for "How to resize a logo to specific dimensions keeping its aspect ratio using PHP" tutorial published at http://burnmind.com/tutorials/resize-logo-keeping-aspect-ratio
{
"require": {
"intervention/image": "~2.1"
},
"autoload": {
"psr-0": {
"LogoResizer": "src/"
}
}
}
<?php
require_once('vendor/autoload.php');
use LogoResizer\LogoResizer;
function get_resized_logo($logo) {
$resizer = new LogoResizer(300, 200);
$resizer->set_source_logo($logo);
if(!$resizer->is_source_logo_valid()) {
return false;
}
$logo_resized = $resizer->resize_logo();
if(!$logo_resized) {
return false;
}
return $resizer->get_resized_logo();
}
if(isset($_FILES['logo'])) {
$resized_logo = get_resized_logo($_FILES['logo']);
if($resized_logo === false) {
//show error
} else {
echo $resized_logo->response('jpg', 70);
die();
}
}
?>
<form method="POST" action="" enctype="multipart/form-data">
<input type="file" name="logo" />
<input type="submit" value="Upload logo" />
</form>
<?php
namespace LogoResizer;
use Intervention\Image\ImageManagerStatic as Image;
class LogoResizer {
private $desired_width;
private $desired_height;
private $desired_aspect_ratio;
private $source_logo;
private $resized_logo;
public function __construct($desired_width = 300, $desired_height = 200) {
$this->desired_width = $desired_width;
$this->desired_height = $desired_height;
$this->desired_aspect_ratio = $this->get_aspect_ratio($desired_width, $desired_height);
}
public function set_source_logo($source_image) {
$this->source_logo = $source_image;
}
public function is_source_logo_valid() {
$details = getimagesize($this->source_logo['tmp_name']);
if($details === false) {
return false;
}
return true;
}
public function get_resized_logo() {
return $this->resized_logo;
}
public function resize_logo() {
try {
$logo = Image::make($this->source_logo['tmp_name']);
if($logo->width() === $this->desired_width && $logo->height() === $this->desired_height) {
$this->resized_logo = $logo;
return true;
}
$logo_aspect_ratio = $this->get_aspect_ratio($logo->width(), $logo->height());
if($this->desired_aspect_ratio === $logo_aspect_ratio) {
$logo = $this->resize($logo, $this->desired_width, $this->desired_height);
$this->resized_logo = $logo;
return true;
}
$background_colour = $logo->pickColor(0, 0, 'hex');
if($this->desired_aspect_ratio > $logo_aspect_ratio) {
$logo = $this->resize($logo, null, $this->desired_height);
} else {
$logo = $this->resize($logo, $this->desired_width, null);
}
$logo->resizeCanvas($this->desired_width, $this->desired_height, 'center', false, $background_colour);
$this->resized_logo = $logo;
return true;
} catch (Exception $e) {
return false;
}
}
private function get_aspect_ratio($width, $height) {
return $width / $height;
}
private function resize($logo, $width, $height) {
$logo->resize($width, $height, function ($constraint) {
$constraint->aspectRatio();
});
return $logo;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment