PageRenderTime 45ms CodeModel.GetById 18ms RepoModel.GetById 1ms app.codeStats 0ms

/panada/Resources/Image.php

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