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

/lib/media.php

https://github.com/jasonchua/ndxzwebsite
PHP | 417 lines | 253 code | 48 blank | 116 comment | 30 complexity | 52e5f8cee2d2274b72e99a49081252cf MD5 | raw file
Possible License(s): AGPL-1.0
  1. <?php if (!defined('SITE')) exit('No direct script access allowed');
  2. /**
  3. * Media class
  4. *
  5. * Resizes and thumbnails images
  6. *
  7. * @version 1.0
  8. * @author Vaska
  9. */
  10. class Media
  11. {
  12. var $image;
  13. var $path;
  14. var $filename;
  15. var $quality;
  16. var $filemime;
  17. var $maxsize;
  18. var $thumbsize;
  19. var $sizelimit;
  20. var $size = array();
  21. var $new_size = array();
  22. var $makethumb = FALSE;
  23. var $final_size = array();
  24. var $out_size = array();
  25. var $uploads = array();
  26. var $sys_thumb = 100;
  27. var $offset = array();
  28. var $sys_size = array();
  29. var $type;
  30. var $input_image;
  31. var $upload_max_size;
  32. var $file_size;
  33. var $tRed;
  34. var $tBlue;
  35. var $tGreen;
  36. var $tFlag = FALSE;
  37. /**
  38. * Returns allowed uploads (filetypes from config.php) array and max size
  39. *
  40. * @param void
  41. * @return mixed
  42. */
  43. function Media()
  44. {
  45. global $uploads;
  46. $this->uploads = $uploads;
  47. $this->upload_max_size();
  48. }
  49. /**
  50. * Returns filetype by file extension
  51. *
  52. * @param void
  53. * @return string
  54. */
  55. function getFileType()
  56. {
  57. $type = explode('.', $this->filename);
  58. $this->filemime = array_pop($type);
  59. }
  60. /**
  61. * Returns array of image filetypes
  62. *
  63. * @param void
  64. * @return array
  65. */
  66. function allowThumbs()
  67. {
  68. return $this->uploads['images'];
  69. }
  70. /**
  71. * Returns server settings for max upload size
  72. *
  73. * @param void
  74. * @return integer
  75. */
  76. function upload_max_size()
  77. {
  78. $upload_max_filesize = ini_get('upload_max_filesize');
  79. $upload_max_filesize = preg_replace('/M/', '000000', $upload_max_filesize);
  80. $post_max_size = ini_get('post_max_size');
  81. $post_max_size = preg_replace('/M/', '000000', $post_max_size);
  82. $this->upload_max_size = ($post_max_size >= $upload_max_filesize) ? $upload_max_filesize : $post_max_size;
  83. }
  84. /**
  85. * Return destroys input image
  86. *
  87. * @param void
  88. * @return mixed
  89. */
  90. function uploader()
  91. {
  92. $this->getFileType();
  93. $this->get_input();
  94. $this->size = getimagesize($this->image);
  95. // first image
  96. $this->upload_image($this->maxsize);
  97. $this->file_size();
  98. // system thumbnail
  99. $this->sys_thumb($this->sys_thumb);
  100. // we'll need to distinguish this for only images
  101. if (($this->makethumb == TRUE) && (in_array($this->filemime, $this->allowThumbs())))
  102. {
  103. $this->upload_image($this->thumbsize, TRUE);
  104. }
  105. imagedestroy($this->input_image);
  106. }
  107. /**
  108. * Deals with the bits
  109. * Oh. So. Messy. ;)
  110. *
  111. * @param integer $maxwidth
  112. * @param boolean $thumb
  113. * @return integer
  114. */
  115. function upload_image($maxwidth, $thumb=FALSE)
  116. {
  117. if (($maxwidth != 9999) || ($thumb == TRUE))
  118. {
  119. // get the new sizes
  120. $this->resizing($maxwidth);
  121. $output_image = imagecreatetruecolor($this->new_size['w'], $this->new_size['h']);
  122. // if we have transparency in the image
  123. // it sucks that PHP auto sets background to black!!!!!!!
  124. if ($this->tFlag == TRUE)
  125. {
  126. imagecolortransparent($output_image, imagecolorallocate($output_image,
  127. $this->tRed, $this->tGreen, $this->tBlue));
  128. }
  129. // png special handling rules
  130. if ($this->filemime == 'png')
  131. {
  132. // http://be.php.net/manual/en/function.imagesavealpha.php
  133. imagealphablending($output_image, false);
  134. imagesavealpha($output_image, true);
  135. }
  136. // resizing
  137. @imagecopyresampled($output_image, $this->input_image, 0, 0, 0, 0,
  138. $this->new_size['w'], $this->new_size['h'], $this->size[0], $this->size[1]);
  139. // how do we flag when we are working on thumbs>
  140. if ($thumb == TRUE)
  141. {
  142. $this->image = $this->path . 'th-' . $this->filename;
  143. }
  144. $this->do_output($output_image, $this->image);
  145. imagedestroy($output_image);
  146. }
  147. else
  148. {
  149. // no resize - get file x, y
  150. $this->out_size['x'] = $this->size[0];
  151. $this->out_size['y'] = $this->size[1];
  152. return;
  153. }
  154. if ($thumb == FALSE)
  155. {
  156. $this->out_size['x'] = $this->new_size['w'];
  157. $this->out_size['y'] = $this->new_size['h'];
  158. }
  159. return;
  160. }
  161. /**
  162. * Returns file size
  163. *
  164. * @param void
  165. * @return integer
  166. */
  167. function file_size()
  168. {
  169. $size = str_replace('.', '', @filesize($this->image));
  170. $this->file_size = ($size == 0) ? 0 : $size;
  171. }
  172. /**
  173. * Returns input image according to type
  174. *
  175. * @param void
  176. * @return variable
  177. */
  178. function get_input()
  179. {
  180. switch($this->filemime)
  181. {
  182. case 'gif':
  183. $this->checkBackground();
  184. $this->input_image = imagecreatefromgif($this->image);
  185. break;
  186. case 'jpg':
  187. $this->input_image = imagecreatefromjpeg($this->image);
  188. break;
  189. case 'jpeg':
  190. $this->input_image = imagecreatefromjpeg($this->image);
  191. break;
  192. case 'png':
  193. $this->input_image = imagecreatefrompng($this->image);
  194. break;
  195. }
  196. }
  197. /**
  198. * Checks file to find background transparency
  199. *
  200. * @param void
  201. * @return string
  202. */
  203. function checkBackground()
  204. {
  205. // we need to determine transparency for gifs
  206. // http://be.php.net/imagecolortransparent
  207. $fp = fopen($this->image, 'rb');
  208. $result = fread($fp, 13);
  209. $colorFlag = ord(substr($result, 10, 1)) >> 7;
  210. $background = ord(substr($result, 11));
  211. if ($colorFlag)
  212. {
  213. $tableSizeNeeded = ($background + 1) * 3;
  214. $result = fread($fp, $tableSizeNeeded);
  215. $this->tRed = ord(substr($result, $background * 3, 1));
  216. $this->tGreen = ord(substr($result, $background * 3 + 1, 1));
  217. $this->tBlue = ord(substr($result, $background * 3 + 2, 1));
  218. if (isset($this->tRed) && isset($this->tGreen) && isset($this->tBlue))
  219. {
  220. $this->tFlag = TRUE;
  221. }
  222. }
  223. fclose($fp);
  224. return;
  225. }
  226. /**
  227. * Returns output image according to type
  228. *
  229. * @param string $output_image
  230. * @param string $image
  231. * @return string
  232. */
  233. function do_output($output_image, $image)
  234. {
  235. switch($this->filemime) {
  236. case 'gif':
  237. imagegif($output_image, $image);
  238. break;
  239. case 'jpg':
  240. imagejpeg($output_image, $image, $this->quality);
  241. break;
  242. case 'jpeg':
  243. imagejpeg($output_image, $image, $this->quality);
  244. break;
  245. case 'png':
  246. imagepng($output_image, $image);
  247. break;
  248. }
  249. }
  250. /**
  251. * Returns array of file size
  252. * (natural dimensions)
  253. *
  254. * @param integer $maxwidth
  255. * @return array
  256. */
  257. function resizing($maxwidth)
  258. {
  259. $width_percentage = $maxwidth / $this->size[0];
  260. $height_percentage = $maxwidth / $this->size[1];
  261. if (($this->size[0] > $maxwidth) || ($this->size[1] > $maxwidth))
  262. {
  263. if ($width_percentage <= $height_percentage)
  264. {
  265. $this->new_size['w'] = round($width_percentage * $this->size[0]);
  266. $this->new_size['h'] = round($width_percentage * $this->size[1]);
  267. }
  268. else
  269. {
  270. $this->new_size['w'] = round($height_percentage * $this->size[0]);
  271. $this->new_size['h'] = round($height_percentage * $this->size[1]);
  272. }
  273. }
  274. else
  275. { // square images ?
  276. $this->new_size['w'] = $this->size[0];
  277. $this->new_size['h'] = $this->size[1];
  278. }
  279. }
  280. /**
  281. * Returns array of file size
  282. * (square thumbnails)
  283. *
  284. * @param void
  285. * @return array
  286. */
  287. function sys_resize()
  288. {
  289. $this->sys_size['w'] = $this->size[0];
  290. $this->sys_size['h'] = $this->size[1];
  291. if ($this->sys_size['w'] > $this->sys_size['h'])
  292. {
  293. $this->offset['w'] = ($this->sys_size['w'] - $this->sys_size['h'])/2;
  294. $this->offset['h'] = 0;
  295. $this->sys_size['w'] = $this->sys_size['h'];
  296. }
  297. elseif ($this->sys_size['h'] > $this->sys_size['w'])
  298. {
  299. $this->offset['w'] = 0;
  300. $this->offset['h'] = ($this->sys_size['h'] - $this->sys_size['w'])/2;
  301. $this->sys_size['h'] = $this->sys_size['w'];
  302. }
  303. else
  304. {
  305. $this->offset['w'] = 0;
  306. $this->offset['h'] = 0;
  307. $this->sys_size['w'] = $this->sys_size['h'];
  308. }
  309. }
  310. /**
  311. * Returns imagedestroy of input image
  312. *
  313. * @param integer $maxwidth
  314. * @return mixed
  315. */
  316. function sys_thumb($maxwidth)
  317. {
  318. $this->sys_resize();
  319. $output_image = imagecreatetruecolor($this->sys_thumb, $this->sys_thumb);
  320. // if we have transparency in the image
  321. // it sucks that PHP auto sets background to black!!!!!!!
  322. if ($this->tFlag == TRUE)
  323. {
  324. imagecolortransparent($output_image, imagecolorallocate($output_image,
  325. $this->tRed, $this->tGreen, $this->tBlue));
  326. }
  327. // png special handling rules
  328. if ($this->filemime == 'png')
  329. {
  330. // http://be.php.net/manual/en/function.imagesavealpha.php
  331. imagealphablending($output_image, false);
  332. imagesavealpha($output_image, true);
  333. }
  334. @imagecopyresampled($output_image, $this->input_image, 0, 0,
  335. $this->offset['w'], $this->offset['h'],
  336. $this->sys_thumb, $this->sys_thumb,
  337. $this->sys_size['w'], $this->sys_size['h']);
  338. // for sys- naming convention
  339. $image = $this->path . 'sys-' . $this->filename;
  340. $this->do_output($output_image, $image);
  341. imagedestroy($output_image);
  342. return;
  343. }
  344. /**
  345. * Returns new file name based upon exiting files to prevent name collisions
  346. *
  347. * @param string $filename
  348. * @return string
  349. */
  350. function checkName($filename)
  351. {
  352. static $v = 1;
  353. if (file_exists($this->path . '/' . $filename . $this->type))
  354. {
  355. // remove the previous version number
  356. $filename = preg_replace('/_v[0-9]{1,3}$/i', '', $filename);
  357. $v++;
  358. $filename = $filename . '_v' . $v;
  359. $filename = $this->checkName($filename);
  360. }
  361. else
  362. {
  363. $v = 1;
  364. return $filename;
  365. }
  366. return $filename;
  367. }
  368. }
  369. ?>