PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/www/image.php

http://myzapp.googlecode.com/
PHP | 347 lines | 250 code | 44 blank | 53 comment | 34 complexity | 0116c7d29dd138e96c25f4673a468457 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. include(join(DIRECTORY_SEPARATOR,array(dirname(__FILE__),'..','application','configs','env.php')));
  3. include(join(DIRECTORY_SEPARATOR,array(dirname(__FILE__),'..','library','phplib.php')));
  4. function getHeader($header)
  5. {
  6. if (empty($header)) {
  7. require_once 'Zend/Controller/Request/Exception.php';
  8. throw new Zend_Controller_Request_Exception('An HTTP header name is required');
  9. }
  10. // Try to get it from the $_SERVER array first
  11. $temp = 'HTTP_' . strtoupper(str_replace('-', '_', $header));
  12. if (isset($_SERVER[$temp])) {
  13. return $_SERVER[$temp];
  14. }
  15. // This seems to be the only way to get the Authorization header on
  16. // Apache
  17. if (function_exists('apache_request_headers')) {
  18. $headers = apache_request_headers();
  19. if (isset($headers[$header])) {
  20. return $headers[$header];
  21. }
  22. $header = strtolower($header);
  23. foreach ($headers as $key => $value) {
  24. if (strtolower($key) == $header) {
  25. return $value;
  26. }
  27. }
  28. }
  29. return false;
  30. }
  31. $config = include(join(DIRECTORY_SEPARATOR,array(dirname(__FILE__),'..','application','configs','application.php')));
  32. $secret = $config['secret_word'];
  33. if (preg_match('~^.*?/(?P<hash>[0-9a-f]{32})/(?P<uri>.*)$~', $_SERVER['REQUEST_URI'], $info))
  34. {
  35. if ($info['hash'] == md5($info['uri'].$secret))
  36. {
  37. if (getHeader('If-None-Match'))
  38. {
  39. header('HTTP/1.1 304 Not Modified', true, 304);
  40. exit();
  41. }
  42. $size = isset($_GET['size']) ? $_GET['size'] : '0x0';
  43. $sharp = isset($_GET['sharp']) ? $_GET['sharp'] : null;
  44. $crop = isset($_GET['crop']) ? $_GET['crop'] : 0;
  45. $logo = isset($_GET['logo']) ? $_GET['logo'] : null;
  46. $format = isset($_GET['format']) ? $_GET['format'] : 'jpeg';
  47. $quality = isset($_GET['quality']) ? $_GET['quality'] : 95;
  48. $filter = isset($_GET['filter']) ? $_GET['filter'] : array();
  49. header("ETag: \"get-cached-image\"",true);
  50. $uri = preg_replace('~\\?.*$~','',$info['uri']);
  51. $object = new ImageConverter();
  52. $object->actionShow($uri, $size, $sharp, $crop, $filter, $logo, $format, $quality);
  53. }
  54. else
  55. {
  56. header('HTTP/1.1 403 Forbidden', true, 403);
  57. echo '<h1> Error: 403 Forbidden </h1>';
  58. }
  59. }
  60. else
  61. {
  62. header('HTTP/1.1 404 Not Found', true, 404);
  63. echo '<h1> Error: 404 Not Found </h1>';
  64. }
  65. /**
  66. * ????? ??????????.
  67. */
  68. class CException extends Exception
  69. {
  70. }
  71. /**
  72. * ????? ??????????, ??? ???????? ??????????????? ? ???????? ???? HTTP ??????.
  73. */
  74. class CExceptionHttp extends CException
  75. {
  76. protected $_httpCodes = array(
  77. 404 => 'Not Found',
  78. 500 => 'Internal Server Error',
  79. );
  80. public function __construct($message, $code)
  81. {
  82. header('HTTP/1.1 '.$code.($text = ' '.isset($this->_httpCodes[$code]) ? $this->_httpCodes[$code] : 'Unknown'),true,$code);
  83. echo '<h1>Error:'.$text.'</h1>';
  84. parent::__construct($message, $code);
  85. }
  86. }
  87. /**
  88. * ????? ?????????????? ???????????.
  89. */
  90. class ImageConverter
  91. {
  92. /**
  93. * @var array ??????? ?????????? ???????? ???????????.
  94. */
  95. protected $_sharpMatrix = array(
  96. array(-1, -2, -1),
  97. array(-2, 24, -2),
  98. array(-1, -2, -1),
  99. );
  100. /**
  101. * @var integer ???????????? ??????? ????????.
  102. */
  103. protected $_sharpDiv = 12;
  104. /**
  105. * @var integer ???????? ????? ??? ?????????? ????????.
  106. */
  107. protected $_sharpOffset = 0;
  108. /**
  109. * ?????? ???????? ?? ??? URI.
  110. *
  111. * @param string $uri
  112. * @throws CException
  113. */
  114. protected function _loadUriContent($uri)
  115. {
  116. $uri = ltrim($uri,'/');
  117. if (file_exists($_SERVER['DOCUMENT_ROOT'].'/'.$uri))
  118. {
  119. return file_get_contents($_SERVER['DOCUMENT_ROOT'].'/'.$uri);
  120. }
  121. elseif (file_exists($uri))
  122. {
  123. return file_get_contents($uri);
  124. }
  125. throw new CExceptionHttp('File was not find on server.', 404);
  126. }
  127. /**
  128. * ??????????? ???????????, ???????????? ? ???????? ??????????.
  129. *
  130. * @param string $uri ???? ? ??????????? ???????????? DOCUMENT_ROOT.
  131. * @param string $size OPTIONAL ??????? ???????, ? ??????? ??????????? ???????????.
  132. * @param integer $sharp OPTIONAL ????????? ???????? ? ??????? ?????? ???? ????????.
  133. * @param boolean $crop OPTIONAL ???? ????????? ????? ??????????? ??? ??? ??????? ???????? ? ???????? ???????.
  134. * @param array $filter OPTIONAL ?????? ??????????? ????????.
  135. * @param array $logo OPTIONAL ???? ? ?????????, ??????? ????? ????????? ?? ???????????.
  136. * @param string $format OPTIONAL ?????? ????????? ???????????.
  137. * @param integer $quality OPTIONAL ??????? ???????? JPEG ???????????.
  138. */
  139. public function actionShow($uri, $size = null, $sharp = null, $crop = null, $filter = null, $logo = null, $format = 'jpeg', $quality = 90)
  140. {
  141. // ?????? ????? ??????????? ? ??????.
  142. $source = $this->_loadUriContent($uri);
  143. // ???????? ??????? ????????? ??????????? ? ????????? ??? ??????????.
  144. if (!$source = @imagecreatefromstring($source))
  145. {
  146. throw new CExceptionHttp('Wrong image file at '.$uri, 500);
  147. }
  148. $srcWidth = $targetWidth = imagesx($source);
  149. $srcHeight = $targetHeight = imagesy($source);
  150. $target = $coofOriginal = 0;
  151. // ?????? ? ????????? ??????????.
  152. list($width, $height) = explode('x', isset($size) ? $size : '0x0', 2);
  153. $width = empty($width) ? $srcWidth : (int)$width;
  154. $height = empty($height) ? $srcHeight : (int)$height;
  155. $crop = empty($crop) ? 0 : (int)$crop;
  156. $sharp = isset($sharp) ? max(0, (int)$sharp) : 20;
  157. // ?????? ????? ???????? ???????????.
  158. while (TRUE):
  159. $coof = $crop ? max($width / $srcWidth, $height / $srcHeight) : min($width / $srcWidth, $height / $srcHeight);
  160. empty($coofOriginal) && $coofOriginal = $coof;
  161. $targetWidth = $crop ? $width : round($srcWidth * $coof);
  162. $targetHeight = $crop ? $height : round($srcHeight * $coof);
  163. $copyWidth = $crop ? min(round($targetWidth / $coof), $srcWidth) : $srcWidth;
  164. $copyHeight = $crop ? min(round($targetHeight / $coof), $srcHeight) : $srcHeight;
  165. $deltaCoof = array(0, 1, 0.32, 0, 1.68, 2);
  166. $deltaX = $crop ? round(($srcWidth - $copyWidth) / 2 * $deltaCoof[$crop]) : 0;
  167. $deltaY = $crop ? round(($srcHeight - $copyHeight) / 2 * $deltaCoof[$crop]) : 0;
  168. // ????????? ???????? ???????????.
  169. if ($targetWidth * 2 < $srcWidth)
  170. {
  171. $target = imagecreatetruecolor($srcWidth >> 1, $srcHeight >> 1);
  172. imagecopyresampled($target, $source, 0, 0, 0, 0, $srcWidth >> 1, $srcHeight >> 1, $srcWidth, $srcHeight);
  173. imagedestroy($source);
  174. $source = $target;
  175. $srcWidth = imagesx($source);
  176. $srcHeight = imagesy($source);
  177. continue;
  178. }
  179. else
  180. {
  181. $target = imagecreatetruecolor($targetWidth, $targetHeight);
  182. imagecopyresampled($target, $source, 0, 0, $deltaX, $deltaY, $targetWidth, $targetHeight, $copyWidth, $copyHeight);
  183. break;
  184. }
  185. endwhile;
  186. // ????????? ???????? ???????????.
  187. if ($coofOriginal < 1 && $coofOriginal > 0 && $sharp > 0)
  188. {
  189. $matrix = $this->_sharpMatrix;
  190. $matrix[1][1] += round($sharp * $coofOriginal);
  191. //imageconvolution($target, $matrix, $matrix[1][1] - $this->_sharpDiv, $this->_sharpOffset);
  192. }
  193. // ?????????? ????????.
  194. foreach ((array)$filter as $name)
  195. {
  196. if (function_exists('imagefilter'))
  197. {
  198. switch ($name)
  199. {
  200. case 'grayscale':
  201. imagefilter($target, IMG_FILTER_GRAYSCALE);
  202. break;
  203. }
  204. }
  205. }
  206. // ????????? ?????????.
  207. if (isset($logo))
  208. {
  209. foreach ((array)$logo as $logotype)
  210. {
  211. list($type, $uri) = explode(',', $logotype, 2);
  212. // ???????? ??????????? ????????.
  213. try
  214. {
  215. $logosource = $this->_loadUriContent($uri);
  216. }
  217. catch (CException $exception)
  218. {
  219. continue;
  220. }
  221. if (!$logosource = @imagecreatefromstring($logosource))
  222. {
  223. continue;
  224. }
  225. // ????????? ???????? ???????? ? ???????? ????, ??? ??????? ??????????? ? ???????????.
  226. $logoWidth = imagesx($logosource);
  227. $logoHeight = imagesy($logosource);
  228. if ($logoWidth > $targetWidth || $logoHeight > $targetHeight)
  229. {
  230. continue;
  231. }
  232. // ????????? ???????? ? ????????? ???????.
  233. if ($type == 0)
  234. {
  235. $widthCnt = floor($targetWidth / $logoWidth);
  236. $heightCnt = floor($targetHeight / $logoHeight);
  237. if ($widthCnt > 1 && $heightCnt > 1)
  238. {
  239. for ($y = 0; $y < $heightCnt; $y++)
  240. {
  241. for ($x = 0; $x < $widthCnt; $x++)
  242. {
  243. if ($x % 2 !== $y % 2)
  244. {
  245. continue;
  246. }
  247. $dst_x = round(($x * 2 + 1) * ($targetWidth / ($widthCnt * 2)) - $logoWidth / 2);
  248. $dst_y = round(($y * 2 + 1) * ($targetHeight / ($heightCnt * 2)) - $logoHeight / 2);
  249. imagecopy($target, $logosource, $dst_x, $dst_y, 0, 0, $logoWidth, $logoHeight);
  250. }
  251. }
  252. continue;
  253. }
  254. $type = 1;
  255. }
  256. // ????? ?? ??????? ???????????? ????????.
  257. $marginCoof = 0.05;
  258. $dst_x = $dst_y = 0;
  259. switch ($type)
  260. {
  261. case 1: // center
  262. case 8: // top
  263. case 9: // bottom
  264. $dst_x = round(($targetWidth - $logoWidth) / 2);
  265. break;
  266. case 2: // left-top
  267. case 3: // left-bottom
  268. case 6: // left
  269. $dst_x = round(0 + ($targetWidth - $logoWidth) * $marginCoof);
  270. break;
  271. case 4: // right-top
  272. case 5: // right-bottom
  273. case 7: // right
  274. $dst_x = round($targetWidth - $logoWidth - ($targetWidth - $logoWidth) * $marginCoof);
  275. break;
  276. }
  277. switch ($type)
  278. {
  279. case 1: // center
  280. case 6: // left
  281. case 7: // right
  282. $dst_y = round(($targetHeight - $logoHeight) / 2);
  283. break;
  284. case 2: // left-top
  285. case 4: // right-top
  286. case 8: // top
  287. $dst_y = round(0 + ($targetHeight - $logoHeight) * $marginCoof);
  288. break;
  289. case 3: // left-bottom
  290. case 5: // right-bottom
  291. case 9: // bottom
  292. $dst_y = round($targetHeight - $logoHeight - ($targetHeight - $logoHeight) * $marginCoof);
  293. break;
  294. }
  295. imagecopy($target, $logosource, $dst_x, $dst_y, 0, 0, $logoWidth, $logoHeight);
  296. }
  297. }
  298. // ?????????? ? ????? ???????????.
  299. header('Content-Type: image/'.$format, true);
  300. $format == 'jpeg' ? imagejpeg($target, null, $quality) : imagepng($target);
  301. }
  302. }
  303. ?>