3 namespace PHPImageWorkshop
;
5 use PHPImageWorkshop\Core\ImageWorkshopLayer
as ImageWorkshopLayer
;
6 use PHPImageWorkshop\Core\ImageWorkshopLib
as ImageWorkshopLib
;
7 use PHPImageWorkshop\Exception\ImageWorkshopException
as ImageWorkshopException
;
9 // If no autoloader, uncomment these lines:
10 //require_once(__DIR__.'/Core/ImageWorkshopLayer.php');
11 //require_once(__DIR__.'/Exception/ImageWorkshopException.php');
16 * Use this class as a factory to initialize ImageWorkshop layers
19 * @link http://phpimageworkshop.com
20 * @author Sybio (Clément Guillemain / @Sybio01)
21 * @license http://en.wikipedia.org/wiki/MIT_License
22 * @copyright Clément Guillemain
29 const ERROR_NOT_AN_IMAGE_FILE
= 1;
34 const ERROR_IMAGE_NOT_FOUND
= 2;
39 const ERROR_NOT_WRITABLE_FILE
= 3;
44 const ERROR_CREATE_IMAGE_FROM_STRING
= 4;
47 * Initialize a layer from a given image path
49 * From an upload form, you can give the "tmp_name" path
53 * @return ImageWorkshopLayer
55 public static function initFromPath($path)
57 if (file_exists($path) && !is_dir($path)) {
59 if (!is_readable($path)) {
60 throw new ImageWorkshopException('Can\'t open the file at "'.$path.'" : file is not writable, did you check permissions (755 / 777) ?', static::ERROR_NOT_WRITABLE_FILE
);
63 $imageSizeInfos = @getImageSize
($path);
64 $mimeContentType = explode('/', $imageSizeInfos['mime']);
66 if (!$mimeContentType ||
!array_key_exists(1, $mimeContentType)) {
67 throw new ImageWorkshopException('Not an image file (jpeg/png/gif) at "'.$path.'"', static::ERROR_NOT_AN_IMAGE_FILE
);
70 $mimeContentType = $mimeContentType[1];
72 switch ($mimeContentType) {
74 $image = imageCreateFromJPEG($path);
78 $image = imageCreateFromGIF($path);
82 $image = imageCreateFromPNG($path);
86 throw new ImageWorkshopException('Not an image file (jpeg/png/gif) at "'.$path.'"', static::ERROR_NOT_AN_IMAGE_FILE
);
90 return new ImageWorkshopLayer($image);
93 throw new ImageWorkshopException('No such file found at "'.$path.'"', static::ERROR_IMAGE_NOT_FOUND
);
97 * Initialize a text layer
100 * @param string $fontPath
101 * @param integer $fontSize
102 * @param string $fontColor
103 * @param integer $textRotation
104 * @param integer $backgroundColor
106 * @return ImageWorkshopLayer
108 public static function initTextLayer($text, $fontPath, $fontSize = 13, $fontColor = 'ffffff', $textRotation = 0, $backgroundColor = null)
110 $textDimensions = ImageWorkshopLib
::getTextBoxDimension($fontSize, $textRotation, $fontPath, $text);
112 $layer = static::initVirginLayer($textDimensions['width'], $textDimensions['height'], $backgroundColor);
113 $layer->write($text, $fontPath, $fontSize, $fontColor, $textDimensions['left'], $textDimensions['top'], $textRotation);
119 * Initialize a new virgin layer
121 * @param integer $width
122 * @param integer $height
123 * @param string $backgroundColor
125 * @return ImageWorkshopLayer
127 public static function initVirginLayer($width = 100, $height = 100, $backgroundColor = null)
131 if (!$backgroundColor ||
$backgroundColor == 'transparent') {
133 $backgroundColor = 'ffffff';
136 return new ImageWorkshopLayer(ImageWorkshopLib
::generateImage($width, $height, $backgroundColor, $opacity));
140 * Initialize a layer from a resource image var
142 * @param \resource $image
144 * @return ImageWorkshopLayer
146 public static function initFromResourceVar($image)
148 return new ImageWorkshopLayer($image);
152 * Initialize a layer from a string (obtains with file_get_contents, cURL...)
154 * This not recommanded to initialize JPEG string with this method, GD displays bugs !
156 * @param string $imageString
158 * @return ImageWorkshopLayer
160 public static function initFromString($imageString)
162 if (!$image = @imageCreateFromString
($imageString)) {
163 throw new ImageWorkshopException('Can\'t generate an image from the given string.', static::ERROR_CREATE_IMAGE_FROM_STRING
);
166 return new ImageWorkshopLayer($image);