/panada/library/image.php

https://github.com/ismanramadhan/Panada · PHP · 290 lines · 140 code · 63 blank · 87 comment · 49 complexity · 5eef8a9d55df64cee1791160306e91c3 MD5 · raw file

  1. <?php defined('THISPATH') or die('Can\'t access directly!');
  2. /**
  3. * Panada Image Modifier.
  4. *
  5. * @package Panada
  6. * @subpackage Library
  7. * @author Iskandar Soesman
  8. * @since Version 0.1
  9. */
  10. class Library_image {
  11. /**
  12. * @var string EN: Option for editng image the option is: resize | crop | resize_crop.
  13. * ID: Opisi edit image pilihannya resize | crop | resize_crop.
  14. */
  15. public $edit_type = 'resize';
  16. /**
  17. *@var boolean EN: Adjust width/heigh autmaticly for resizing proccess.
  18. * ID: Jika yang di set adalah width maka height-nya menyesuaikan dan sebaliknya.
  19. */
  20. public $auto_ratio = true;
  21. /**
  22. * @var integer EN: New image width.
  23. */
  24. public $resize_width = 0;
  25. /**
  26. * @var integer EN: New image height.
  27. */
  28. public $resize_height = 0;
  29. /**
  30. * @var integer EN: Crope width size.
  31. */
  32. public $crop_width = 0;
  33. /**
  34. * @var integer EN: Crope height size.
  35. */
  36. public $crop_height = 0;
  37. /**
  38. * @var string EN: Source file name.
  39. */
  40. public $file_name = '';
  41. /**
  42. * @var string EN: Source full path location.
  43. */
  44. public $file_path;
  45. /**
  46. * @var string EN: Source file info.
  47. */
  48. public $file_info = array();
  49. /**
  50. * @var string EN: Define new file name.
  51. */
  52. public $new_file_name = '';
  53. /**
  54. * @var boolean EN: Remove any space in file name.
  55. * ID: Hapus spasi pada nama file.
  56. */
  57. public $strip_spaces = true;
  58. /**
  59. * @var string EN: Source folder location.
  60. */
  61. public $folder = '';
  62. /**
  63. * @var string EN: Source image type: gif, jpg or png.
  64. */
  65. public $image_type;
  66. /**
  67. * @var integer EN: The value jpg compression. the range is 0 - 100.
  68. * ID: Nilai untuk menentukan kwalitas kompresi jpg. Rentannya 0 - 100.
  69. */
  70. public $jpeg_compression = 90;
  71. /**
  72. * @var string EN: Folder to placed new edited image.
  73. * ID: Folder tempat menyimpan file baru.
  74. */
  75. public $save_to = '';
  76. /**
  77. * @var array EN: Initiate the error mesages.
  78. * ID: Set pesan-pesan error.
  79. */
  80. public $error_messages = array();
  81. /**
  82. * EN: Class constructor
  83. */
  84. public function __construct(){
  85. if( ! function_exists('imagecopyresampled') )
  86. Library_error::costume(500, 'Image resizing function that required by Library_images Class is not available.');
  87. }
  88. private function pre_error_checker(){
  89. /**
  90. * EN: Check is folder destionation has set.
  91. * ID: Pastikan folder tujan telah ditentukan.
  92. */
  93. if( empty($this->folder) ) {
  94. $this->error_messages[] = 'No folder located. Please define the folder location.';
  95. return false;
  96. }
  97. /**
  98. * EN: Does it folder exist?
  99. * ID: Apakah folder tersebut ada?
  100. */
  101. if( ! is_dir($this->folder) ) {
  102. $this->error_messages[] = 'The folder '.$this->folder.' doesn\'t exists please create it first.';
  103. return false;
  104. }
  105. /**
  106. * EN: Does it folder writable?
  107. * ID: Apakah folder tersebut bisa untuk tulis/hapus?
  108. */
  109. if( ! is_writable($this->folder) ) {
  110. $this->error_messages[] = 'The folder '.$this->folder.' is not writable.';
  111. return false;
  112. }
  113. /**
  114. * EN: Does the file exist?
  115. * ID: Apakah file yang akan dimodifiksi ada?
  116. */
  117. if( ! file_exists($this->file_path) ) {
  118. $this->error_messages[] = 'File '.$this->file_path.' doesn\'t exists.';
  119. return false;
  120. }
  121. return true;
  122. }
  123. private function error_handler(){
  124. if( ! $this->file_info || ! $this->file_info[0] || ! $this->file_info[1] ) {
  125. $this->error_messages[] = 'Unable to get image dimensions.';
  126. return false;
  127. }
  128. if ( ! function_exists( 'imagegif' ) && $this->image_type == IMAGETYPE_GIF || ! function_exists( 'imagejpeg' ) && $this->image_type == IMAGETYPE_JPEG || ! function_exists( 'imagepng' ) && $this->image_type == IMAGETYPE_PNG ) {
  129. $this->error_messages[] = 'Filetype not supported.';
  130. return false;
  131. }
  132. return true;
  133. }
  134. public function init_file_info(){
  135. $this->file_info = @getimagesize($this->file_path);
  136. $this->image_type = $this->file_info[2];
  137. }
  138. public function edit($file_name){
  139. $this->file_name = $file_name;
  140. $this->file_path = $this->folder . '/' . $file_name;
  141. // EN: Pre condition cheking.
  142. if( ! $this->pre_error_checker() )
  143. return false;
  144. @chmod($this->file_path, 0666);
  145. $this->init_file_info();
  146. if ( ! $this->error_handler() )
  147. return false;
  148. $image = $this->create_image_from();
  149. if( ! empty($this->error_messages) )
  150. return false;
  151. if ( function_exists('imageantialias') )
  152. imageantialias( $image, true );
  153. // EN: Initial heigh and widht variable.
  154. $image_width = $this->file_info[0];
  155. $image_height = $this->file_info[1];
  156. $image_new_width = $this->file_info[0];
  157. $image_new_height = $this->file_info[1];
  158. if( $this->resize_width > 0 && $this->resize_height == 0 ) {
  159. $image_new_width = $this->resize_width;
  160. $image_ratio = $image_width / $image_new_width;
  161. if($this->auto_ratio)
  162. $image_new_height = $image_height / $image_ratio;
  163. }
  164. if( $this->resize_height > 0 && $this->resize_width == 0 ) {
  165. $image_new_height = $this->resize_height;
  166. $image_ratio = $image_height / $image_new_height;
  167. if($this->auto_ratio)
  168. $image_new_width = $image_width / $image_ratio;
  169. }
  170. if( $this->resize_height > 0 && $this->resize_width > 0 && $this->edit_type == 'resize' ) {
  171. $image_new_height = $this->resize_height;
  172. $image_new_height = $this->resize_height;
  173. }
  174. //EN: Resizing
  175. if($this->edit_type == 'resize' || $this->edit_type == 'resize_crop') {
  176. $image_edited = imagecreatetruecolor( $image_new_width, $image_new_height);
  177. @imagecopyresampled( $image_edited, $image, 0, 0, 0, 0, $image_new_width, $image_new_height, $this->file_info[0], $this->file_info[1] );
  178. }
  179. //EN: Cropping process
  180. if($this->edit_type == 'crop' || $this->edit_type == 'resize_crop') {
  181. $image_edited = ( isset($image_edited) ) ? $image_edited : $image;
  182. $cropped = imagecreatetruecolor($this->crop_width, $this->crop_height);
  183. imagecopyresampled($cropped, $image_edited, 0, 0, ( ($image_new_width/2) - ($this->crop_width/2) ), ( ($image_new_height/2) - ($this->crop_height/2) ), $this->crop_width, $this->crop_height, $this->crop_width, $this->crop_height);
  184. $image_edited = $cropped;
  185. }
  186. $this->create_image($image_edited);
  187. if( ! empty($this->error_messages) )
  188. return false;
  189. return true;
  190. }
  191. public function create_image_from(){
  192. // create the initial copy from the original file
  193. switch($this->image_type) {
  194. case IMAGETYPE_GIF:
  195. return imagecreatefromgif( $this->file_path );
  196. exit;
  197. case IMAGETYPE_JPEG:
  198. return imagecreatefromjpeg( $this->file_path );
  199. exit;
  200. case IMAGETYPE_PNG:
  201. return imagecreatefrompng( $this->file_path );
  202. exit;
  203. default:
  204. $this->error_messages[] = 'Unrecognized image format.';
  205. return false;
  206. }
  207. }
  208. public function create_image($image_edited){
  209. $file_extension = Library_upload::get_file_extension($this->file_name);
  210. $save_to = ( ! empty($this->save_to) ) ? $this->save_to : $this->folder;
  211. $new_filename = ( ! empty($this->new_file_name) )? $save_to . '/' . $this->new_file_name . '.' . $file_extension : $this->file_path;
  212. $new_filename = ($this->strip_spaces) ? str_replace(' ', '_', $new_filename) : $new_filename;
  213. // move the new file
  214. if ( $this->image_type == IMAGETYPE_GIF ) {
  215. if ( ! imagegif( $image_edited, $new_filename ) )
  216. $this->error_messages[] = 'File path invalid.';
  217. }
  218. elseif ( $this->image_type == IMAGETYPE_JPEG ) {
  219. if (! imagejpeg( $image_edited, $new_filename, $this->jpeg_compression ) )
  220. $this->error_messages[] = 'File path invalid.';
  221. }
  222. elseif ( $this->image_type == IMAGETYPE_PNG ) {
  223. if (! imagepng( $image_edited, $new_filename ) )
  224. $this->error_messages[] = 'File path invalid.';
  225. }
  226. }
  227. } // End Image Modifier Class