PageRenderTime 78ms CodeModel.GetById 18ms RepoModel.GetById 3ms app.codeStats 0ms

/admin/libraries/image.php

https://bitbucket.org/nathanphan/joomla_zjdonation_custom
PHP | 310 lines | 179 code | 48 blank | 83 comment | 40 complexity | 899070dcf2b453492caa0deb0c7ec2fe MD5 | raw file
  1. <?php
  2. /**
  3. * @version $Id$
  4. * @author Joomseller
  5. * @package Joomla!
  6. * @subpackage ZJ_Donation
  7. * @copyright Copyright (C) 2008 - 2011 by Joomseller Solutions. All rights reserved.
  8. * @license http://www.gnu.org/licenses/gpl-3.0.html GNU/GPL, SEE LICENSE.php
  9. * This file may not be redistributed in whole or significant part.
  10. */
  11. // no direct access
  12. defined('_JEXEC') or die('Restricted access');
  13. /**
  14. * ZJ_Donation Image Class
  15. * @package ZJ_Donation
  16. * @subpackage Class
  17. * @since 1.0
  18. */
  19. class ZJ_DonationImage {
  20. /**
  21. * Get version of GD library.
  22. * @access public
  23. * @since 1.0
  24. */
  25. function getGDVersion($user_ver = 0) {
  26. if (!extension_loaded('gd')) {
  27. return;
  28. }
  29. static $gd_ver = 0;
  30. // just accept the specified setting if it's 1.
  31. if ($user_ver == 1) {
  32. $gd_ver = 1;
  33. return 1;
  34. }
  35. // use static variable if function was cancelled previously.
  36. if ($user_ver != 2 && $gd_ver > 0) {
  37. return $gd_ver;
  38. }
  39. // use the gd_info() function if posible.
  40. if (function_exists('gd_info')) {
  41. $ver_info = gd_info();
  42. $match = null;
  43. preg_match('/\d/', $ver_info['GD Version'], $match);
  44. $gd_ver = $match[0];
  45. return $match[0];
  46. }
  47. // if phpinfo() is disabled use a specified / fail-safe choice...
  48. if (preg_match('/phpinfo/', ini_get('disable_functions'))) {
  49. if ($user_ver == 2) {
  50. $gd_ver = 2;
  51. return 2;
  52. } else {
  53. $gd_ver = 1;
  54. return 1;
  55. }
  56. }
  57. // ...otherwise use phpinfo().
  58. ob_start();
  59. phpinfo(8);
  60. $info = ob_get_contents();
  61. ob_end_clean();
  62. $info = stristr($info, 'gd version');
  63. $match = null;
  64. preg_match('/\d/', $info, $match);
  65. $gd_ver = $match[0];
  66. return $match[0];
  67. }
  68. /**
  69. * Get real image width and height to resize.
  70. * @access public
  71. * @since 1.0
  72. */
  73. function getSize($image, $width, $height) {
  74. $info = @getimagesize($image); // width = info[0], height = info[1]
  75. if ($info[0] < $width && $info[1] < $height) {
  76. return array($info[0], $info[1]);
  77. }
  78. if ($info[0] / $width > $info[1] / $height) {
  79. $percentage = $width / $info[0];
  80. } else {
  81. $percentage = $height / $info[1];
  82. }
  83. return array(round($info[0] * $percentage), round($info[1] * $percentage));
  84. }
  85. /**
  86. * Get real size.
  87. * @access public
  88. * @since 1.0
  89. */
  90. function imageResize($width, $height, $max_width, $max_height) {
  91. if ($width < $max_width && $height < $max_height) {
  92. return array($width, $height);
  93. }
  94. if ($width / $max_width > $height / $max_height) {
  95. $percentage = $max_width / $width;
  96. } else {
  97. $percentage = $max_height / $height;
  98. }
  99. return array(round($width * $percentage), round($height * $percentage));
  100. }
  101. /**
  102. * Get image filename to upload.
  103. * @access public
  104. * @since 1.0
  105. */
  106. function sanitize($base_dir, $filename) {
  107. jimport('joomla.filesystem.file');
  108. //check for any leading/trailing dots and remove them (trailing shouldn't be possible cause of the getEXT check)
  109. $filename = preg_replace("/^[.]*/", '', $filename);
  110. $filename = preg_replace("/[.]*$/", '', $filename); //shouldn't be necessary, see above
  111. //we need to save the last dot position cause preg_replace will also replace dots
  112. $lastdotpos = strrpos($filename, '.');
  113. //replace invalid characters
  114. $chars = '[^0-9a-zA-Z()_-]';
  115. $filename = strtolower(preg_replace("/$chars/", '_', $filename));
  116. //get the parts before and after the dot (assuming we have an extension...check was done before)
  117. $beforedot = substr($filename, 0, $lastdotpos);
  118. $afterdot = substr($filename, $lastdotpos + 1);
  119. //make a unique filename for the image and check it is not already taken
  120. //if it is already taken keep trying till success
  121. $now = time();
  122. while (JFile::exists($base_dir . $beforedot . '_' . $now . '.' . $afterdot)) {
  123. $now++;
  124. }
  125. //create out of the seperated parts the new filename
  126. $filename = $beforedot . '_' . $now . '.' . $afterdot;
  127. return $filename;
  128. }
  129. /**
  130. * Add image subfix.
  131. * @access public
  132. * @since 1.0
  133. */
  134. function addSubfix($filename, $subfix) {
  135. //check for any leading/trailing dots and remove them (trailing shouldn't be possible cause of the getEXT check)
  136. $filename = preg_replace("/^[.]*/", '', $filename);
  137. $filename = preg_replace("/[.]*$/", '', $filename); //shouldn't be necessary, see above
  138. //we need to save the last dot position cause preg_replace will also replace dots
  139. $lastdotpos = strrpos($filename, '.');
  140. //replace invalid characters
  141. $chars = '[^0-9a-zA-Z()_-]';
  142. $filename = strtolower(preg_replace("/$chars/", '_', $filename));
  143. //get the parts before and after the dot (assuming we have an extension...check was done before)
  144. $beforedot = substr($filename, 0, $lastdotpos);
  145. $afterdot = substr($filename, $lastdotpos + 1);
  146. $filename = $beforedot . '_' . $subfix . '.' . $afterdot;
  147. return $filename;
  148. }
  149. /**
  150. * Check image for uploading.
  151. * @access public
  152. * @since 1.0
  153. */
  154. function check($file, $settings) {
  155. jimport('joomla.filesystem.file');
  156. $maxsize = $settings->image_max_size;
  157. $allowable = explode(',', str_replace(' ', '', $settings->image_allowed_ext));
  158. $sizelimit = $maxsize * 1024; //size limit in kb
  159. $imagesize = $file['size'];
  160. // check if the upload is an image...getimagesize will return false if not
  161. if (!getimagesize($file['tmp_name'])) {
  162. JError::raiseWarning(100, JText::_('Upload FAILED. The uploaded file is not an image.') . ': ' . htmlspecialchars($file['name'], ENT_COMPAT, 'UTF-8'));
  163. return false;
  164. }
  165. // check if the imagefiletype is valid
  166. $fileext = JFile::getExt($file['name']);
  167. if (!in_array(strtolower($fileext), $allowable)) {
  168. $allowed_exts = implode(', ', $allowable);
  169. JError::raiseWarning(100, JText::printf('The file must be %s', $allowed_exts).': ' . htmlspecialchars($file['name'], ENT_COMPAT, 'UTF-8'));
  170. return false;
  171. }
  172. // check filesize
  173. if ($imagesize > $sizelimit) {
  174. JError::raiseWarning(100, JText::_('File size is wrong').': ' . htmlspecialchars($file['name'], ENT_COMPAT, 'UTF-8'));
  175. return false;
  176. }
  177. // XSS check
  178. $xss_check = JFile::read($file['tmp_name'], false, 256);
  179. $html_tags = array('abbr', 'acronym', 'address', 'applet', 'area', 'audioscope', 'base', 'basefont', 'bdo', 'bgsound', 'big', 'blackface', 'blink', 'blockquote', 'body', 'bq', 'br', 'button', 'caption', 'center', 'cite', 'code', 'col', 'colgroup', 'comment', 'custom', 'dd', 'del', 'dfn', 'dir', 'div', 'dl', 'dt', 'em', 'embed', 'fieldset', 'fn', 'font', 'form', 'frame', 'frameset', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'hr', 'html', 'iframe', 'ilayer', 'img', 'input', 'ins', 'isindex', 'keygen', 'kbd', 'label', 'layer', 'legend', 'li', 'limittext', 'link', 'listing', 'map', 'marquee', 'menu', 'meta', 'multicol', 'nobr', 'noembed', 'noframes', 'noscript', 'nosmartquotes', 'object', 'ol', 'optgroup', 'option', 'param', 'plaintext', 'pre', 'rt', 'ruby', 's', 'samp', 'script', 'select', 'server', 'shadow', 'sidebar', 'small', 'spacer', 'span', 'strike', 'strong', 'style', 'sub', 'sup', 'table', 'tbody', 'td', 'textarea', 'tfoot', 'th', 'thead', 'title', 'tr', 'tt', 'ul', 'var', 'wbr', 'xml', 'xmp', '!DOCTYPE', '!--');
  180. foreach($html_tags as $tag) {
  181. // A tag is '<tagname ', so we need to add < and a space or '<tagname>'
  182. if(stristr($xss_check, '<'.$tag.' ') || stristr($xss_check, '<'.$tag.'>')) {
  183. JError::raiseWarning(100, JText::_('IE XSS Warning'));
  184. return false;
  185. }
  186. }
  187. return true;
  188. }
  189. /**
  190. * Resize image with width and height limit.
  191. * @access public
  192. * @since 1.0
  193. */
  194. function resize($file, $save, $width, $height) {
  195. // GD-Lib > 2.0 only!
  196. @unlink($save);
  197. // get sizes else stop
  198. if (!$infos = @getimagesize($file)) {
  199. return false;
  200. }
  201. // keep proportions
  202. $iWidth = $infos[0];
  203. $iHeight = $infos[1];
  204. $iRatioW = $width / $iWidth;
  205. $iRatioH = $height / $iHeight;
  206. if ($iRatioW < $iRatioH) {
  207. $iNewW = $iWidth * $iRatioW;
  208. $iNewH = $iHeight * $iRatioW;
  209. } else {
  210. $iNewW = $iWidth * $iRatioH;
  211. $iNewH = $iHeight * $iRatioH;
  212. }
  213. // don't resize images which are smaller than thumbs
  214. if ($infos[0] < $width && $infos[1] < $height) {
  215. $iNewW = $infos[0];
  216. $iNewH = $infos[1];
  217. }
  218. if($infos[2] == 1) {
  219. // image is type gif
  220. $imgA = imagecreatefromgif($file);
  221. $imgB = imagecreate($iNewW,$iNewH);
  222. // keep gif transparent color if possible
  223. if(function_exists('imagecolorsforindex') && function_exists('imagecolortransparent')) {
  224. $transcolorindex = imagecolortransparent($imgA);
  225. //transparent color exists
  226. if($transcolorindex >= 0 ) {
  227. $transcolor = imagecolorsforindex($imgA, $transcolorindex);
  228. $transcolorindex = imagecolorallocate($imgB, $transcolor['red'], $transcolor['green'], $transcolor['blue']);
  229. imagefill($imgB, 0, 0, $transcolorindex);
  230. imagecolortransparent($imgB, $transcolorindex);
  231. // fill white
  232. } else {
  233. $whitecolorindex = @imagecolorallocate($imgB, 255, 255, 255);
  234. imagefill($imgB, 0, 0, $whitecolorindex);
  235. }
  236. // fill white
  237. } else {
  238. $whitecolorindex = imagecolorallocate($imgB, 255, 255, 255);
  239. imagefill($imgB, 0, 0, $whitecolorindex);
  240. }
  241. imagecopyresampled($imgB, $imgA, 0, 0, 0, 0, $iNewW, $iNewH, $infos[0], $infos[1]);
  242. imagegif($imgB, $save);
  243. } elseif($infos[2] == 2) {
  244. // image is type jpg
  245. $imgA = imagecreatefromjpeg($file);
  246. $imgB = imagecreatetruecolor($iNewW,$iNewH);
  247. imagecopyresampled($imgB, $imgA, 0, 0, 0, 0, $iNewW, $iNewH, $infos[0], $infos[1]);
  248. imagejpeg($imgB, $save);
  249. } elseif($infos[2] == 3) {
  250. // image is type png
  251. $imgA = imagecreatefrompng($file);
  252. $imgB = imagecreatetruecolor($iNewW, $iNewH);
  253. imagealphablending($imgB, false);
  254. imagecopyresampled($imgB, $imgA, 0, 0, 0, 0, $iNewW, $iNewH, $infos[0], $infos[1]);
  255. imagesavealpha($imgB, true);
  256. imagepng($imgB, $save);
  257. } else {
  258. return false;
  259. }
  260. return true;
  261. }
  262. }