PageRenderTime 47ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/system/application/views/admin/static/js/ckeditor/ckfinder/ckfinder/core/connector/php/php4/CommandHandler/Thumbnail.php

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