| 1 |
<?php |
| 2 |
|
| 3 |
namespace Intervention\Image; |
| 4 |
|
| 5 |
use Intervention\Image\Exception\NotSupportedException; |
| 6 |
|
| 7 |
abstract class AbstractDriver |
| 8 |
{ |
| 9 |
/** |
| 10 |
* Decoder instance to init images from |
| 11 |
* |
| 12 |
* @var \Intervention\Image\AbstractDecoder |
| 13 |
*/ |
| 14 |
public $decoder; |
| 15 |
|
| 16 |
/** |
| 17 |
* Image encoder instance |
| 18 |
* |
| 19 |
* @var \Intervention\Image\AbstractEncoder |
| 20 |
*/ |
| 21 |
public $encoder; |
| 22 |
|
| 23 |
/** |
| 24 |
* Creates new image instance |
| 25 |
* |
| 26 |
* @param int $width |
| 27 |
* @param int $height |
| 28 |
* @param string $background |
| 29 |
* @return \Intervention\Image\Image |
| 30 |
*/ |
| 31 |
abstract public function newImage($width, $height, $background); |
| 32 |
|
| 33 |
/** |
| 34 |
* Reads given string into color object |
| 35 |
* |
| 36 |
* @param string $value |
| 37 |
* @return AbstractColor |
| 38 |
*/ |
| 39 |
abstract public function parseColor($value); |
| 40 |
|
| 41 |
/** |
| 42 |
* Checks if core module installation is available |
| 43 |
* |
| 44 |
* @return boolean |
| 45 |
*/ |
| 46 |
abstract protected function coreAvailable(); |
| 47 |
|
| 48 |
/** |
| 49 |
* Returns clone of given core |
| 50 |
* |
| 51 |
* @return mixed |
| 52 |
*/ |
| 53 |
public function cloneCore($core) |
| 54 |
{ |
| 55 |
return clone $core; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Initiates new image from given input |
| 60 |
* |
| 61 |
* @param mixed $data |
| 62 |
* @return \Intervention\Image\Image |
| 63 |
*/ |
| 64 |
public function init($data) |
| 65 |
{ |
| 66 |
return $this->decoder->init($data); |
| 67 |
} |
| 68 |
|
| 69 |
/** |
| 70 |
* Encodes given image |
| 71 |
* |
| 72 |
* @param Image $image |
| 73 |
* @param string $format |
| 74 |
* @param int $quality |
| 75 |
* @return \Intervention\Image\Image |
| 76 |
*/ |
| 77 |
public function encode($image, $format, $quality) |
| 78 |
{ |
| 79 |
return $this->encoder->process($image, $format, $quality); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* Executes named command on given image |
| 84 |
* |
| 85 |
* @param Image $image |
| 86 |
* @param string $name |
| 87 |
* @param array $arguments |
| 88 |
* @return \Intervention\Image\Commands\AbstractCommand |
| 89 |
*/ |
| 90 |
public function executeCommand($image, $name, $arguments) |
| 91 |
{ |
| 92 |
$commandName = $this->getCommandClassName($name); |
| 93 |
$command = new $commandName($arguments); |
| 94 |
$command->execute($image); |
| 95 |
|
| 96 |
return $command; |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Returns classname of given command name |
| 101 |
* |
| 102 |
* @param string $name |
| 103 |
* @return string |
| 104 |
*/ |
| 105 |
private function getCommandClassName($name) |
| 106 |
{ |
| 107 |
if (extension_loaded('mbstring')) { |
| 108 |
$name = mb_strtoupper(mb_substr($name, 0, 1)) . mb_substr($name, 1); |
| 109 |
} else { |
| 110 |
$name = strtoupper(substr($name, 0, 1)) . substr($name, 1); |
| 111 |
} |
| 112 |
|
| 113 |
$drivername = $this->getDriverName(); |
| 114 |
$classnameLocal = sprintf('\Intervention\Image\%s\Commands\%sCommand', $drivername, ucfirst($name)); |
| 115 |
$classnameGlobal = sprintf('\Intervention\Image\Commands\%sCommand', ucfirst($name)); |
| 116 |
|
| 117 |
if (class_exists($classnameLocal)) { |
| 118 |
return $classnameLocal; |
| 119 |
} elseif (class_exists($classnameGlobal)) { |
| 120 |
return $classnameGlobal; |
| 121 |
} |
| 122 |
|
| 123 |
throw new NotSupportedException( |
| 124 |
"Command ({$name}) is not available for driver ({$drivername})." |
| 125 |
); |
| 126 |
} |
| 127 |
|
| 128 |
/** |
| 129 |
* Returns name of current driver instance |
| 130 |
* |
| 131 |
* @return string |
| 132 |
*/ |
| 133 |
public function getDriverName() |
| 134 |
{ |
| 135 |
$reflect = new \ReflectionClass($this); |
| 136 |
$namespace = $reflect->getNamespaceName(); |
| 137 |
|
| 138 |
return substr(strrchr($namespace, "\\"), 1); |
| 139 |
} |
| 140 |
} |
| 141 |
|