Created
April 11, 2013 22:53
-
-
Save hjpbarcelos/5367856 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace GdWrapper\Image\Resource; | |
use GdWrapper\Image\Resource\Exceptions\InvalidRawResourceException; | |
use GdWrapper\Image\Resource\Exceptions\InvalidFilePathException; | |
abstract class AbstractResource { | |
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 $rawResource; | |
public function __construct($resource) | |
{ | |
if (is_string($resource)) { | |
$this->createResource($resource); | |
} else { | |
$this->setRawResource($resource); | |
} | |
} | |
final public function __destruct() { | |
if (is_resource($this->rawResource)) { | |
imagedestroy($this->rawResource); | |
} | |
} | |
final public function __clone() | |
{ | |
$this->rawResource = $this->cloneResource(); | |
} | |
private function cloneResource() { | |
ob_start(); | |
imagegd2($this->rawResource); | |
return imagecreatefromstring(ob_get_clean()); | |
} | |
public function setRawResource($resource) | |
{ | |
if (!is_resource($resource)) { | |
throw new InvalidRawResourceException( | |
'Invalid resource passed to ' . get_class($this) | |
); | |
} | |
$this->rawResource = $resource; | |
} | |
public function raw($copy = false) | |
{ | |
if($copy) { | |
return $this->cloneResource(); | |
} | |
return $this->rawResource; | |
} | |
private function createResource($filepath) { | |
if (! (new \SplFileInfo($filepath))->isFile()) { | |
throw new InvalidFilePathException( | |
"Invalid filepath location: ${filepath}" | |
); | |
} | |
$this->setRawResource($this->doCreateResource($filepath)); | |
} | |
abstract protected function doCreateResource($filepath); | |
abstract public function output( | |
$filename = null, | |
$quality = self::IMAGE_QUALITY_MAX, | |
$additionalParameters = null | |
); | |
public static function factory($path) { | |
$type = pathinfo($path, PATHINFO_EXTENSION); | |
$className = __NAMESPACE__ . '\\' . ucfirst(strtolower($type)) . 'Resource'; | |
try { | |
return (new \ReflectionClass($className))->newInstance($path); | |
} catch(\ReflectionException $e) { | |
throw new UnsupportedFileExtensionException("Extension ${path} not supported!"); | |
} | |
} | |
public static function copy(AbstractResource $orig, $newType = null) { | |
if ($newType === null) { | |
return clone $orig; | |
} else { | |
$className = __NAMESPACE__ . '\\' . ucfirst(strtolower($newType)) . 'Resource'; | |
try { | |
return (new \ReflectionClass($className))->newInstance($orig->raw(true)); | |
} catch(\ReflectionException $e) { | |
throw new UnsupportedFileExtensionException("Extension ${fileType} not supported!"); | |
} | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace GdWrapper\Image\Resource; | |
/** | |
* | |
**/ | |
class JpegResource extends AbstractResource | |
{ | |
final protected function doCreateResource($filepath) | |
{ | |
return imagecreatefromjpeg($filepath); | |
} | |
final public function output( | |
$filename = null, | |
$quality = self::IMAGE_QUALITY_MAX, | |
$additionalParameters = null | |
) { | |
if(strpos($filename, '.') === false) { | |
$filename = null; | |
} | |
$args = array_merge(array($this->raw()), func_get_args()); | |
return call_user_func_array('imagejpeg', $args); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace GdWrapper\Image\Resource; | |
/** | |
* | |
**/ | |
class PngResource extends AbstractResource | |
{ | |
final protected function doCreateResource($filepath) | |
{ | |
return imagecreatefrompng($filepath); | |
} | |
final public function output( | |
$filename = null, | |
$quality = self::IMAGE_QUALITY_MAX, | |
$additionalParameters = array() | |
) { | |
$funcArgs = func_get_args(); | |
if(count($funcArgs) > 2) { | |
$additionalParameters = array_slice($funcArgs, 2); | |
} | |
if(strpos($filename, '.') === false) { | |
$filename = null; | |
} | |
if($filename === null) { | |
$quality = 100; | |
} | |
if($quality !== null) { | |
$quality = floor((100 - $quality) / (100/9)); | |
} | |
$args = array_merge([$this->raw(), $filename, $quality], $additionalParameters); | |
return call_user_func_array('imagepng', $args); | |
} | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
require 'autoload.php'; | |
use GdWrapper\Image\Resource\AbstractResource; | |
use GdWrapper\Image\Resource\JpegResource; | |
use GdWrapper\Image\Resource\PngResource; | |
$ext = 'jpeg'; | |
$res = AbstractResource::factory("file.{$ext}"); | |
$png = AbstractResource::copy($res, 'png'); | |
$png->output('teste2.png'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment