Initial commit
[2ch-be.git] / dev-test / new / Sybio-ImageWorkshop-14bbecd / src / PHPImageWorkshop / ImageWorkshop.php
blobf36c6e541d72c63b662b70cd03e7bcfbce9ded1c
1 <?php
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');
13 /**
14 * ImageWorkshop class
16 * Use this class as a factory to initialize ImageWorkshop layers
18 * @version 2.0.6
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
24 class ImageWorkshop
26 /**
27 * @var integer
29 const ERROR_NOT_AN_IMAGE_FILE = 1;
31 /**
32 * @var integer
34 const ERROR_IMAGE_NOT_FOUND = 2;
36 /**
37 * @var integer
39 const ERROR_NOT_WRITABLE_FILE = 3;
41 /**
42 * @var integer
44 const ERROR_CREATE_IMAGE_FROM_STRING = 4;
46 /**
47 * Initialize a layer from a given image path
49 * From an upload form, you can give the "tmp_name" path
51 * @param string $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) {
73 case 'jpeg':
74 $image = imageCreateFromJPEG($path);
75 break;
77 case 'gif':
78 $image = imageCreateFromGIF($path);
79 break;
81 case 'png':
82 $image = imageCreateFromPNG($path);
83 break;
85 default:
86 throw new ImageWorkshopException('Not an image file (jpeg/png/gif) at "'.$path.'"', static::ERROR_NOT_AN_IMAGE_FILE);
87 break;
90 return new ImageWorkshopLayer($image);
93 throw new ImageWorkshopException('No such file found at "'.$path.'"', static::ERROR_IMAGE_NOT_FOUND);
96 /**
97 * Initialize a text layer
99 * @param string $text
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);
115 return $layer;
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)
129 $opacity = 0;
131 if (!$backgroundColor || $backgroundColor == 'transparent') {
132 $opacity = 127;
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);