Skip to content

Instantly share code, notes, and snippets.

@jamsesso
Created December 19, 2010 17:24
Show Gist options
  • Save jamsesso/747496 to your computer and use it in GitHub Desktop.
Save jamsesso/747496 to your computer and use it in GitHub Desktop.
<?php
#! Photo resize controller.
#! Class interface.
interface iResize
{
public function __construct($filename);
public function resize($x, $y);
public function save($locale);
}
#! Class.
class resize implements iResize
{
private $image, $type;
public function __construct($filename)
{
$img = getimagesize($filename);
$this->type = $img[2];
switch($this->type)
{
case IMAGETYPE_JPEG:
$this->image = imagecreatefromjpeg($filename);
break;
case IMAGETYPE_GIF:
$this->image = imagecreatefromgif($filename);
break;
case IMAGETYPE_PNG:
$this->image = imagecreatefrompng($filename);
break;
default:
throw new Exception("Invalid image type.");
}
}
private function axis()
{
return (object) array(
"width" => imagesx($this->image),
"height" => imagesy($this->image)
);
}
public function resize($x, $y)
{
$new = imagecreatetruecolor($x, $y);
imagecopyresampled($new, $this->image, 0, 0, 0, 0, $x, $y, $this->axis()->width, $this->axis()->height);
$this->image = $new;
}
public function save($locale)
{
switch($this->type)
{
case IMAGETYPE_JPEG:
imagejpeg($this->image, $locale, 75);
break;
case IMAGETYPE_GIF:
imagegif($this->image, $locale);
break;
case IMAGETYPE_PNG:
imagepng($this->image, $locale);
break;
default:
throw new Exception("Invalid image type.");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment