PageRenderTime 46ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/panada/Resources/Image.php

http://github.com/panada/Panada
PHP | 333 lines | 183 code | 65 blank | 85 comment | 55 complexity | 6d047a342334d2fd71541d8d6512959b MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Panada Image Modifier.
  4. *
  5. * @link http://panadaframework.com/
  6. *
  7. * @license http://www.opensource.org/licenses/bsd-license.php
  8. * @author Iskandar Soesman <k4ndar@yahoo.com>
  9. *
  10. * @since Version 0.1
  11. */
  12. namespace Resources;
  13. if (!defined('PIMG_RESIZE')) {
  14. define('PIMG_RESIZE', 'resize');
  15. }
  16. if (!defined('PIMG_CROP')) {
  17. define('PIMG_CROP', 'crop');
  18. }
  19. if (!defined('PIMG_RESIZE_CROP')) {
  20. define('PIMG_RESIZE_CROP', 'resize_crop');
  21. }
  22. class Image
  23. {
  24. /**
  25. * @var string Option for editng image the option is: resize | crop | resize_crop.
  26. */
  27. public $editType = PIMG_RESIZE;
  28. /**
  29. *@var bool Adjust width/heigh autmaticly for resizing proccess.
  30. */
  31. public $autoRatio = true;
  32. /**
  33. * @var int New image width.
  34. */
  35. public $resizeWidth = 0;
  36. /**
  37. * @var int New image height.
  38. */
  39. public $resizeHeight = 0;
  40. /**
  41. * @var int Crope width size.
  42. */
  43. public $cropWidth = 0;
  44. /**
  45. * @var int Crope height size.
  46. */
  47. public $cropHeight = 0;
  48. /**
  49. * @var string Source file name.
  50. */
  51. public $fileName = '';
  52. /**
  53. * @var string Source full path location.
  54. */
  55. public $filePath;
  56. /**
  57. * @var string Source file info.
  58. */
  59. public $fileInfo = array();
  60. /**
  61. * @var string Define new file name.
  62. */
  63. public $newFileName = '';
  64. /**
  65. * @var bool Remove any space in file name.
  66. */
  67. public $stripSpaces = true;
  68. /**
  69. * @var string Source folder location.
  70. */
  71. public $folder = '';
  72. /**
  73. * @var string Source image type: gif, jpg or png.
  74. */
  75. public $imageType;
  76. /**
  77. * @var int The value jpg compression. the range is 0 - 100.
  78. */
  79. public $jpegCompression = 90;
  80. /**
  81. * @var string Folder to placed new edited image.
  82. */
  83. public $saveTo = '';
  84. /**
  85. * @var array Initiate the error mesages.
  86. */
  87. public $errorMessages = array();
  88. /**
  89. * Class constructor.
  90. */
  91. public function __construct()
  92. {
  93. if (!function_exists('imagecopyresampled')) {
  94. throw new RunException('Image resizing function that required by Image Class is not available.');
  95. }
  96. }
  97. /**
  98. * Setter for option.
  99. *
  100. * @param string | array $var
  101. * @param mix $value
  102. */
  103. public function setOption($var, $value = false)
  104. {
  105. if (is_string($var)) {
  106. $this->$var = $value;
  107. }
  108. if (is_array($var)) {
  109. foreach ($var as $key => $value) {
  110. $this->$key = $value;
  111. }
  112. }
  113. return $this;
  114. }
  115. private function preErrorChecker()
  116. {
  117. /*
  118. * Check is folder destionation has set.
  119. */
  120. if (empty($this->folder)) {
  121. $this->errorMessages[] = 'No folder located. Please define the folder location.';
  122. return false;
  123. }
  124. /*
  125. * Does it folder exist?
  126. */
  127. if (!is_dir($this->folder)) {
  128. $this->errorMessages[] = 'The folder '.$this->folder.' doesn\'t exists please create it first.';
  129. return false;
  130. }
  131. /*
  132. * Does it folder writable?
  133. */
  134. if (!is_writable($this->folder)) {
  135. $this->errorMessages[] = 'The folder '.$this->folder.' is not writable.';
  136. return false;
  137. }
  138. /*
  139. * Does the file exist?
  140. */
  141. if (!file_exists($this->filePath)) {
  142. $this->errorMessages[] = 'File '.$this->filePath.' doesn\'t exists.';
  143. return false;
  144. }
  145. return true;
  146. }
  147. private function errorHandler()
  148. {
  149. if (!$this->fileInfo || !$this->fileInfo[0] || !$this->fileInfo[1]) {
  150. $this->errorMessages[] = 'Unable to get image dimensions.';
  151. return false;
  152. }
  153. if (!function_exists('imagegif') && $this->imageType == IMAGETYPE_GIF || !function_exists('imagejpeg') && $this->imageType == IMAGETYPE_JPEG || !function_exists('imagepng') && $this->imageType == IMAGETYPE_PNG) {
  154. $this->errorMessages[] = 'Filetype not supported.';
  155. return false;
  156. }
  157. return true;
  158. }
  159. public function initFileInfo()
  160. {
  161. $this->fileInfo = @getimagesize($this->filePath);
  162. $this->imageType = $this->fileInfo[2];
  163. }
  164. public function edit($fileName)
  165. {
  166. $this->fileName = $fileName;
  167. $this->filePath = $this->folder.'/'.$fileName;
  168. // Pre condition cheking.
  169. if (!$this->preErrorChecker()) {
  170. return false;
  171. }
  172. @chmod($this->filePath, 0666);
  173. $this->initFileInfo();
  174. if (!$this->errorHandler()) {
  175. return false;
  176. }
  177. $image = $this->createImageFrom();
  178. if (!empty($this->errorMessages)) {
  179. return false;
  180. }
  181. if (function_exists('imageantialias')) {
  182. imageantialias($image, true);
  183. }
  184. // Initial heigh and widht variable.
  185. $image_width = $this->fileInfo[0];
  186. $image_height = $this->fileInfo[1];
  187. $image_new_width = $this->fileInfo[0];
  188. $image_new_height = $this->fileInfo[1];
  189. if ($this->resizeWidth > 0 && $this->resizeHeight == 0) {
  190. $image_new_width = $this->resizeWidth;
  191. $image_ratio = $image_width / $image_new_width;
  192. if ($this->autoRatio) {
  193. $image_new_height = $image_height / $image_ratio;
  194. }
  195. }
  196. if ($this->resizeHeight > 0 && $this->resizeWidth == 0) {
  197. $image_new_height = $this->resizeHeight;
  198. $image_ratio = $image_height / $image_new_height;
  199. if ($this->autoRatio) {
  200. $image_new_width = $image_width / $image_ratio;
  201. }
  202. }
  203. if ($this->resizeHeight > 0 && $this->resizeWidth > 0 && $this->editType == 'resize') {
  204. $image_new_height = $this->resizeHeight;
  205. $image_new_width = $this->resizeWidth;
  206. }
  207. //Resizing
  208. if ($this->editType == 'resize' || $this->editType == 'resize_crop') {
  209. $imageEdited = imagecreatetruecolor($image_new_width, $image_new_height);
  210. @imagecopyresampled($imageEdited, $image, 0, 0, 0, 0, $image_new_width, $image_new_height, $this->fileInfo[0], $this->fileInfo[1]);
  211. }
  212. //Cropping process
  213. if ($this->editType == 'crop' || $this->editType == 'resize_crop') {
  214. $imageEdited = (isset($imageEdited)) ? $imageEdited : $image;
  215. $cropped = imagecreatetruecolor($this->cropWidth, $this->cropHeight);
  216. imagecopyresampled($cropped, $imageEdited, 0, 0, (($image_new_width / 2) - ($this->cropWidth / 2)), (($image_new_height / 2) - ($this->cropHeight / 2)), $this->cropWidth, $this->cropHeight, $this->cropWidth, $this->cropHeight);
  217. $imageEdited = $cropped;
  218. }
  219. $this->createImage($imageEdited);
  220. if (!empty($this->errorMessages)) {
  221. return false;
  222. }
  223. return true;
  224. }
  225. public function createImageFrom()
  226. {
  227. // create the initial copy from the original file
  228. switch ($this->imageType) {
  229. case IMAGETYPE_GIF:
  230. return imagecreatefromgif($this->filePath);
  231. exit;
  232. case IMAGETYPE_JPEG:
  233. return imagecreatefromjpeg($this->filePath);
  234. exit;
  235. case IMAGETYPE_PNG:
  236. return imagecreatefrompng($this->filePath);
  237. exit;
  238. default:
  239. $this->errorMessages[] = 'Unrecognized image format.';
  240. return false;
  241. }
  242. }
  243. public function createImage($imageEdited)
  244. {
  245. $file_extension = Upload::getFileExtension($this->fileName);
  246. $saveTo = (!empty($this->saveTo)) ? $this->saveTo : $this->folder;
  247. $new_filename = (!empty($this->newFileName)) ? $saveTo.'/'.$this->newFileName.'.'.$file_extension : $saveTo.'/'.$this->fileName;
  248. $new_filename = ($this->stripSpaces) ? str_replace(' ', '_', $new_filename) : $new_filename;
  249. // move the new file
  250. if ($this->imageType == IMAGETYPE_GIF) {
  251. if (!imagegif($imageEdited, $new_filename)) {
  252. $this->errorMessages[] = 'File path invalid.';
  253. }
  254. } elseif ($this->imageType == IMAGETYPE_JPEG) {
  255. if (!imagejpeg($imageEdited, $new_filename, $this->jpegCompression)) {
  256. $this->errorMessages[] = 'File path invalid.';
  257. }
  258. } elseif ($this->imageType == IMAGETYPE_PNG) {
  259. if (!imagepng($imageEdited, $new_filename)) {
  260. $this->errorMessages[] = 'File path invalid.';
  261. }
  262. }
  263. }
  264. public function getErrorMessage()
  265. {
  266. return $this->errorMessages;
  267. }
  268. } // End Image Modifier Class