PageRenderTime 49ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 1ms

/system/modules/Control/Img2Thumb.php

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