Skip to content

Instantly share code, notes, and snippets.

@hjpbarcelos
Last active December 15, 2015 21:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hjpbarcelos/5328596 to your computer and use it in GitHub Desktop.
Save hjpbarcelos/5328596 to your computer and use it in GitHub Desktop.
Brincando...
<?php
namespace GdWrapper;
interface Image {
public function getPath();
public function getWidth();
public function getHeight();
public function getType();
}
class ImageInfo {
private $width;
private $height;
public function __consctuct($path) {
$dimensions = getimagesize($path);
$this->width = $dimensions[0];
$this->height = $dimensions[1];
}
public function getWidth() {
return $this->width;
}
public function getHeight() {
return $this->height;
}
}
class AbstractImage extends SplFileInfo implements Image {
private $width:
private $height;
private $info;
private $resource;
public function __clone() {
$this->resource = clone $this->resource;
}
private function getInfo() {
if($this->info === null) {
$this->info = new ImageInfo($this->getPath());
}
return $this->info;
}
public function getWidth() {
return $this->getInfo()->getWidth();
}
public function getHeight() {
return $this->getInfo()->getHeight();
}
protected function setResource(ImageResource $resource) {
$this->resource = $resource;
}
public function getResource() {
return $this->resource;
}
public static function factory($path) {
$type = pathinfo($path, FILEINFO_PATH);
$className = __NAMESPACE__ . "\\Image" . ucfirst(strtolower($type));
try {
$reflection = new \ReflectionClass($className);
return $reflection->newInstance($path);
} catch(\ReflectionException $e) {
throw new UnsupportedFileExtensionException("Extension ${path} not supported!");
}
}
}
abstract class ImageResource {
const IMAGE_QUALITY_MAX = 100;
const IMAGE_QUALITY_HIGH = 90;
const IMAGE_QUALITY_MED = 75;
const IMAGE_QUALITY_LOW = 50;
const IMAGE_QUALITY_DRAFT = 30;
private $resource;
final protected function setRaw($resource) {
$this->resource = $resource;
}
final public function __clone() {
ob_start();
imagegd2($this->resource);
$this->resource = imagecreatefromstring(ob_get_clean());
}
public function raw() {
return $resource;
}
public function output($filename = null, $quality = self::IMAGE_QUALITY_MAX,
$addtionalParameters = null) {
$args = func_get_args();
call_user_func_array($thi->getOutputFn(), $args);
}
abstract protected function getOutputFn();
final public __destruct() {
if($this->resource !== null)
imagedestroy($this->resource);
}
}
class ImageJpegResource extends ImageResource {
public function __construct($path) {
$this->setRaw(imagecreatefromjpeg($path));
}
protected function getOutputFn() {
return 'imagejpeg';
}
}
class ImagePngResource extends ImageResource {
public function __construct($path) {
$this->setRaw(imagecreatefrompng($path));
}
protected function getOutputFn() {
return 'imagepng';
}
}
class ImageGifResource extends ImageResource {
public function __construct($path) {
$this->setRaw(imagecreatefromgif($path));
}
protected function getOutputFn() {
return 'imagegif';
}
}
class ImageJpeg extends AbstractImage {
public function __construct($path) {
parent::__construct($path);
$this->setResource(new ImageJpegResource($path));
}
}
class ImageJpg extends ImageJpeg {
public function getType() {
return 'jpeg';
}
}
class ImagePng extends AbstractImage {
public function __construct($path) {
parent::__construct($path);
$this->setResource(new ImagePngResource($path));
}
}
class ImageGif extends AbstractImage {
public function __construct($path) {
parent::__construct($path);
$this->setResource(new ImageGifResource($path));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment