/engines/modules/portfolio/classes/class_image.php

https://github.com/masterscript/DLE-module-Portfolio · PHP · 463 lines · 325 code · 102 blank · 36 comment · 57 complexity · fde30eff4decb7ab0202c3531b44522a MD5 · raw file

  1. <?php
  2. /* ---------------------------------------------------------------------- *
  3. * Created *
  4. * by nick-on © Copyright 2009. All Rights Reserved! *
  5. * ---------------------------------------------------------------------- *
  6. * This file may no be redistributed in whole or significant part. *
  7. * ---------------------------------------------------------------------- */
  8. class class_image
  9. { var $image = false;
  10. var $allow_watermark = false;
  11. var $error = false;
  12. var $types = array ( '1' => 'GIF', '2' => 'JPG', '3' => 'PNG' );
  13. var $data = array ();
  14. var $gd_version = 2;
  15. var $watermark_light = '';
  16. var $watermark_dark = '';
  17. var $quality = 100;
  18. public function __construct ( $file_name = "" )
  19. {
  20. $gd_functions = array
  21. (
  22. 'imagecreate',
  23. 'imagecreatefromgif',
  24. 'imagecreatefromjpeg',
  25. 'imagecreatefrompng',
  26. 'imagegif',
  27. 'imagejpeg',
  28. 'imagepng',
  29. );
  30. foreach ( $gd_functions as $function_name )
  31. {
  32. if ( !function_exists ( $function_name ))
  33. {
  34. $this->error = GD_ERROR;
  35. break;
  36. }
  37. }
  38. if ( $this->error )
  39. {
  40. return false;
  41. }
  42. if ( $file_name != '' )
  43. { $this->open_image ( $file_name );
  44. }
  45. }
  46. /* ---------------------------------------------------------------- *
  47. * Init image params
  48. * ---------------------------------------------------------------- */
  49. function open_image ( $file_name )
  50. {
  51. if ( $this->error == GD_ERROR )
  52. { return false;
  53. }
  54. $this->image = false;
  55. $this->error = false;
  56. $this->data = array ();
  57. $this->data['file'] = $file_name;
  58. if ( !file_exists ( $this->data['file'] ))
  59. { $this->error = NO_IMG_FILE;
  60. return false;
  61. }
  62. /* --------------------------------------------------------- *
  63. * Get file type & size
  64. * --------------------------------------------------------- */
  65. if ( !$image_info = getimagesize ( $this->data['file'] ) )
  66. { $this->error = NO_IMAGE;
  67. return false;
  68. }
  69. if ( trim ( $this->data['type'] = $this->types [ $image_info[2] ] ) == '' )
  70. { $this->error = UNKNOWN_TYPE;
  71. return false;
  72. }
  73. switch ( $this->data['type'] )
  74. { case 'GIF' :
  75. $this->image = @imagecreatefromgif ( $this->data['file'] );
  76. break;
  77. case 'PNG' :
  78. $this->image = @imagecreatefrompng ( $this->data['file'] );
  79. break;
  80. case 'JPG' :
  81. $this->image = @imagecreatefromjpeg ( $this->data['file'] );
  82. break;
  83. }
  84. $this->data['width'] = imagesx ( $this->image );
  85. $this->data['height'] = imagesy ( $this->image );
  86. if ( $this->data['width'] == 0 OR $this->data['height'] == 0 )
  87. { $this->error = SIZE_ERROR;
  88. return false;
  89. }
  90. }
  91. /* --------------------------------------------------------------- *
  92. * Generate thumbnail
  93. * --------------------------------------------------------------- */
  94. function thumbnail ( $size, $crop_image = false )
  95. {
  96. if ( $this->error )
  97. { return false;
  98. }
  99. if ( is_numeric ( $size ) AND intval ( $size ) != 0 )
  100. {
  101. $size_num = intval ( $size );
  102. $size = array ();
  103. if ( $this->data['width'] >= $this->data['height'] )
  104. { $size['width'] = $size_num;
  105. $size['height'] = ceil ( ( $size['width'] / $this->data['width'] ) * $this->data['height'] );
  106. }
  107. else
  108. {
  109. $size['height'] = $size_num;
  110. $size['width'] = ceil ( ( $size['height'] / $this->data['height'] ) * $this->data['width'] );
  111. }
  112. }
  113. elseif ( !is_array ( $size ) OR intval ( $size['width'] ) == 0 OR intval ( $size['height'] ) == 0 )
  114. {
  115. $this->error = NO_THUMB_SIZE;
  116. return false;
  117. }
  118. if ( $this->image )
  119. {
  120. /* ----------------------------------------------------- *
  121. * Need to scale image ?
  122. * ----------------------------------------------------- */
  123. if ( $this->data['width'] > $size['width'] OR $this->data['height'] > $size['height'] )
  124. {
  125. /* ---------------------------------------------------- *
  126. * It's imposible to scale animated images
  127. * ---------------------------------------------------- */
  128. if ( $this->data['type'] == 'GIF' and $this->gif_animated () )
  129. {
  130. $this->error = ANIMATED_GIF; return false;
  131. }
  132. else
  133. {
  134. if ( $this->gd_version == 1 )
  135. { $_thumb = imagecreate ( $size['width'], $size['height'] );
  136. }
  137. else
  138. { $_thumb = imagecreatetruecolor ( $size['width'], $size['height'] );
  139. }
  140. if ( $size['width'] == $size['height'] OR $crop_image )
  141. {
  142. if ( $this->data['width'] > $this->data['height'] )
  143. {
  144. $this->scale_image ( $_thumb, $this->image, 0, 0, round((max($this->data['width'], $this->data['height']) - min($this->data['width'], $this->data['height']))/2), 0, $size['width'], $size['width'], min($this->data['width'], $this->data['height']), min($this->data['width'], $this->data['height']));
  145. }
  146. if ( $this->data['width'] < $this->data['height'] )
  147. { $this->scale_image ( $_thumb, $this->image, 0, 0, 0, 0, $size['width'], $size['width'], min ( $this->data['width'], $this->data['height'] ), min ( $this->data['width'], $this->data['height'] ));
  148. }
  149. if ( $this->data['width'] == $this->data['height'] )
  150. { $this->scale_image ( $_thumb, $this->image, 0, 0, 0, 0, $size['width'], $size['width'], $this->data['width'], $this->data['height'] );
  151. }
  152. }
  153. else
  154. {
  155. $this->scale_image ( $_thumb, $this->image, 0, 0, 0, 0, $size['width'], $size['height'], $this->data['width'], $this->data['height'] );
  156. }
  157. $this->image = $_thumb;
  158. }
  159. }
  160. }
  161. else
  162. { $this->error = IMG_CREATE_ERROR;
  163. return $this->error;
  164. }
  165. }
  166. /* --------------------------------------------------------------- *
  167. * Resize image
  168. * --------------------------------------------------------------- */
  169. function scale_image ( $dist, $source, $x1, $y1, $x2, $y2, $width_1, $height_1, $width_2, $height_2 )
  170. {
  171. if ( $this->gd_version == 1 )
  172. { imagecopyresized ( $dist, $source, $x1, $y1, $x2, $y2, $width_1, $height_1, $width_2, $height_2 );
  173. }
  174. else
  175. { imagecopyresampled ( $dist, $source, $x1, $y1, $x2, $y2, $width_1, $height_1, $width_2, $height_2 );
  176. }
  177. }
  178. /* --------------------------------------------------------------- *
  179. * Maybe it's gif animated image ?
  180. * --------------------------------------------------------------- */
  181. function gif_animated ()
  182. { $content = @file_get_contents ( $this->data['file'] );
  183. if ( trim ( $content ) == '' )
  184. { $this->error = READ_FILE_ERROR;
  185. return false;
  186. }
  187. $count = 0;
  188. $pos = 0;
  189. while ( $count < 2 )
  190. { $frame_one = strpos ( $content, "\x00\x21\xF9\x04", $pos );
  191. if ( $frame_one === false )
  192. {
  193. break;
  194. }
  195. else
  196. { $pos = $frame_one + 1;
  197. $frame_two = strpos ( $content, "\x00\x2C", $pos );
  198. if ( $frame_two === false )
  199. { break;
  200. }
  201. else
  202. { if ( $frame_one + 8 == $frame_two )
  203. { $count++;
  204. }
  205. $pos = $frame_two + 1;
  206. }
  207. }
  208. }
  209. return $count > 1;
  210. }
  211. /* --------------------------------------------------------------- *
  212. * Saving ?
  213. * --------------------------------------------------------------- */
  214. function save ( $file_name = '', $source = false )
  215. {
  216. if ( $this->error )
  217. {
  218. return false;
  219. }
  220. if ( trim ( $file_name ) == '' )
  221. { $file_name = $this->data['file'];
  222. }
  223. if ( !$source )
  224. { $source = $this->image;
  225. }
  226. $file_name = preg_replace( "/^(.*)\..+?$/", "\\1", $file_name ) . "." . strtolower ( $this->data['type'] );
  227. switch ( $this->data['type'] )
  228. { case 'GIF' :
  229. @imagegif ( $source, $file_name );
  230. break;
  231. case 'PNG' :
  232. @imagepng ( $source, $file_name );
  233. break;
  234. case 'JPG' :
  235. @imagejpeg ( $source, $file_name, $this->quality );
  236. break;
  237. }
  238. if ( !file_exists ( $file_name ))
  239. {
  240. $this->error = SAVE_ERROR; return false;
  241. }
  242. @imagedestroy ( $source );
  243. @chmod ( $file_name, 0666 );
  244. return $file_name;
  245. }
  246. /* ---------------------------------------------------------------- *
  247. * Insert watermark!
  248. * ---------------------------------------------------------------- */
  249. function watermark ( $position = BOTTOM_RIGHT, $source = false, $margin = 7 )
  250. {
  251. if ( $this->error )
  252. {
  253. return false;
  254. }
  255. if ( !$this->allow_watermark )
  256. { $this->error = NOT_ALLOWED;
  257. return false;
  258. }
  259. if ( $this->data['type'] == 'GIF' AND $this->gif_animated() )
  260. { $this->error = ANIMATED_GIF;
  261. return false;
  262. }
  263. if ( !$source )
  264. {
  265. $source = $this->image;
  266. }
  267. if ( !file_exists ( $this->watermark_light ) OR !file_exists ( $this->watermark_dark )){
  268. $this->error = NO_WATERMARK_FILE;
  269. return false;
  270. }
  271. $image_width = imagesx ( $this->image );
  272. $image_height = imagesy ( $this->image );
  273. $watermark_size = getimagesize ( $this->watermark_light );
  274. switch ( $position )
  275. {
  276. case TOP_RIGHT :
  277. $pos_x = $image_width - $margin - $watermark_size[0];
  278. $pos_y = $margin;
  279. break;
  280. case TOP_LEFT :
  281. $pos_x = $margin;
  282. $pos_y = $margin;
  283. break;
  284. case BOTTOM_RIGHT :
  285. $pos_x = $image_width - $margin - $watermark_size[0];
  286. $pos_y = $image_height - $margin - $watermark_size[1];
  287. break;
  288. case BOTTOM_LEFT :
  289. $pos_x = $margin;
  290. $pos_y = $image_height - $margin - $watermark_size[1];
  291. break;
  292. case CENTER:
  293. $pos_x = ceil ( $image_width / 2 ) - ceil ( $watermark_size[0] / 2 );
  294. $pos_y = ceil ( $image_height / 2 ) - ceil ( $watermark_size[1] / 2 );
  295. break;
  296. case TOP :
  297. $pos_x = ceil ( $image_width / 2 ) - ceil ( $watermark_size[0] / 2 );
  298. $pos_y = $margin;
  299. break;
  300. case BOTTOM :
  301. $pos_x = ceil ( $image_width / 2 ) - ceil ( $watermark_size[0] / 2 );
  302. $pos_y = $image_height - $margin;
  303. break;
  304. case LEFT :
  305. $pos_x = $margin;
  306. $pos_y = ceil ( $image_height / 2 ) - ceil ( $watermark_size[1] / 2 );
  307. break;
  308. case RIGHT :
  309. $pos_x = $image_width - $margin - $watermark_size[0];
  310. $pos_y = ceil ( $image_height / 2 ) - ceil ( $watermark_size[1] / 2 );
  311. break;
  312. default :
  313. $pos_x = $image_width - $margin - $watermark_size[0];
  314. $pos_y = $image_height - $margin - $watermark_size[1];
  315. break;
  316. }
  317. if ( $pos_x < 0 OR $pos_y < 0 OR $watermark_size[0] + $margin > $image_width OR $watermark_size[1] + $margin > $image_height )
  318. {
  319. return false;
  320. }
  321. $pixel = imagecreatetruecolor ( 1, 1 );
  322. imagecopyresampled ( $pixel, $source, 0, 0, $pos_x, $pos_y, 1, 1, $watermark_size[0], $watermark_size[1] );
  323. $RGB = imagecolorat ( $pixel, 0, 0 );
  324. $red = ( $RGB >> 16 ) & 0xFF;
  325. $green = ( $RGB >> 8 ) & 0xFF;
  326. $blue = $RGB & 0xFF;
  327. $min = min ( $red, $green, $blue );
  328. $max = max ( $red, $green, $blue );
  329. $light = (double)(( $min + $max ) / 510.0 );
  330. @imagedestroy ( $pixel );
  331. $watermark_image = $light < 0.5 ? $this->watermark_light : $this->watermark_dark;
  332. $watermark = @imagecreatefrompng ( $watermark_image );
  333. imagealphablending ( $this->image, true );
  334. imagealphablending ( $watermark, true );
  335. imagecopy ( $this->image, $watermark, $pos_x, $pos_y, 0, 0, $watermark_size[0], $watermark_size[1] );
  336. imagedestroy ( $watermark );
  337. }
  338. /* ---------------------------------------------------------------- *
  339. * Show GD image
  340. * ---------------------------------------------------------------- */
  341. function show ( $source = false )
  342. { if ( !$source )
  343. { $source = $this->image;
  344. }
  345. flush();
  346. switch ( $this->data['type'] )
  347. { case 'GIF' :
  348. @header('Content-type: image/gif');
  349. break;
  350. case 'PNG' :
  351. @header('Content-Type: image/png' );
  352. break;
  353. case 'JPG' :
  354. @header('Content-Type: image/jpeg' );
  355. break;
  356. }
  357. print_r ( $this->image );
  358. exit ();
  359. }
  360. }
  361. ?>