rawky

Thumbnails.php - Magzimus

Jun 9th, 2011
403
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.98 KB | None | 0 0
  1. <?
  2. /**
  3. * Functions to handle and manipulate the thumbnails behaviour and display
  4. * Since 2.9, it serves mostly as a backup function for old posts that don't contain thumbnails
  5. * Exclusive to the Magzimus theme
  6. *
  7. * @package WordPress
  8. * @subpackage Magzimus
  9. **/
  10.  
  11.  
  12. ini_set('display_errors','1');
  13. ini_set('display_startup_errors','1');
  14. error_reporting(E_ALL ^ E_NOTICE);
  15.  
  16. class thumbnail {
  17.  
  18. function thumbnail($src, $width = 100, $height = 133, $position = 'left', $crop = 1, $quality = 80) {
  19.  
  20. $this->src = $src;
  21. $this->width = $width;
  22. $this->height = $height;
  23. $this->position = $position;
  24. $this->crop = $crop;
  25. $this->quality = $quality;
  26.  
  27. $this->doc_root = '';
  28. $this->mime_type = '';
  29. $this->thumb_src = '';
  30. $this->cache_dir = './cache';
  31.  
  32. }
  33.  
  34. //Main Process
  35. function make() {
  36. $this->src = str_replace(get_bloginfo('siteurl') . '/', '', $this->src);
  37.  
  38. $this->get_document_root();
  39. $this->get_mime_type();
  40.  
  41. // make sure that the src is gif/jpg/png
  42. if(!$this->valid_src_mime_type($this->mime_type)) { //Overchecking?
  43. die("Invalid src mime type: $mime_type");
  44. }
  45.  
  46. // check to see if GD function exist
  47. if(!function_exists('imagecreatetruecolor')) {
  48. die("GD Library Error: imagecreatetruecolor does not exist");
  49. }
  50. if(strlen($this->src) && file_exists($this->src)) {
  51. $this->generate_thumbnail();
  52. }
  53. else {
  54.  
  55. //If you want to add a default image, this is the place
  56. //$this->thumb_src = 'wp-content/.jpg';
  57.  
  58. }
  59.  
  60. }
  61.  
  62. //Generate the new image, the magic happens here
  63. function generate_thumbnail() {
  64.  
  65. // open the existing image
  66. $image = $this->open_image();
  67. if($image === false) {
  68. die('Unable to open image : ' . $this->src);
  69. }
  70.  
  71. // Get original width and height
  72. $width = imagesx($image);
  73. $height = imagesy($image);
  74.  
  75. // don't allow new width or height to be greater than the original
  76. /*if( $this->width > $width ) {
  77. $this->width = $width;
  78. }
  79. if( $this->height > $height ) {
  80. $this->height = $height;
  81. }*/
  82.  
  83. // generate new w/h if not provided
  84. if( $this->width && !$this->height ) {
  85.  
  86. $this->height = $height * ( $this->width / $width );
  87.  
  88. } elseif($this->height && !$this->width) {
  89.  
  90. $this->width = $width * ( $this->height / $height );
  91.  
  92. } elseif(!$this->width && !$this->height) {
  93.  
  94. $this->width = $width;
  95. $this->height = $height;
  96.  
  97. }
  98.  
  99. // create a new true color image
  100. $canvas = imagecreatetruecolor( $this->width, $this->height );
  101.  
  102. if( $this->crop ) {
  103.  
  104. $src_x = $src_y = 0;
  105. $src_w = $width;
  106. $src_h = $height;
  107.  
  108. $cmp_x = $width / $this->width;
  109. $cmp_y = $height / $this->height;
  110.  
  111. // calculate x or y coordinate and width or height of source
  112.  
  113. if ( $cmp_x > $cmp_y ) {
  114.  
  115. $src_w = round( ( $width / $cmp_x * $cmp_y ) );
  116. $src_x = round( ( $width - ( $width / $cmp_x * $cmp_y ) ) / 2 );
  117.  
  118. } elseif ( $cmp_y > $cmp_x ) {
  119.  
  120. $src_h = round( ( $height / $cmp_y * $cmp_x ) );
  121. $src_y = round( ( $height - ( $height / $cmp_y * $cmp_x ) ) / 2 );
  122.  
  123. }
  124.  
  125. imagecopyresampled( $canvas, $image, 0, 0, $src_x, $src_y, $this->width, $this->height, $src_w, $src_h );
  126.  
  127. } else {
  128.  
  129. // copy and resize part of an image with resampling
  130. imagecopyresampled( $canvas, $image, 0, 0, 0, 0, $this->width, $this->height, $width, $height );
  131.  
  132. }
  133.  
  134. // output image to browser based on mime type
  135. //$this->show_image( $canvas );
  136.  
  137. $this->thumb_src = basename($this->src);
  138. $target_dir = str_replace($this->thumb_src, '', $this->src);
  139. $this->thumb_src = $target_dir . str_replace('.', '_thumb' . $this->width . 'x' . $this->height . '.', $this->thumb_src);
  140.  
  141. //echo $this->thumb_src;
  142.  
  143. if(!file_exists($this->thumb_src)) {
  144.  
  145. if(stristr($this->mime_type, 'gif')) {
  146.  
  147. if(!@imagegif($canvas, $this->thumb_src)) {
  148. $thumbnail = get_bloginfo('stylesheet_directory') . '/images/default_thumbnail.jpg';
  149. ?><img src="<?php echo $thumbnail; ?>" alt="thumbnail" /><?php
  150. }
  151.  
  152. } elseif(stristr($this->mime_type, 'jpeg')) {
  153.  
  154. if(!@imagejpeg($canvas, $this->thumb_src, $this->quality)) {
  155. $thumbnail = get_bloginfo('stylesheet_directory') . '/images/default_thumbnail.jpg';
  156. ?><img src="<?php echo $thumbnail; ?>" alt="thumbnail" /><?php
  157. }
  158.  
  159. } elseif(stristr($this->mime_type, 'png')) {
  160.  
  161. if(!@imagepng($canvas, $this->thumb_src, 9)) {
  162. $thumbnail = get_bloginfo('stylesheet_directory') . '/images/default_thumbnail.jpg';
  163. ?><img src="<?php echo $thumbnail; ?>" alt="thumbnail" /><?php
  164. }
  165.  
  166. }
  167.  
  168. }
  169.  
  170. // remove image from memory
  171. imagedestroy( $canvas );
  172.  
  173. }
  174.  
  175. function get_tag( $attributes = '' ) {
  176.  
  177. //print_r($attributes);
  178.  
  179. if(file_exists($this->thumb_src)) {
  180. $attributes_markup = '';
  181. foreach($attributes as $attr => $value) {
  182. $attributes_markup .= $attr.'="'. $value .'" ';
  183. }
  184. $tag = '<img src="' . get_bloginfo('home') . '/' . $this->thumb_src . '" ' . $attributes_markup . ' />';
  185. return $tag;
  186. }
  187.  
  188. }
  189.  
  190. function generate_tag( $attributes = '' ) {
  191.  
  192. //print_r($attributes);
  193.  
  194. if(file_exists($this->thumb_src)) {
  195. $attributes_markup = '';
  196. foreach($attributes as $attr => $value) {
  197. $attributes_markup .= $attr.'="'. $value .'"';
  198. }
  199. $tag = '<img src="' . get_bloginfo('home') . '/' . $this->thumb_src . '" ' . $attributes_markup . ' />';
  200. echo $tag;
  201. }
  202.  
  203. }
  204.  
  205. function open_image() {
  206.  
  207. if(stristr($this->mime_type, 'gif')) {
  208.  
  209. $image = imagecreatefromgif($this->src);
  210.  
  211. } elseif(stristr($this->mime_type, 'jpeg')) {
  212.  
  213. @ini_set('gd.jpeg_ignore_warning', 1);
  214. $image = imagecreatefromjpeg($this->src);
  215.  
  216. } elseif( stristr($this->mime_type, 'png')) {
  217.  
  218. $image = imagecreatefrompng($this->src);
  219.  
  220. }
  221.  
  222. return $image;
  223.  
  224. }
  225.  
  226. function clean_source () {
  227.  
  228. // remove http/ https/ ftp
  229. $this->src = preg_replace("/^((ht|f)tp(s|):\/\/)/i", "", $this->src);
  230.  
  231. // remove domain name from the source url
  232. $host = $_SERVER["HTTP_HOST"];
  233. $this->src = str_replace($host, "", $this->src);
  234. $host = str_replace("www.", "", $host);
  235. $this->src = str_replace($host, "", $this->src);
  236.  
  237. //$src = preg_replace( "/(?:^\/+|\.{2,}\/+?)/", "", $src );
  238. //$src = preg_replace( '/^\w+:\/\/[^\/]+/', '', $src );
  239.  
  240. // don't allow users the ability to use '../'
  241. // in order to gain access to files below document root
  242.  
  243. // src should be specified relative to document root like:
  244. // src=images/img.jpg or src=/images/img.jpg
  245. // not like:
  246. // src=../images/img.jpg
  247. $this->src = preg_replace( "/\.\.+\//", "", $this->src );
  248. $this->src = substr($this->src, 1, strlen($this->src));
  249. }
  250.  
  251. function get_document_root () {
  252. if( @file_exists( $_SERVER['DOCUMENT_ROOT'] . '/' . $this->src ) ) {
  253. $this->doc_root = $_SERVER['DOCUMENT_ROOT'];
  254. }
  255. // the relative paths below are useful if timthumb is moved outside of document root
  256. // specifically if installed in wordpress themes like mimbo pro:
  257. // /wp-content/themes/mimbopro/scripts/timthumb.php
  258. $paths = array( '..', '../..', '../../..', '../../../..' );
  259. foreach( $paths as $path ) {
  260. if( @file_exists( $path . '/' . $this->src ) ) {
  261. $this->doc_root = $path;
  262. }
  263. }
  264.  
  265. }
  266.  
  267. function get_mime_type() {
  268.  
  269. $os = strtolower(php_uname());
  270. $mime_type = '';
  271.  
  272. // use PECL fileinfo to determine mime type
  273. if( function_exists('finfo_open')) {
  274. $finfo = finfo_open(FILEINFO_MIME);
  275. $mime_type = finfo_file($finfo, $this->src);
  276. finfo_close($finfo);
  277. }
  278.  
  279. // try to determine mime type by using unix file command
  280. // this should not be executed on windows
  281. if(!$this->valid_src_mime_type($mime_type) && !(preg_match('/windows/i', $os))) {
  282. if(preg_match("/freebsd|linux/", $os)) {
  283. $mime_type = trim(@shell_exec('file -bi $file'));
  284. }
  285. }
  286.  
  287. // use file's extension to determine mime type
  288. if(!$this->valid_src_mime_type($mime_type)) {
  289.  
  290. // set defaults
  291. $mime_type = 'image/jpeg';
  292. // file details
  293. $fileDetails = pathinfo($this->src);
  294. $ext = strtolower($fileDetails["extension"]);
  295. // mime types
  296. $types = array(
  297. 'jpg' => 'image/jpeg',
  298. 'jpeg' => 'image/jpeg',
  299. 'png' => 'image/png',
  300. 'gif' => 'image/gif'
  301. );
  302.  
  303. if(strlen($ext) && strlen($types[$ext])) {
  304. $mime_type = $types[$ext];
  305. }
  306.  
  307. }
  308.  
  309. $this->mime_type = $mime_type;
  310.  
  311. }
  312.  
  313. function valid_src_mime_type($mime_type) {
  314.  
  315. if(preg_match("/jpg|jpeg|gif|png/i", $mime_type)) {
  316. return true;
  317. }
  318. return false;
  319.  
  320. }
  321.  
  322. function check_cache() {
  323.  
  324. // make sure cache dir exists
  325. if(!file_exists($this->cache_dir)) {
  326. // give 777 permissions so that developer can overwrite
  327. // files created by web server user
  328. mkdir($this->cache_dir);
  329. chmod($this->cache_dir, 0777);
  330. }
  331.  
  332. $this->show_cache_file($this->cache_dir);
  333.  
  334. }
  335.  
  336. function show_cache_file() {
  337.  
  338. $cache_file = $this->cache_dir . '/' . $this->get_cache_file();
  339.  
  340. if( file_exists( $cache_file ) ) {
  341.  
  342. if( isset( $_SERVER[ "HTTP_IF_MODIFIED_SINCE" ] ) ) {
  343.  
  344. // check for updates
  345. $if_modified_since = preg_replace( '/;.*$/', '', $_SERVER[ "HTTP_IF_MODIFIED_SINCE" ] );
  346. $gmdate_mod = gmdate( 'D, d M Y H:i:s', filemtime( $cache_file ) );
  347.  
  348. if( strstr( $gmdate_mod, 'GMT' ) ) {
  349. $gmdate_mod .= " GMT";
  350. }
  351.  
  352. if ( $if_modified_since == $gmdate_mod ) {
  353. header( "HTTP/1.1 304 Not Modified" );
  354. exit;
  355. }
  356.  
  357. }
  358.  
  359. $fileSize = filesize($cache_file);
  360.  
  361. // send headers then display image
  362. header("Content-Type: " . $this->mime_type);
  363. //header("Accept-Ranges: bytes");
  364. header("Last-Modified: " . gmdate('D, d M Y H:i:s', filemtime($cache_file)) . " GMT");
  365. header("Content-Length: " . $fileSize);
  366. header("Cache-Control: max-age=9999, must-revalidate");
  367. header("Expires: " . gmdate("D, d M Y H:i:s", time() + 9999) . "GMT");
  368.  
  369. readfile($cache_file);
  370.  
  371. die();
  372.  
  373. }
  374.  
  375. }
  376.  
  377. function get_cache_file () {
  378.  
  379. static $cache_file;
  380. if(!$cache_file) {
  381. $frags = split( "\.", $_REQUEST['src'] );
  382. $ext = strtolower( $frags[ count( $frags ) - 1 ] );
  383. if(!$this->valid_extension($ext)) { $ext = 'jpg'; }
  384. //$cachename = get_request( 'src', 'timthumb' ) . get_request( 'w', 100 ) . get_request( 'h', 100 ) . get_request( 'zc', 1 ) . get_request( '9', 80 );
  385. //$cachename = $this->get_request( 'src', 'timthumb' ) . $this->width . $this->height . $this->crop . $this->quality;
  386. $cachename = $this->src . $this->width . $this->height . $this->crop . $this->quality;
  387. $cache_file = md5( $cachename ) . '.' . $ext;
  388. }
  389. return $cache_file;
  390.  
  391. }
  392.  
  393. function get_request( $property, $default = 0 ) {
  394.  
  395. if( isset($_REQUEST[$property]) ) {
  396. return $_REQUEST[$property];
  397. } else {
  398. return $default;
  399. }
  400.  
  401. }
  402.  
  403. function valid_extension ($ext) {
  404.  
  405. if( preg_match( "/jpg|jpeg|png|gif/i", $ext ) ) return 1;
  406. return 0;
  407.  
  408. }
  409.  
  410.  
  411. }
  412.  
  413.  
  414. ?>
Advertisement
Add Comment
Please, Sign In to add comment