PageRenderTime 43ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/Quản lý website bán mỹ phẩm PHP/administrator/components/com_virtuemart/helpers/img2thumb.php

https://gitlab.com/phamngsinh/baitaplon_sinhvien
PHP | 393 lines | 229 code | 34 blank | 130 comment | 36 complexity | 729f28bcda84ece5f33c764470231cd6 MD5 | raw file
  1. <?php
  2. /**
  3. * class Image2Thumbnail
  4. * Thumbnail creation with PHP4 and GDLib (recommended, but not mandatory: 2.0.1 !)
  5. *
  6. *
  7. * @author Andreas Martens <heyn@plautdietsch.de>
  8. * @author Patrick Teague <webdude@veslach.com>
  9. * @author Soeren Eberhardt <soeren|at|virtuemart.net>
  10. *@version 1.0b
  11. *@date modified 11/22/2004
  12. *@modifications
  13. * - added support for GDLib < 2.0.1
  14. * - added support for reading gif images
  15. * - makes jpg thumbnails
  16. * - changed several groups of 'if' statements to single 'switch' statements
  17. * - commented out original code so modification could be identified.
  18. */
  19. class Img2Thumb {
  20. // New modification
  21. /**
  22. * private variables - do not use
  23. *
  24. * @var int $bg_red 0-255 - red color variable for background filler
  25. * @var int $bg_green 0-255 - green color variable for background filler
  26. * @var int $bg_blue 0-255 - blue color variable for background filler
  27. * @var int $maxSize 0-1 - true/false - should thumbnail be filled to max pixels
  28. */
  29. var $bg_red;
  30. var $bg_green;
  31. var $bg_blue;
  32. var $maxSize;
  33. /**
  34. * @var string Filename for the thumbnail
  35. */
  36. var $fileout;
  37. /**
  38. * Constructor - requires following vars:
  39. *
  40. * @param string $filename image path
  41. *
  42. * These are additional vars:
  43. *
  44. * @param int $newxsize new maximum image width
  45. * @param int $newysize new maximum image height
  46. * @param string $fileout output image path
  47. * @param int $thumbMaxSize whether thumbnail should have background fill to make it exactly $newxsize x $newysize
  48. * @param int $bgred 0-255 - red color variable for background filler
  49. * @param int $bggreen 0-255 - green color variable for background filler
  50. * @param int $bgblue 0-255 - blue color variable for background filler
  51. *
  52. */
  53. function Img2Thumb($filename, $newxsize=60, $newysize=60, $fileout='',
  54. $thumbMaxSize=0, $bgred=0, $bggreen=0, $bgblue=0)
  55. {
  56. // New modification - checks color int to be sure within range
  57. if($thumbMaxSize)
  58. {
  59. $this->maxSize = true;
  60. }
  61. else
  62. {
  63. $this->maxSize = false;
  64. }
  65. if($bgred>=0 || $bgred<=255)
  66. {
  67. $this->bg_red = $bgred;
  68. }
  69. else
  70. {
  71. $this->bg_red = 0;
  72. }
  73. if($bggreen>=0 || $bggreen<=255)
  74. {
  75. $this->bg_green = $bggreen;
  76. }
  77. else
  78. {
  79. $this->bg_green = 0;
  80. }
  81. if($bgblue>=0 || $bgblue<=255)
  82. {
  83. $this->bg_blue = $bgblue;
  84. }
  85. else
  86. {
  87. $this->bg_blue = 0;
  88. }
  89. $this->NewImgCreate($filename,$newxsize,$newysize,$fileout);
  90. }
  91. /**
  92. *
  93. * private function - do not call
  94. *
  95. */
  96. private function NewImgCreate($filename,$newxsize,$newysize,$fileout)
  97. {
  98. // if( !function_exists('imagecreatefromjpeg') ){
  99. // $app = JFactory::getApplication();
  100. // $app->enqueueMessage('This server does NOT suppport auto generating Thumbnails by jpg');
  101. // }
  102. $type = $this->GetImgType($filename);
  103. $pathinfo = pathinfo( $fileout );
  104. if( empty( $pathinfo['extension'])) {
  105. $fileout .= '.'.$type;
  106. }
  107. $this->fileout = $fileout;
  108. switch($type){
  109. case "gif":
  110. // unfortunately this function does not work on windows
  111. // via the precompiled php installation :(
  112. // it should work on all other systems however.
  113. if( function_exists("imagecreatefromgif") ) {
  114. $orig_img = imagecreatefromgif($filename);
  115. } else {
  116. $app = JFactory::getApplication();
  117. $app->enqueueMessage('This server does NOT suppport auto generating Thumbnails by gif');
  118. exit;
  119. }
  120. break;
  121. case "jpg":
  122. if( function_exists("imagecreatefromjpeg") ) {
  123. $orig_img = imagecreatefromjpeg($filename);
  124. } else {
  125. $app = JFactory::getApplication();
  126. $app->enqueueMessage('This server does NOT suppport auto generating Thumbnails by jpg');
  127. exit;
  128. }
  129. break;
  130. case "png":
  131. if( function_exists("imagecreatefrompng") ) {
  132. $orig_img = imagecreatefrompng($filename);
  133. } else {
  134. $app = JFactory::getApplication();
  135. $app->enqueueMessage('This server does NOT suppport auto generating Thumbnails by png');
  136. exit;
  137. }
  138. break;
  139. }
  140. $new_img =$this->NewImgResize($orig_img,$newxsize,$newysize,$filename);
  141. if (!empty($fileout))
  142. {
  143. $this-> NewImgSave($new_img,$fileout,$type);
  144. }
  145. else
  146. {
  147. $this->NewImgShow($new_img,$type);
  148. }
  149. ImageDestroy($new_img);
  150. ImageDestroy($orig_img);
  151. }
  152. /**
  153. * Maybe adding sharpening with
  154. * $sharpenMatrix = array
  155. (
  156. array(-1.2, -1, -1.2),
  157. array(-1, 20, -1),
  158. array(-1.2, -1, -1.2)
  159. );
  160. // calculate the sharpen divisor
  161. $divisor = array_sum(array_map('array_sum', $sharpenMatrix));
  162. $offset = 0;
  163. // apply the matrix
  164. imageconvolution($img, $sharpenMatrix, $divisor, $offset);
  165. *
  166. * private function - do not call
  167. * includes function ImageCreateTrueColor and ImageCopyResampled which are available only under GD 2.0.1 or higher !
  168. */
  169. private function NewImgResize($orig_img,$newxsize,$newysize,$filename)
  170. {
  171. //getimagesize returns array
  172. // [0] = width in pixels
  173. // [1] = height in pixels
  174. // [2] = type
  175. // [3] = img tag "width=xx height=xx" values
  176. $orig_size = getimagesize($filename);
  177. $maxX = $newxsize;
  178. $maxY = $newysize;
  179. if ($orig_size[0]<$orig_size[1])
  180. {
  181. $newxsize = $newysize * ($orig_size[0]/$orig_size[1]);
  182. $adjustX = ($maxX - $newxsize)/2;
  183. $adjustY = 0;
  184. }
  185. else
  186. {
  187. $newysize = $newxsize / ($orig_size[0]/$orig_size[1]);
  188. $adjustX = 0;
  189. $adjustY = ($maxY - $newysize)/2;
  190. }
  191. /* Original code removed to allow for maxSize thumbnails
  192. $im_out = ImageCreateTrueColor($newxsize,$newysize);
  193. ImageCopyResampled($im_out, $orig_img, 0, 0, 0, 0,
  194. $newxsize, $newysize,$orig_size[0], $orig_size[1]);
  195. */
  196. // New modification - creates new image at maxSize
  197. if( $this->maxSize )
  198. {
  199. if( function_exists("imagecreatetruecolor") )
  200. $im_out = imagecreatetruecolor($maxX,$maxY);
  201. else
  202. $im_out = imagecreate($maxX,$maxY);
  203. // Need to image fill just in case image is transparent, don't always want black background
  204. $bgfill = imagecolorallocate( $im_out, $this->bg_red, $this->bg_green, $this->bg_blue );
  205. if( function_exists( "imageAntiAlias" )) {
  206. imageAntiAlias($im_out,true);
  207. }
  208. imagealphablending($im_out, false);
  209. if( function_exists( "imagesavealpha")) {
  210. imagesavealpha($im_out,true);
  211. }
  212. if( function_exists( "imagecolorallocatealpha")) {
  213. $transparent = imagecolorallocatealpha($im_out, 255, 255, 255, 127);
  214. }
  215. //imagefill( $im_out, 0,0, $bgfill );
  216. if( function_exists("imagecopyresampled") ){
  217. ImageCopyResampled($im_out, $orig_img, $adjustX, $adjustY, 0, 0, $newxsize, $newysize,$orig_size[0], $orig_size[1]);
  218. }
  219. else {
  220. ImageCopyResized($im_out, $orig_img, $adjustX, $adjustY, 0, 0, $newxsize, $newysize,$orig_size[0], $orig_size[1]);
  221. }
  222. }
  223. else
  224. {
  225. if( function_exists("imagecreatetruecolor") )
  226. $im_out = ImageCreateTrueColor($newxsize,$newysize);
  227. else
  228. $im_out = imagecreate($newxsize,$newysize);
  229. if( function_exists( "imageAntiAlias" ))
  230. imageAntiAlias($im_out,true);
  231. imagealphablending($im_out, false);
  232. if( function_exists( "imagesavealpha"))
  233. imagesavealpha($im_out,true);
  234. if( function_exists( "imagecolorallocatealpha"))
  235. $transparent = imagecolorallocatealpha($im_out, 255, 255, 255, 127);
  236. if( function_exists("imagecopyresampled") )
  237. ImageCopyResampled($im_out, $orig_img, 0, 0, 0, 0, $newxsize, $newysize,$orig_size[0], $orig_size[1]);
  238. else
  239. ImageCopyResized($im_out, $orig_img, 0, 0, 0, 0, $newxsize, $newysize,$orig_size[0], $orig_size[1]);
  240. }
  241. return $im_out;
  242. }
  243. /**
  244. *
  245. * private function - do not call
  246. *
  247. */
  248. private function NewImgSave($new_img,$fileout,$type)
  249. {
  250. if( !@is_dir( dirname($fileout))) {
  251. @mkdir( dirname($fileout) );
  252. }
  253. switch($type)
  254. {
  255. case "gif":
  256. if( !function_exists("imagegif") )
  257. {
  258. if (strtolower(substr($fileout,strlen($fileout)-4,4))!=".gif") {
  259. $fileout .= ".png";
  260. }
  261. return imagepng($new_img,$fileout);
  262. }
  263. else {
  264. if (strtolower(substr($fileout,strlen($fileout)-4,4))!=".gif") {
  265. $fileout .= '.gif';
  266. }
  267. return imagegif( $new_img, $fileout );
  268. }
  269. break;
  270. case "jpg":
  271. if (strtolower(substr($fileout,strlen($fileout)-4,4))!=".jpg")
  272. $fileout .= ".jpg";
  273. return imagejpeg($new_img, $fileout, 100);
  274. break;
  275. case "png":
  276. if (strtolower(substr($fileout,strlen($fileout)-4,4))!=".png")
  277. $fileout .= ".png";
  278. return imagepng($new_img,$fileout);
  279. break;
  280. }
  281. }
  282. /**
  283. *
  284. * private function - do not call
  285. *
  286. */
  287. private function NewImgShow($new_img,$type)
  288. {
  289. /* Original code removed in favor of 'switch' statement
  290. if ($type=="png")
  291. {
  292. header ("Content-type: image/png");
  293. return imagepng($new_img);
  294. }
  295. if ($type=="jpg")
  296. {
  297. header ("Content-type: image/jpeg");
  298. return imagejpeg($new_img);
  299. }
  300. */
  301. switch($type)
  302. {
  303. case "gif":
  304. if( function_exists("imagegif") )
  305. {
  306. header ("Content-type: image/gif");
  307. return imagegif($new_img);
  308. break;
  309. }
  310. else
  311. $this->NewImgShow( $new_img, "jpg" );
  312. case "jpg":
  313. header ("Content-type: image/jpeg");
  314. return imagejpeg($new_img);
  315. break;
  316. case "png":
  317. header ("Content-type: image/png");
  318. return imagepng($new_img);
  319. break;
  320. }
  321. }
  322. /**
  323. *
  324. * private function - do not call
  325. *
  326. * 1 = GIF, 2 = JPG, 3 = PNG, 4 = SWF,
  327. * 5 = PSD, 6 = BMP,
  328. * 7 = TIFF(intel byte order),
  329. * 8 = TIFF(motorola byte order),
  330. * 9 = JPC, 10 = JP2, 11 = JPX,
  331. * 12 = JB2, 13 = SWC, 14 = IFF
  332. */
  333. private function GetImgType($filename)
  334. {
  335. $info = getimagesize($filename);
  336. /* Original code removed in favor of 'switch' statement
  337. if($size[2]==2)
  338. return "jpg";
  339. elseif($size[2]==3)
  340. return "png";
  341. */
  342. switch($info[2]) {
  343. case 1:
  344. return "gif";
  345. break;
  346. case 2:
  347. return "jpg";
  348. break;
  349. case 3:
  350. return "png";
  351. break;
  352. default:
  353. return false;
  354. }
  355. }
  356. }