PageRenderTime 26ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/htroot/manager/media/browser/mcpuk/connectors/php/Commands/Thumbnail.php

https://github.com/gunf/novo-isaak.local
PHP | 239 lines | 174 code | 36 blank | 29 comment | 39 complexity | cadbbedbb1fe08c421f8c5ae6529a863 MD5 | raw file
  1. <?php
  2. /*
  3. * FCKeditor - The text editor for internet
  4. * Copyright (C) 2003-2005 Frederico Caldeira Knabben
  5. *
  6. * Licensed under the terms of the GNU Lesser General Public License:
  7. * http://www.opensource.org/licenses/lgpl-license.php
  8. *
  9. * For further information visit:
  10. * http://www.fckeditor.net/
  11. *
  12. * File Name: Thumbnail.php
  13. * Implements the Thumbnail command, to return
  14. * a thumbnail to the browser for the sent file,
  15. * if the file is an image an attempt is made to
  16. * generate a thumbnail, otherwise an appropriate
  17. * icon is returned.
  18. * Output is image data
  19. *
  20. * File Authors:
  21. * Grant French (grant@mcpuk.net)
  22. */
  23. include MODX_BASE_PATH."manager/media/browser/mcpuk/connectors/php/Commands/helpers/iconlookup.php";
  24. class Thumbnail {
  25. var $fckphp_config;
  26. var $type;
  27. var $cwd;
  28. var $actual_cwd;
  29. var $filename;
  30. function Thumbnail($fckphp_config,$type,$cwd) {
  31. $this->fckphp_config=$fckphp_config;
  32. $this->type=$type;
  33. $this->raw_cwd=$cwd;
  34. $this->actual_cwd=str_replace("//","/",($fckphp_config['UserFilesPath']."/$type/".$this->raw_cwd));
  35. $this->real_cwd=str_replace("//","/",($this->fckphp_config['basedir']."/".$this->actual_cwd));
  36. $this->filename=str_replace(array("..","/"),"",$_GET['FileName']);
  37. }
  38. function run() {
  39. //$mimeIcon=getMimeIcon($mime);
  40. $fullfile=$this->real_cwd.'/'.$this->filename;
  41. $thumbfile=$this->real_cwd.'/.thumb_'.$this->filename;
  42. $icon=false;
  43. if (file_exists($thumbfile)) {
  44. $icon=$thumbfile;
  45. } else {
  46. $mime=$this->getMIME($fullfile);
  47. $ext=strtolower($this->getExtension($this->filename));
  48. if ($this->isImage($mime,$ext))
  49. {
  50. //Try and find a thumbnail, else try to generate one
  51. // else send generic picture icon.
  52. if ($this->isJPEG($mime,$ext)) {
  53. $result=$this->resizeFromJPEG($fullfile);
  54. } elseif ($this->isGIF($mime,$ext)) {
  55. $result=$this->resizeFromGIF($fullfile);
  56. } elseif ($this->isPNG($mime,$ext)) {
  57. $result=$this->resizeFromPNG($fullfile);
  58. }
  59. if ($result!==false) {
  60. if (function_exists("imagejpeg")) {
  61. imagejpeg($result,$thumbfile,70);
  62. @chmod($thumbfile,$this->fckphp_config['modx']['file_permissions']); //modified for MODx
  63. $icon=$thumbfile;
  64. } elseif (function_exists("imagepng")) {
  65. imagepng($result,$thumbfile);
  66. @chmod($thumbfile,$this->fckphp_config['modx']['file_permissions']); //modified for MODx
  67. $icon=$thumbfile;
  68. } elseif (function_exists("imagegif")) {
  69. imagegif($result,$thumbfile);
  70. @chmod($thumbfile,$this->fckphp_config['modx']['file_permissions']); //modified for MODx
  71. $icon=$thumbfile;
  72. } else {
  73. $icon=iconLookup($mime,$ext);
  74. }
  75. } else {
  76. $icon=iconLookup($mime,$ext);
  77. }
  78. } else {
  79. $icon=iconLookup($mime,$ext);
  80. }
  81. }
  82. $iconMime=$this->image2MIME($icon);
  83. if ($iconMime==false) $iconMime="image/jpeg";
  84. header("Content-type: $iconMime",true);
  85. readfile($icon);
  86. }
  87. function getMIME($file) {
  88. //If mime magic is installed
  89. if (function_exists("mime_content_type")) {
  90. $mime=mime_content_type($file);
  91. } else {
  92. $mime=$this->image2MIME($file);
  93. if($mime==false) $mime="text/plain";
  94. }
  95. return strtolower($mime);
  96. }
  97. function image2MIME($file) {
  98. $fh=fopen($file,"r");
  99. if ($fh) {
  100. $start4=fread($fh,4);
  101. $start3=substr($start4,0,3);
  102. if ($start4=="\x89PNG") {
  103. return "image/png";
  104. } elseif ($start3=="GIF") {
  105. return "image/gif";
  106. } elseif ($start3=="\xFF\xD8\xFF") {
  107. return "image/jpeg";
  108. } elseif ($start4=="hsi1") {
  109. return "image/jpeg";
  110. } else {
  111. return false;
  112. }
  113. unset($start3);unset($start4);
  114. fclose($fh);
  115. } else {
  116. return false;
  117. }
  118. }
  119. function isImage($mime,$ext) {
  120. if (
  121. ($mime=="image/gif")||
  122. ($mime=="image/jpeg")||
  123. ($mime=="image/jpg")||
  124. ($mime=="image/pjpeg")||
  125. ($mime=="image/png")||
  126. ($ext=="jpg")||
  127. ($ext=="jpeg")||
  128. ($ext=="png")||
  129. ($ext=="gif") ) {
  130. return true;
  131. } else {
  132. return false;
  133. }
  134. }
  135. function isJPEG($mime,$ext) {
  136. if (($mime=="image/jpeg")||($mime=="image/jpg")||($mime=="image/pjpeg")||($ext=="jpg")||($ext=="jpeg")) {
  137. return true;
  138. } else {
  139. return false;
  140. }
  141. }
  142. function isGIF($mime,$ext) {
  143. if (($mime=="image/gif")||($ext=="gif")) {
  144. return true;
  145. } else {
  146. return false;
  147. }
  148. }
  149. function isPNG($mime,$ext) {
  150. if (($mime=="image/png")||($ext=="png")) {
  151. return true;
  152. } else {
  153. return false;
  154. }
  155. }
  156. function getExtension($filename) {
  157. //Get Extension
  158. $ext="";
  159. $lastpos=strrpos($this->filename,'.');
  160. if ($lastpos!==false) $ext=substr($this->filename,($lastpos+1));
  161. return strtolower($ext);
  162. }
  163. function resizeFromJPEG($file) {
  164. if (function_exists("imagecreatefromjpeg")) {
  165. $img=@imagecreatefromjpeg($this->real_cwd.'/'.$this->filename);
  166. return (($img)?$this->resizeImage($img):false);
  167. } else { return false; }
  168. }
  169. function resizeFromGIF($file) {
  170. if (function_exists("imagecreatefromgif")) {
  171. $img=@imagecreatefromgif($this->real_cwd.'/'.$this->filename);
  172. return (($img)?$this->resizeImage($img):false);
  173. } else { return false; }
  174. }
  175. function resizeFromPNG($file) {
  176. if (function_exists("imagecreatefrompng")) {
  177. $img=@imagecreatefrompng($this->real_cwd.'/'.$this->filename);
  178. return (($img)?$this->resizeImage($img):false);
  179. } else { return false; }
  180. }
  181. function resizeImage($img) {
  182. //Get size for thumbnail
  183. $width=imagesx($img); $height=imagesy($img);
  184. if ($width>$height) { $n_height=$height*(96/$width); $n_width=96; } else { $n_width=$width*(96/$height); $n_height=96; }
  185. $x=0;$y=0;
  186. if ($n_width<96) $x=round((96-$n_width)/2);
  187. if ($n_height<96) $y=round((96-$n_height)/2);
  188. $thumb=imagecreatetruecolor(96,96);
  189. #Background colour fix by:
  190. #Ben Lancaster (benlanc@ster.me.uk)
  191. $bgcolor = imagecolorallocate($thumb,255,255,255);
  192. imagefill($thumb, 0, 0, $bgcolor);
  193. if (function_exists("imagecopyresampled")) {
  194. if (!($result=@imagecopyresampled($thumb,$img,$x,$y,0,0,$n_width,$n_height,$width,$height))) {
  195. $result=imagecopyresized($thumb,$img,$x,$y,0,0,$n_width,$n_height,$width,$height);
  196. }
  197. } else {
  198. $result=imagecopyresized($thumb,$img,$x,$y,0,0,$n_width,$n_height,$width,$height);
  199. }
  200. return ($result)?$thumb:false;
  201. }
  202. }
  203. ?>