PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/GetStockImage.php

http://kwamoja.codeplex.com
PHP | 351 lines | 298 code | 16 blank | 37 comment | 85 complexity | c28e9d04be1157920f4c03bd0e3b1c28 MD5 | raw file
Possible License(s): LGPL-3.0, AGPL-1.0, GPL-2.0, LGPL-2.1
  1. <?php
  2. /* $Id$*/
  3. include ('includes/session.inc');
  4. /*
  5. http://127.0.0.1/~brink/KwaMoja/GetStockImage.php
  6. ?automake=1&width=81&height=74&stockid=&textcolor=FFFFF0&bevel=3&text=aa&bgcolor=007F00
  7. automake - if specified allows autocreate images
  8. stockid - if not specified it produces a blank image if set to empty string uses default stock image
  9. bgcolor - Background color specified in hex
  10. textcolor - Forground color specified in hex
  11. transcolor - Transparent color specified in hex
  12. width - if specified scales image to width
  13. height - if specified scales image to height
  14. transparent - if specfied uses bgcolor as transparent unless specified
  15. text - if specified override stockid to be printed on image
  16. bevel - if specified draws a drop down bevel
  17. */
  18. // Color decode function
  19. function DecodeBgColor( $ColourStr ) {
  20. if ( $ColourStr[0] == '#' ) {
  21. $ColourStr = mb_substr($ColourStr,1,mb_strlen($ColourStr));
  22. }
  23. $Red = 0;
  24. if(mb_strlen($ColourStr) > 1) {
  25. $Red = hexdec(mb_substr($ColourStr,0,2));
  26. $ColourStr = mb_substr($ColourStr,2,mb_strlen($ColourStr));
  27. }
  28. $Green = 0;
  29. if(mb_strlen($ColourStr) > 1) {
  30. $Green = hexdec(mb_substr($ColourStr,0,2));
  31. $ColourStr = mb_substr($ColourStr,2,mb_strlen($ColourStr));
  32. }
  33. $Blue = 0;
  34. if(mb_strlen($ColourStr) > 1) {
  35. $Blue = hexdec(mb_substr($ColourStr,0,2));
  36. $ColourStr = mb_substr($ColourStr,2,mb_strlen($ColourStr));
  37. }
  38. if(mb_strlen($ColourStr) > 1) {
  39. $Alpha = hexdec(mb_substr($ColourStr,0,2));
  40. $ColourStr = mb_substr($ColourStr,2,mb_strlen($ColourStr));
  41. }
  42. if ( isset($Alpha) )
  43. return array('red' => $Red, 'green' => $Green, 'blue' => $Blue, 'alpha' => $Alpha );
  44. else
  45. return array('red' => $Red, 'green' => $Green, 'blue' => $Blue );
  46. }
  47. if (!function_exists('imagecreatefrompng')){
  48. $Title = _('Image Manipulation Script Problem');
  49. include('includes/header.inc');
  50. prnMsg(_('This script requires the gd image functions to be available to php - this needs to be enabled in your server php version before this script can be used'),'error');
  51. include('includes/footer.inc');
  52. exit;
  53. }
  54. $DefaultImage = 'KwaMojasmall.png';
  55. $FilePath = $_SESSION['part_pics_dir'] . '/';
  56. $StockID = trim(mb_strtoupper($_GET['StockID']));
  57. if( isset($_GET['bgcolor']) )
  58. $BackgroundColour = $_GET['bgcolor'];
  59. if( isset($_GET['textcolor']) )
  60. $TextColour = $_GET['textcolor'];
  61. if( isset($_GET['width']) )
  62. $width = $_GET['width'];
  63. if( isset($_GET['height']) )
  64. $height = $_GET['height'];
  65. if( isset($_GET['scale']) )
  66. $scale = $_GET['scale'];
  67. if( isset($_GET['automake']) )
  68. $automake = $_GET['automake'];
  69. if( isset($_GET['transparent'])) {
  70. $doTrans = true;
  71. }
  72. if( isset($_GET['text']) ) {
  73. $text = $_GET['text'];
  74. }
  75. if( isset($_GET['transcolor'])) {
  76. $doTrans = true;
  77. $TranspColour = $_GET['transcolor'];
  78. } else {
  79. $doTrans = false;
  80. }
  81. if( isset($_GET['bevel']) ) {
  82. $bevel = $_GET['bevel'];
  83. } else {
  84. $bevel = false;
  85. }
  86. if( isset($_GET['fontsize']) ) {
  87. $fontsize = $_GET['fontsize'];
  88. } else {
  89. $fontsize = 3;
  90. }
  91. if( isset($_GET['notextbg']) ) {
  92. $notextbg = true;
  93. } else {
  94. $notextbg = false;
  95. }
  96. // Extension requirements and Stock ID Isolation
  97. if($StockID == '') {
  98. $StockID = $DefaultImage;
  99. $blanktext = true;
  100. }
  101. $i = strrpos($StockID,'.');
  102. if( $i === false )
  103. $type = 'png';
  104. else {
  105. $type = strtolower(mb_substr($StockID,$i+1,mb_strlen($StockID)));
  106. $StockID = mb_substr($StockID,0,$i);
  107. if($blanktext and !isset($text))
  108. $text = '';
  109. }
  110. $style = $type;
  111. $functype = $type;
  112. if ( $style == 'jpg' ) {
  113. $style = 'jpeg';
  114. $functype = 'jpeg';
  115. }
  116. $tmpFileName = $FilePath.$StockID;
  117. // First check for an image this is not the type requested
  118. if ( file_exists($tmpFileName.'.jpg') ) {
  119. $FileName = $StockID.'.jpg';
  120. $IsJpeg = true;
  121. } elseif (file_exists($tmpFileName.'.jpeg')) {
  122. $FileName = $StockID.'.jpeg';
  123. $IsJpeg = true;
  124. } elseif (file_exists($tmpFileName.'.png')) {
  125. $FileName = $StockID.'.png';
  126. $IsJpeg = false;
  127. } else {
  128. $FileName = $DefaultImage;
  129. $IsJpeg = $DefaultIsJpeg;
  130. }
  131. if( !$automake and !isset($FileName) ) {
  132. $Title = _('Stock Image Retrieval ....');
  133. include('includes/header.inc');
  134. prnMsg( _('The Image could not be retrieved because it does not exist'), 'error');
  135. echo '<br /><a href="' .$RootPath .'/index.php">'. _('Back to the menu'). '</a>';
  136. include('includes/footer.inc');
  137. exit;
  138. }
  139. // See if we need to automake this image
  140. if( $automake and !isset($FileName) ) {
  141. // Have we got height and width specs
  142. if( !isset($width) )
  143. $width = 64;
  144. if( !isset($height) )
  145. $height = 64;
  146. // Have we got a background color
  147. $im = imagecreate($width, $height);
  148. if( isset($BackgroundColour) )
  149. $BackgroundColour = DecodeBgColor( $BackgroundColour );
  150. else
  151. $BackgroundColour = DecodeBgColor( '#7F7F7F' );
  152. if( !isset($BackgroundColour['alpha']) ) {
  153. $ixbgcolor = imagecolorallocate($im,
  154. $BackgroundColour['red'],$BackgroundColour['green'],$BackgroundColour['blue']);
  155. } else {
  156. $ixbgcolor = imagecolorallocatealpha($im,
  157. $BackgroundColour['red'],$BackgroundColour['green'],$BackgroundColour['blue'],$BackgroundColour['alpha']);
  158. }
  159. // Have we got a text color
  160. if( isset($TextColour) )
  161. $TextColour = DecodeBgColor( $TextColour );
  162. else
  163. $TextColour = DecodeBgColor( '#000000' );
  164. if( !isset($TextColour['alpha']) ) {
  165. $ixtextcolor = imagecolorallocate($im,
  166. $TextColour['red'],$TextColour['green'],$TextColour['blue']);
  167. } else {
  168. $ixtextcolor = imagecolorallocatealpha($im,
  169. $TextColour['red'],$TextColour['green'],$TextColour['blue'],$TextColour['alpha']);
  170. }
  171. // Have we got transparency requirements
  172. if( isset($TranspColour) ) {
  173. $TranspColour = DecodeBgColor( $TranspColour );
  174. if( $TranspColour != $BackgroundColour ) {
  175. if( !isset($TextColour['alpha']) ) {
  176. $ixtranscolor = imagecolorallocate($im,
  177. $TranspColour['red'],$TranspColour['green'],$TranspColour['blue']);
  178. } else {
  179. $ixtranscolor = imagecolorallocatealpha($im,
  180. $TranspColour['red'],$TranspColour['green'],$TranspColour['blue'],$TranspColour['alpha']);
  181. }
  182. } else {
  183. $ixtranscolor = $ixbgcolor;
  184. }
  185. }
  186. imagefill($im, 0, 0, $ixbgcolor );
  187. if( $doTrans ) {
  188. imagecolortransparent($im, $ixtranscolor);
  189. }
  190. if(!isset($text))
  191. $text = $StockID;
  192. if(mb_strlen($text) > 0 ) {
  193. $fw = imagefontwidth($fontsize);
  194. $fh = imagefontheight($fontsize);
  195. $fy = (imagesy($im) - ($fh)) / 2;
  196. $fyh = $fy + $fh - 1;
  197. $textwidth = $fw * mb_strlen($text);
  198. $px = (imagesx($im) - $textwidth) / 2;
  199. if (!$notextbg)
  200. imagefilledrectangle($im,$px,$fy,imagesx($im)-($px+1),$fyh, $ixtextbgcolor );
  201. imagestring($im, $fontsize, $px, $fy, $text, $ixtextcolor);
  202. }
  203. } else {
  204. $tmpFileName = $FilePath.$FileName;
  205. if( $IsJpeg ) {
  206. $im = imagecreatefromjpeg($tmpFileName);
  207. } else {
  208. $im = imagecreatefrompng($tmpFileName);
  209. }
  210. // Have we got a background color
  211. if( isset($BackgroundColour) )
  212. $BackgroundColour = DecodeBgColor( $BackgroundColour );
  213. else
  214. $BackgroundColour = DecodeBgColor( '#7F7F7F' );
  215. if( !isset($BackgroundColour['alpha']) ) {
  216. $ixbgcolor = imagecolorallocate($im,
  217. $BackgroundColour['red'],$BackgroundColour['green'],$BackgroundColour['blue']);
  218. } else {
  219. $ixbgcolor = imagecolorallocatealpha($im,
  220. $BackgroundColour['red'],$BackgroundColour['green'],$BackgroundColour['blue'],$BackgroundColour['alpha']);
  221. }
  222. // Have we got a text color
  223. if( isset($TextColour) )
  224. $TextColour = DecodeBgColor( $TextColour );
  225. else
  226. $TextColour = DecodeBgColor( '#000000' );
  227. if( !isset($TextColour['alpha']) ) {
  228. $ixtextcolor = imagecolorallocate($im,
  229. $TextColour['red'],$TextColour['green'],$TextColour['blue']);
  230. } else {
  231. $ixtextcolor = imagecolorallocatealpha($im,
  232. $TextColour['red'],$TextColour['green'],$TextColour['blue'],$TextColour['alpha']);
  233. }
  234. $sw = imagesx($im);
  235. $sh = imagesy($im);
  236. if ( isset($width) and ($width != $sw) or isset($height) and ($height != $sh)) {
  237. if( !isset($width) )
  238. $width = imagesx($im);
  239. if( !isset($height) )
  240. $height = imagesy($im);
  241. $resize_scale = min($width/$sw, $height/$sh);
  242. if ($resize_scale < 1) {
  243. $resize_new_width = floor($resize_scale*$sw);
  244. $resize_new_height = floor($resize_scale*$sh);
  245. } else {
  246. $resize_new_width = $sw;
  247. $resize_new_height = $sh;
  248. }
  249. $tmpim = imagecreatetruecolor($resize_new_width, $resize_new_height);
  250. imagealphablending ( $tmpim, true);
  251. imagecopyresampled($tmpim,$im,0,0,0,0,$resize_new_width, $resize_new_height, $sw, $sh );
  252. imagedestroy($im);
  253. $im = $tmpim;
  254. unset($tmpim);
  255. if( !isset($BackgroundColour['alpha']) ) {
  256. $ixbgcolor = imagecolorallocate($im,
  257. $BackgroundColour['red'],$BackgroundColour['green'],$BackgroundColour['blue']);
  258. } else {
  259. $ixbgcolor = imagecolorallocatealpha($im,
  260. $BackgroundColour['red'],$BackgroundColour['green'],$BackgroundColour['blue'],$BackgroundColour['alpha']);
  261. }
  262. if( !isset($TextColour['alpha']) ) {
  263. $ixtextcolor = imagecolorallocate($im,
  264. $TextColour['red'],$TextColour['green'],$TextColour['blue']);
  265. } else {
  266. $ixtextcolor = imagecolorallocatealpha($im,
  267. $TextColour['red'],$TextColour['green'],$TextColour['blue'],$TextColour['alpha']);
  268. }
  269. //imagealphablending ( $im, false);
  270. }
  271. // Have we got transparency requirements
  272. if( isset($TranspColour) ) {
  273. $TranspColour = DecodeBgColor( $TranspColour );
  274. if( $TranspColour != $BackgroundColour ) {
  275. if( !isset($TextColour['alpha']) ) {
  276. $ixtranscolor = imagecolorallocate($im,
  277. $TranspColour['red'],$TranspColour['green'],$TranspColour['blue']);
  278. } else {
  279. $ixtranscolor = imagecolorallocatealpha($im,
  280. $TranspColour['red'],$TranspColour['green'],$TranspColour['blue'],$TranspColour['alpha']);
  281. }
  282. } else {
  283. $ixtranscolor = $ixbgcolor;
  284. }
  285. }
  286. if( $doTrans ) {
  287. imagecolortransparent($im, $ixtranscolor);
  288. }
  289. if( $doTrans )
  290. $ixtextbgcolor = $ixtranscolor;
  291. else
  292. $ixtextbgcolor = $ixbgcolor;
  293. // $ixtextbgcolor = imagecolorallocatealpha($im,
  294. // 0,0,0,0);
  295. if(!isset($text))
  296. $text = $StockID;
  297. if(mb_strlen($text) > 0 ) {
  298. $fw = imagefontwidth($fontsize);
  299. $fh = imagefontheight($fontsize);
  300. $fy = imagesy($im) - ($fh);
  301. $fyh = imagesy($im) - 1;
  302. $textwidth = $fw * mb_strlen($text);
  303. $px = (imagesx($im) - $textwidth) / 2;
  304. if (!$notextbg)
  305. imagefilledrectangle($im,$px,$fy,imagesx($im)-($px+1),$fyh, $ixtextbgcolor );
  306. imagestring($im, $fontsize, $px, $fy, $text, $ixtextcolor);
  307. }
  308. }
  309. // Do we need to bevel
  310. if( $bevel ) {
  311. $drgray = imagecolorallocate($im,63,63,63);
  312. $silver = imagecolorallocate($im,127,127,127);
  313. $white = imagecolorallocate($im,255,255,255);
  314. imageline($im, 0,0,imagesx($im)-1, 0, $drgray); // top
  315. imageline($im, 0,1,imagesx($im)-1, 1, $drgray); // top
  316. imageline($im, 1,0,1, imagesy($im)-1, $drgray); // left
  317. imageline($im, 0,0,0, imagesy($im)-1, $drgray); // left
  318. imageline($im, 0,imagesy($im)-1,imagesx($im)-1, imagesy($im)-1, $silver); // bottom
  319. imageline($im, imagesx($im)-1,0,imagesx($im)-1, imagesy($im)-1, $silver); // right
  320. }
  321. // Set up headers
  322. header('Content-Disposition: filename='.$StockID.'.'.$type);
  323. header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
  324. header('Pragma: public');
  325. header('Content-type: image/'.$style);
  326. // Which function should we use jpeg or png
  327. //images
  328. $func = 'image'.$functype;
  329. // AND send image
  330. $func($im);
  331. // Destroy image
  332. imagedestroy($im);
  333. ?>