Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?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.");
- }
- }
- }
Add Comment
Please, Sign In to add comment