PageRenderTime 57ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/Quản lý website bán vé máy bay PHP/ckfinder/core/connector/php/php5/CommandHandler/Thumbnail.php

https://gitlab.com/phamngsinh/baitaplon_sinhvien
PHP | 333 lines | 206 code | 43 blank | 84 comment | 60 complexity | e1f79f38094c18769fd85360a2cb7627 MD5 | raw file
  1. <?php
  2. /*
  3. * CKFinder
  4. * ========
  5. * http://ckfinder.com
  6. * Copyright (C) 2007-2012, CKSource - Frederico Knabben. All rights reserved.
  7. *
  8. * The software, this file and its contents are subject to the CKFinder
  9. * License. Please read the license.txt file before using, installing, copying,
  10. * modifying or distribute this file or part of its contents. The contents of
  11. * this file is part of the Source Code of CKFinder.
  12. */
  13. if (!defined('IN_CKFINDER')) exit;
  14. /**
  15. * @package CKFinder
  16. * @subpackage CommandHandlers
  17. * @copyright CKSource - Frederico Knabben
  18. */
  19. /**
  20. * Handle Thumbnail command (create thumbnail if doesn't exist)
  21. *
  22. * @package CKFinder
  23. * @subpackage CommandHandlers
  24. * @copyright CKSource - Frederico Knabben
  25. */
  26. class CKFinder_Connector_CommandHandler_Thumbnail extends CKFinder_Connector_CommandHandler_CommandHandlerBase
  27. {
  28. /**
  29. * Command name
  30. *
  31. * @access private
  32. * @var string
  33. */
  34. private $command = "Thumbnail";
  35. /**
  36. * handle request and send response
  37. * @access public
  38. *
  39. */
  40. public function sendResponse()
  41. {
  42. // Get rid of BOM markers
  43. if (ob_get_level()) {
  44. while (@ob_end_clean() && ob_get_level());
  45. }
  46. header("Content-Encoding: none");
  47. $this->checkConnector();
  48. $this->checkRequest();
  49. $_config =& CKFinder_Connector_Core_Factory::getInstance("Core_Config");
  50. $_thumbnails = $_config->getThumbnailsConfig();
  51. if (!$_thumbnails->getIsEnabled()) {
  52. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_THUMBNAILS_DISABLED);
  53. }
  54. if (!$this->_currentFolder->checkAcl(CKFINDER_CONNECTOR_ACL_FILE_VIEW)) {
  55. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_UNAUTHORIZED);
  56. }
  57. if (!isset($_GET["FileName"])) {
  58. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_REQUEST);
  59. }
  60. $fileName = CKFinder_Connector_Utils_FileSystem::convertToFilesystemEncoding($_GET["FileName"]);
  61. $_resourceTypeInfo = $this->_currentFolder->getResourceTypeConfig();
  62. if (!CKFinder_Connector_Utils_FileSystem::checkFileName($fileName)) {
  63. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_INVALID_REQUEST);
  64. }
  65. $sourceFilePath = CKFinder_Connector_Utils_FileSystem::combinePaths($this->_currentFolder->getServerPath(), $fileName);
  66. if ($_resourceTypeInfo->checkIsHiddenFile($fileName) || !file_exists($sourceFilePath)) {
  67. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_FILE_NOT_FOUND);
  68. }
  69. $thumbFilePath = CKFinder_Connector_Utils_FileSystem::combinePaths($this->_currentFolder->getThumbsServerPath(), $fileName);
  70. // If the thumbnail file doesn't exists, create it now.
  71. if (!file_exists($thumbFilePath)) {
  72. if(!$this->createThumb($sourceFilePath, $thumbFilePath, $_thumbnails->getMaxWidth(), $_thumbnails->getMaxHeight(), $_thumbnails->getQuality(), true, $_thumbnails->getBmpSupported())) {
  73. $this->_errorHandler->throwError(CKFINDER_CONNECTOR_ERROR_ACCESS_DENIED);
  74. }
  75. }
  76. $size = filesize($thumbFilePath);
  77. $sourceImageAttr = getimagesize($thumbFilePath);
  78. $mime = $sourceImageAttr["mime"];
  79. $rtime = isset($_SERVER["HTTP_IF_MODIFIED_SINCE"])?@strtotime($_SERVER["HTTP_IF_MODIFIED_SINCE"]):0;
  80. $mtime = filemtime($thumbFilePath);
  81. $etag = dechex($mtime) . "-" . dechex($size);
  82. $is304 = false;
  83. if (isset($_SERVER["HTTP_IF_NONE_MATCH"]) && $_SERVER["HTTP_IF_NONE_MATCH"] === $etag) {
  84. $is304 = true;
  85. }
  86. else if($rtime == $mtime) {
  87. $is304 = true;
  88. }
  89. if ($is304) {
  90. header("HTTP/1.0 304 Not Modified");
  91. exit();
  92. }
  93. //header("Cache-Control: cache, must-revalidate");
  94. //header("Pragma: public");
  95. //header("Expires: 0");
  96. header('Cache-control: public');
  97. header('Etag: ' . $etag);
  98. header("Content-type: " . $mime . "; name=\"" . CKFinder_Connector_Utils_Misc::mbBasename($thumbFilePath) . "\"");
  99. header("Last-Modified: ".gmdate('D, d M Y H:i:s', $mtime) . " GMT");
  100. //header("Content-type: application/octet-stream; name=\"{$file}\"");
  101. //header("Content-Disposition: attachment; filename=\"{$file}\"");
  102. header("Content-Length: ".$size);
  103. readfile($thumbFilePath);
  104. exit;
  105. }
  106. /**
  107. * Create thumbnail
  108. *
  109. * @param string $sourceFile
  110. * @param string $targetFile
  111. * @param int $maxWidth
  112. * @param int $maxHeight
  113. * @param boolean $preserverAspectRatio
  114. * @param boolean $bmpSupported
  115. * @return boolean
  116. * @static
  117. * @access public
  118. */
  119. public static function createThumb($sourceFile, $targetFile, $maxWidth, $maxHeight, $quality, $preserverAspectRatio, $bmpSupported = false)
  120. {
  121. $sourceImageAttr = @getimagesize($sourceFile);
  122. if ($sourceImageAttr === false) {
  123. return false;
  124. }
  125. $sourceImageWidth = isset($sourceImageAttr[0]) ? $sourceImageAttr[0] : 0;
  126. $sourceImageHeight = isset($sourceImageAttr[1]) ? $sourceImageAttr[1] : 0;
  127. $sourceImageMime = isset($sourceImageAttr["mime"]) ? $sourceImageAttr["mime"] : "";
  128. $sourceImageBits = isset($sourceImageAttr["bits"]) ? $sourceImageAttr["bits"] : 8;
  129. $sourceImageChannels = isset($sourceImageAttr["channels"]) ? $sourceImageAttr["channels"] : 3;
  130. if (!$sourceImageWidth || !$sourceImageHeight || !$sourceImageMime) {
  131. return false;
  132. }
  133. $iFinalWidth = $maxWidth == 0 ? $sourceImageWidth : $maxWidth;
  134. $iFinalHeight = $maxHeight == 0 ? $sourceImageHeight : $maxHeight;
  135. if ($sourceImageWidth <= $iFinalWidth && $sourceImageHeight <= $iFinalHeight) {
  136. if ($sourceFile != $targetFile) {
  137. copy($sourceFile, $targetFile);
  138. }
  139. return true;
  140. }
  141. if ($preserverAspectRatio)
  142. {
  143. // Gets the best size for aspect ratio resampling
  144. $oSize = CKFinder_Connector_CommandHandler_Thumbnail::GetAspectRatioSize($iFinalWidth, $iFinalHeight, $sourceImageWidth, $sourceImageHeight );
  145. }
  146. else {
  147. $oSize = array('Width' => $iFinalWidth, 'Height' => $iFinalHeight);
  148. }
  149. CKFinder_Connector_Utils_Misc::setMemoryForImage($sourceImageWidth, $sourceImageHeight, $sourceImageBits, $sourceImageChannels);
  150. switch ($sourceImageAttr['mime'])
  151. {
  152. case 'image/gif':
  153. {
  154. if (@imagetypes() & IMG_GIF) {
  155. $oImage = @imagecreatefromgif($sourceFile);
  156. } else {
  157. $ermsg = 'GIF images are not supported';
  158. }
  159. }
  160. break;
  161. case 'image/jpeg':
  162. {
  163. if (@imagetypes() & IMG_JPG) {
  164. $oImage = @imagecreatefromjpeg($sourceFile) ;
  165. } else {
  166. $ermsg = 'JPEG images are not supported';
  167. }
  168. }
  169. break;
  170. case 'image/png':
  171. {
  172. if (@imagetypes() & IMG_PNG) {
  173. $oImage = @imagecreatefrompng($sourceFile) ;
  174. } else {
  175. $ermsg = 'PNG images are not supported';
  176. }
  177. }
  178. break;
  179. case 'image/wbmp':
  180. {
  181. if (@imagetypes() & IMG_WBMP) {
  182. $oImage = @imagecreatefromwbmp($sourceFile);
  183. } else {
  184. $ermsg = 'WBMP images are not supported';
  185. }
  186. }
  187. break;
  188. case 'image/bmp':
  189. {
  190. /*
  191. * This is sad that PHP doesn't support bitmaps.
  192. * Anyway, we will use our custom function at least to display thumbnails.
  193. * We'll not resize images this way (if $sourceFile === $targetFile),
  194. * because user defined imagecreatefrombmp and imagecreatebmp are horribly slow
  195. */
  196. if ($bmpSupported && (@imagetypes() & IMG_JPG) && $sourceFile != $targetFile) {
  197. $oImage = CKFinder_Connector_Utils_Misc::imageCreateFromBmp($sourceFile);
  198. } else {
  199. $ermsg = 'BMP/JPG images are not supported';
  200. }
  201. }
  202. break;
  203. default:
  204. $ermsg = $sourceImageAttr['mime'].' images are not supported';
  205. break;
  206. }
  207. if (isset($ermsg) || false === $oImage) {
  208. return false;
  209. }
  210. $oThumbImage = imagecreatetruecolor($oSize["Width"], $oSize["Height"]);
  211. if ($sourceImageAttr['mime'] == 'image/png')
  212. {
  213. $bg = imagecolorallocatealpha($oThumbImage, 255, 255, 255, 127); // (PHP 4 >= 4.3.2, PHP 5)
  214. imagefill($oThumbImage, 0, 0 , $bg);
  215. imagealphablending($oThumbImage, false);
  216. imagesavealpha($oThumbImage, true);
  217. }
  218. //imagecopyresampled($oThumbImage, $oImage, 0, 0, 0, 0, $oSize["Width"], $oSize["Height"], $sourceImageWidth, $sourceImageHeight);
  219. CKFinder_Connector_Utils_Misc::fastImageCopyResampled($oThumbImage, $oImage, 0, 0, 0, 0, $oSize["Width"], $oSize["Height"], $sourceImageWidth, $sourceImageHeight, (int)max(floor($quality/20), 6));
  220. switch ($sourceImageAttr['mime'])
  221. {
  222. case 'image/gif':
  223. imagegif($oThumbImage, $targetFile);
  224. break;
  225. case 'image/jpeg':
  226. case 'image/bmp':
  227. imagejpeg($oThumbImage, $targetFile, $quality);
  228. break;
  229. case 'image/png':
  230. imagepng($oThumbImage, $targetFile);
  231. break;
  232. case 'image/wbmp':
  233. imagewbmp($oThumbImage, $targetFile);
  234. break;
  235. }
  236. $_config =& CKFinder_Connector_Core_Factory::getInstance("Core_Config");
  237. if (file_exists($targetFile) && ($perms = $_config->getChmodFiles())) {
  238. $oldUmask = umask(0);
  239. chmod($targetFile, $perms);
  240. umask($oldUmask);
  241. }
  242. imageDestroy($oImage);
  243. imageDestroy($oThumbImage);
  244. return true;
  245. }
  246. /**
  247. * Return aspect ratio size, returns associative array:
  248. * <pre>
  249. * Array
  250. * (
  251. * [Width] => 80
  252. * [Heigth] => 120
  253. * )
  254. * </pre>
  255. *
  256. * @param int $maxWidth
  257. * @param int $maxHeight
  258. * @param int $actualWidth
  259. * @param int $actualHeight
  260. * @return array
  261. * @static
  262. * @access public
  263. */
  264. public static function getAspectRatioSize($maxWidth, $maxHeight, $actualWidth, $actualHeight)
  265. {
  266. $oSize = array("Width"=>$maxWidth, "Height"=>$maxHeight);
  267. // Calculates the X and Y resize factors
  268. $iFactorX = (float)$maxWidth / (float)$actualWidth;
  269. $iFactorY = (float)$maxHeight / (float)$actualHeight;
  270. // If some dimension have to be scaled
  271. if ($iFactorX != 1 || $iFactorY != 1)
  272. {
  273. // Uses the lower Factor to scale the oposite size
  274. if ($iFactorX < $iFactorY) {
  275. $oSize["Height"] = (int)round($actualHeight * $iFactorX);
  276. }
  277. else if ($iFactorX > $iFactorY) {
  278. $oSize["Width"] = (int)round($actualWidth * $iFactorY);
  279. }
  280. }
  281. if ($oSize["Height"] <= 0) {
  282. $oSize["Height"] = 1;
  283. }
  284. if ($oSize["Width"] <= 0) {
  285. $oSize["Width"] = 1;
  286. }
  287. // Returns the Size
  288. return $oSize;
  289. }
  290. }