/ Published in: PHP
URL: http://timthumb.googlecode.com/svn/trunk/timthumb.php
TimThumb script created by Tim McDaniels and Darren Hoyt with tweaks by Ben Gillbanks (revision 81)
Expand |
Embed | Plain Text
<?php /* TimThumb script created by Tim McDaniels and Darren Hoyt with tweaks by Ben Gillbanks http://code.google.com/p/timthumb/ MIT License: http://www.opensource.org/licenses/mit-license.php Paramters --------- w: width h: height zc: zoom crop (0 or 1) q: quality (default is 75 and max is 100) HTML example: <img src="/scripts/timthumb.php?src=/images/whatever.jpg&w=150&h=200&zc=1" alt="" /> */ /* $sizeLimits = array( "100x100", "150x150", ); error_reporting(E_ALL); ini_set("display_errors", 1); */ // check to see if GD function exist displayError('GD Library Error: imagecreatetruecolor does not exist - please contact your webhost and ask them to install the GD library'); } ); } // sort out image source $src = get_request("src", ""); displayError ('no image specified'); } // clean params before use $src = cleanSource($src); // last modified time (for caching) // get properties $filters = get_request('f', ''); $sharpen = get_request('s', 0); if ($new_width == 0 && $new_height == 0) { $new_width = 100; $new_height = 100; } // get mime type of src $mime_type = mime_type($src); // check to see if this image is in the cache already check_cache ($mime_type); // if not in cache then clear some space and generate a new file cleanCache(); // make sure that the src is gif/jpg/png if(!valid_src_mime_type($mime_type)) { displayError('Invalid src mime type: ' . $mime_type); } // open the existing image $image = open_image($mime_type, $src); if($image === false) { displayError('Unable to open image : ' . $src); } // Get original width and height $width = imagesx($image); $height = imagesy($image); // generate new w/h if not provided if( $new_width && !$new_height ) { $new_height = $height * ( $new_width / $width ); } elseif($new_height && !$new_width) { $new_width = $width * ( $new_height / $height ); } elseif(!$new_width && !$new_height) { $new_width = $width; $new_height = $height; } // create a new true color image $canvas = imagecreatetruecolor( $new_width, $new_height ); imagealphablending($canvas, false); // Create a new transparent color for image $color = imagecolorallocatealpha($canvas, 0, 0, 0, 127); // Completely fill the background of the new image with allocated color. imagefill($canvas, 0, 0, $color); // Restore transparency blending imagesavealpha($canvas, true); if( $zoom_crop ) { $src_x = $src_y = 0; $src_w = $width; $src_h = $height; $cmp_x = $width / $new_width; $cmp_y = $height / $new_height; // calculate x or y coordinate and width or height of source if ( $cmp_x > $cmp_y ) { } elseif ( $cmp_y > $cmp_x ) { } imagecopyresampled( $canvas, $image, 0, 0, $src_x, $src_y, $new_width, $new_height, $src_w, $src_h ); } else { // copy and resize part of an image with resampling imagecopyresampled( $canvas, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height ); } // apply filters to image foreach($filterList as $fl) { for($i = 0; $i < 4; $i ++) { $filterSettings[$i] = null; } } switch($imageFilters[$filterSettings[0]][1]) { case 1: imagefilter($canvas, $imageFilters[$filterSettings[0]][0], $filterSettings[1]); break; case 2: imagefilter($canvas, $imageFilters[$filterSettings[0]][0], $filterSettings[1], $filterSettings[2]); break; case 3: imagefilter($canvas, $imageFilters[$filterSettings[0]][0], $filterSettings[1], $filterSettings[2], $filterSettings[3]); break; default: imagefilter($canvas, $imageFilters[$filterSettings[0]][0]); break; } } } } ); $divisor = 8; $offset = 0; imageconvolution($canvas, $sharpenMatrix, $divisor, $offset); } // output image to browser based on mime type show_image($mime_type, $canvas); // remove image from memory imagedestroy($canvas); } else { displayError ('image ' . $src . ' not found'); } else { displayError ('no source specified'); } } /** * */ function show_image($mime_type, $image_resized) { global $quality; // check to see if we can write to the cache directory $is_writable = 0; $cache_file_name = DIRECTORY_CACHE . '/' . get_cache_file(); // give 666 permissions so that the developer // can overwrite web server user $is_writable = 1; } else { $cache_file_name = NULL; } switch ($mime_type) { case 'image/jpeg': imagejpeg($image_resized, $cache_file_name, $quality); break; default : imagepng($image_resized, $cache_file_name, $quality); } if ($is_writable) { show_cache_file ($mime_type); } imagedestroy ($image_resized); displayError ('error showing image'); } /** * */ function get_request( $property, $default = 0 ) { return $_REQUEST[$property]; } else { return $default; } } /** * */ function open_image($mime_type, $src) { $image = imagecreatefromgif($src); $image = imagecreatefromjpeg($src); $image = imagecreatefrompng($src); } return $image; } /** * clean out old files from the cache * you can change the number of files to store and to delete per loop in the defines at the top of the code */ function cleanCache() { $i = 0; foreach ($files as $file) { $i ++; if ($i >= CACHE_CLEAR) { return; } return; } } } } } } /** * compare the file time of two files */ function filemtime_compare($a, $b) { } /** * determine the file mime type */ function mime_type($file) { $os = 'WIN'; } else { $os = PHP_OS; } $mime_type = ''; $mime_type = mime_content_type($file); } // use PECL fileinfo to determine mime type if (!valid_src_mime_type($mime_type)) { $finfo = @finfo_open(FILEINFO_MIME); if ($finfo != '') { $mime_type = finfo_file($finfo, $file); finfo_close($finfo); } } } // try to determine mime type by using unix file command // this should not be executed on windows if (!valid_src_mime_type($mime_type) && $os != "WIN") { } } // use file's extension to determine mime type if (!valid_src_mime_type($mime_type)) { // set defaults $mime_type = 'image/png'; // file details // mime types 'jpg' => 'image/jpeg', 'jpeg' => 'image/jpeg', 'png' => 'image/png', 'gif' => 'image/gif' ); $mime_type = $types[$ext]; } } return $mime_type; } /** * */ function valid_src_mime_type($mime_type) { return true; } return false; } /** * */ function check_cache ($mime_type) { // make sure cache dir exists // give 777 permissions so that developer can overwrite // files created by web server user } show_cache_file ($mime_type); } /** * */ function show_cache_file ($mime_type) { $cache_file = DIRECTORY_CACHE . '/' . get_cache_file(); $gmdate_mod .= " GMT"; } // check for updates if ($if_modified_since == $gmdate_mod) { } } // send headers then display image } } /** * */ function get_cache_file() { global $lastModified; static $cache_file; if (!$cache_file) { $cachename = $_SERVER['QUERY_STRING'] . VERSION . $lastModified; } return $cache_file; } /** * check to if the url is valid or not */ function valid_extension ($ext) { return TRUE; } else { return FALSE; } } /** * */ function checkExternal ($src) { 'flickr.com', 'picasa.com', 'blogger.com', 'wordpress.com', 'img.youtube.com', ); $isAllowedSite = false; foreach ($allowedSites as $site) { $isAllowedSite = true; } } if ($isAllowedSite) { $local_filepath = DIRECTORY_TEMP . '/' . $filename . '.' . $ext; $ch = curl_init($src); curl_setopt($ch, CURLOPT_URL, $src); curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.5) Gecko/20041107 Firefox/1.0'); curl_setopt($ch, CURLOPT_FILE, $fh); if (curl_exec($ch) === FALSE) { } displayError('error reading file ' . $src . ' from remote host: ' . curl_error($ch)); } curl_close($ch); } else { displayError('remote file for ' . $src . ' can not be accessed. It is likely that the file permissions are restricted'); } if (file_put_contents($local_filepath, $img) == FALSE) { displayError('error writing temporary file'); } } displayError('local file for ' . $src . ' can not be created'); } } $src = $local_filepath; } else { displayError('remote host "' . $url_info['host'] . '" not allowed'); } } return $src; } /** * tidy up the image source url */ function cleanSource($src) { $regex = "/^((ht|f)tp(s|):\/\/)(www\.|)" . $host . "/i"; $src = checkExternal ($src); // remove slash from start of string } // don't allow users the ability to use '../' // in order to gain access to files below document root // get path to image on file system $src = get_document_root($src) . '/' . $src; return $src; } /** * */ function get_document_root ($src) { // check for unix servers return $_SERVER['DOCUMENT_ROOT']; } // check from script filename (to get all directories to timthumb location) $parts = array_diff(explode('/', $_SERVER['SCRIPT_FILENAME']), explode('/', $_SERVER['DOCUMENT_ROOT'])); $path = $_SERVER['DOCUMENT_ROOT']; foreach ($parts as $part) { $path .= '/' . $part; return $path; } } // the relative paths below are useful if timthumb is moved outside of document root // specifically if installed in wordpress themes like mimbo pro: // /wp-content/themes/mimbopro/scripts/timthumb.php ".", "..", "../..", "../../..", "../../../..", "../../../../.." ); foreach ($paths as $path) { return $path; } } // special check for microsoft servers return $path; } } displayError('file not found ' . $src); } /** * generic error message */ function displayError ($errorString = '') { } ?>
You need to login to post a comment.
