Basic PHP document management system, including automatic detection of corporate logos in letters
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

thumb.php 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?PHP
  2. /******************************************************************************\
  3. thumb.php: Generate thumbnail image of specified file.
  4. Copyright (C) 2004-2007 Chris 'MD87' Smith
  5. This program is free software; you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation; either version 2
  8. of the License, or (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. |******************************************************************************|
  17. Usage:
  18. <img src="thumb.php?file_name.png"...>
  19. <img src="thumb.php?directory/file.jpg"...>
  20. <img src="thumb.php?http://www.example.com/file.jpg"...>
  21. \******************************************************************************/
  22. header('Cache-control: public');
  23. $image = $_SERVER['QUERY_STRING'];
  24. if (substr($image, 0, 6) == 'large:') {
  25. define('LARGE', true);
  26. $image = substr($image, 6);
  27. } else {
  28. define('LARGE', false);
  29. }
  30. define('THUMB_WIDTH', LARGE ? 240 : 80); // Maximum width for thumbnails
  31. define('THUMB_HEIGHT',LARGE ? 300 : 100); // Maximum height for thumbnails
  32. define('THUMB_BACK','255,255,255'); // Background colour
  33. if (!file_exists($image)) {
  34. /* TODO: Output error image. */
  35. die();
  36. }
  37. if (file_exists('.thumbs/' . (LARGE ? 'large-' : '') . md5($image))) {
  38. $mtime = filemtime('.thumbs/' . (LARGE ? 'large-' : '') . md5($image));
  39. if ($mtime >= filemtime($image)) {
  40. header('Content-type: image/jpeg');
  41. readfile('.thumbs/' . (LARGE ? 'large-' : '') . md5($image));
  42. exit;
  43. }
  44. }
  45. /* TODO: Optimise. */
  46. if (($imi = @imagecreatefromjpeg($image)) === FALSE) {
  47. if (($imi = @imagecreatefrompng($image)) === FALSE) {
  48. if (($imi = @imagecreatefromgif($image)) === FALSE) {
  49. /* TODO: Output error image. */
  50. die();
  51. }
  52. }
  53. }
  54. $width = imagesx($imi); $height = imagesy($imi);
  55. $Rwidth = (THUMB_WIDTH/$width);
  56. $Rheight = (THUMB_HEIGHT/$height);
  57. if ($Rwidth > $Rheight) { $ratio = $Rheight; } else { $ratio = $Rwidth; }
  58. if ($width > THUMB_WIDTH || $height > THUMB_HEIGHT) {
  59. $Nwidth = $width * $ratio;
  60. $Nheight = $height * $ratio;
  61. } else {
  62. $Nheight = $height;
  63. $Nwidth = $width;
  64. }
  65. $imo = imagecreatetruecolor(THUMB_WIDTH, LARGE ? $Nheight : THUMB_HEIGHT);
  66. $colour = explode(',',THUMB_BACK);
  67. imagefill($imo,1,1,imagecolorallocate($imo,$colour[0],$colour[1],$colour[2]));
  68. imagecopyresampled($imo,$imi,(THUMB_WIDTH-$Nwidth)/2 , LARGE ? 0 : (THUMB_HEIGHT-$Nheight)/2, 0, 0, $Nwidth, $Nheight, $width, $height);
  69. header('Content-type: image/jpeg');
  70. imagejpeg($imo, '.thumbs/' . (LARGE ? 'large-' : '') . md5($image), 100);
  71. imagejpeg($imo, false, 100);
  72. ?>