| 1 |
<?php |
| 2 |
|
| 3 |
namespace Intervention\Image\Gd; |
| 4 |
|
| 5 |
use Intervention\Image\Exception\NotSupportedException; |
| 6 |
use Intervention\Image\Image; |
| 7 |
|
| 8 |
class Driver extends \Intervention\Image\AbstractDriver |
| 9 |
{ |
| 10 |
/** |
| 11 |
* Creates new instance of driver |
| 12 |
* |
| 13 |
* @param Decoder $decoder |
| 14 |
* @param Encoder $encoder |
| 15 |
*/ |
| 16 |
public function __construct(Decoder $decoder = null, Encoder $encoder = null) |
| 17 |
{ |
| 18 |
if ( ! $this->coreAvailable()) { |
| 19 |
throw new NotSupportedException( |
| 20 |
"GD Library extension not available with this PHP installation." |
| 21 |
); |
| 22 |
} |
| 23 |
|
| 24 |
$this->decoder = $decoder ? $decoder : new Decoder; |
| 25 |
$this->encoder = $encoder ? $encoder : new Encoder; |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Creates new image instance |
| 30 |
* |
| 31 |
* @param int $width |
| 32 |
* @param int $height |
| 33 |
* @param mixed $background |
| 34 |
* @return \Intervention\Image\Image |
| 35 |
*/ |
| 36 |
public function newImage($width, $height, $background = null) |
| 37 |
{ |
| 38 |
// create empty resource |
| 39 |
$core = imagecreatetruecolor($width, $height); |
| 40 |
$image = new Image(new static, $core); |
| 41 |
|
| 42 |
// set background color |
| 43 |
$background = new Color($background); |
| 44 |
imagefill($image->getCore(), 0, 0, $background->getInt()); |
| 45 |
|
| 46 |
return $image; |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Reads given string into color object |
| 51 |
* |
| 52 |
* @param string $value |
| 53 |
* @return AbstractColor |
| 54 |
*/ |
| 55 |
public function parseColor($value) |
| 56 |
{ |
| 57 |
return new Color($value); |
| 58 |
} |
| 59 |
|
| 60 |
/** |
| 61 |
* Checks if core module installation is available |
| 62 |
* |
| 63 |
* @return boolean |
| 64 |
*/ |
| 65 |
protected function coreAvailable() |
| 66 |
{ |
| 67 |
return (extension_loaded('gd') && function_exists('gd_info')); |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* Returns clone of given core |
| 72 |
* |
| 73 |
* @return mixed |
| 74 |
*/ |
| 75 |
public function cloneCore($core) |
| 76 |
{ |
| 77 |
$width = imagesx($core); |
| 78 |
$height = imagesy($core); |
| 79 |
$clone = imagecreatetruecolor($width, $height); |
| 80 |
imagealphablending($clone, false); |
| 81 |
imagesavealpha($clone, true); |
| 82 |
$transparency = imagecolorallocatealpha($clone, 0, 0, 0, 127); |
| 83 |
imagefill($clone, 0, 0, $transparency); |
| 84 |
|
| 85 |
imagecopy($clone, $core, 0, 0, 0, 0, $width, $height); |
| 86 |
|
| 87 |
return $clone; |
| 88 |
} |
| 89 |
} |
| 90 |
|