Ver Mensaje Individual
  #7 (permalink)  
Antiguo 11/02/2010, 09:09
Masane
 
Fecha de Ingreso: marzo-2008
Mensajes: 207
Antigüedad: 16 años, 2 meses
Puntos: 0
Respuesta: Guardar thumbnails

Pues chico, no me ha servido, no me reconoce el directorio :S ...

Pongo el código del timthumb.php de Wordpress, que hace unos thumbnails estupendos, a ver si alguien me puede decir qué insertar y dónde para poder copiar el thumbnail generado en cache y pegarlo con otro nombre para poder llamarlo luego desde la home estática.

Código PHP:
Ver original
  1. <?php
  2. /*
  3.     TimThumb script created by Tim McDaniels and Darren Hoyt with tweaks by Ben Gillbanks
  4.     http://code.google.com/p/timthumb/
  5.  
  6.     MIT License: http://www.opensource.org/licenses/mit-license.php
  7.  
  8.     Paramters
  9.     ---------
  10.     w: width
  11.     h: height
  12.     zc: zoom crop (0 or 1)
  13.     q: quality (default is 75 and max is 100)
  14.    
  15.     HTML example: <img src="/scripts/timthumb.php?src=/images/whatever.jpg&w=150&h=200&zc=1" alt="" />
  16. */
  17.  
  18. /*
  19. $sizeLimits = array(
  20.     "100x100",
  21.     "150x150",
  22. );
  23.  
  24. error_reporting(E_ALL);
  25. ini_set("display_errors", 1);
  26. */
  27.  
  28. // check to see if GD function exist
  29. if(!function_exists('imagecreatetruecolor')) {
  30.     displayError('GD Library Error: imagecreatetruecolor does not exist - please contact your webhost and ask them to install the GD library');
  31. }
  32.  
  33. define ('CACHE_SIZE', 250);        // number of files to store before clearing cache
  34. define ('CACHE_CLEAR', 5);        // maximum number of files to delete on each cache clear
  35. define ('VERSION', '1.11');        // version number (to force a cache refresh
  36.  
  37. if (function_exists('imagefilter') && defined('IMG_FILTER_NEGATE')) {
  38.     $imageFilters = array(
  39.         "1" => array(IMG_FILTER_NEGATE, 0),
  40.         "2" => array(IMG_FILTER_GRAYSCALE, 0),
  41.         "3" => array(IMG_FILTER_BRIGHTNESS, 1),
  42.         "4" => array(IMG_FILTER_CONTRAST, 1),
  43.         "5" => array(IMG_FILTER_COLORIZE, 4),
  44.         "6" => array(IMG_FILTER_EDGEDETECT, 0),
  45.         "7" => array(IMG_FILTER_EMBOSS, 0),
  46.         "8" => array(IMG_FILTER_GAUSSIAN_BLUR, 0),
  47.         "9" => array(IMG_FILTER_SELECTIVE_BLUR, 0),
  48.         "10" => array(IMG_FILTER_MEAN_REMOVAL, 0),
  49.         "11" => array(IMG_FILTER_SMOOTH, 0),
  50.     );
  51. }
  52.  
  53. // sort out image source
  54. $src = get_request("src", "");
  55. if($src == '' || strlen($src) <= 3) {
  56.     displayError ('no image specified');
  57. }
  58.  
  59. // clean params before use
  60. $src = cleanSource($src);
  61. // last modified time (for caching)
  62. $lastModified = filemtime($src);
  63.  
  64. // get properties
  65. $new_width         = preg_replace("/[^0-9]+/", "", get_request("w", 0));
  66. $new_height     = preg_replace("/[^0-9]+/", "", get_request("h", 0));
  67. $zoom_crop         = preg_replace("/[^0-9]+/", "", get_request("zc", 1));
  68. $quality         = preg_replace("/[^0-9]+/", "", get_request("q", 80));
  69. $filters        = get_request("f", "");
  70.  
  71. if ($new_width == 0 && $new_height == 0) {
  72.     $new_width = 100;
  73.     $new_height = 100;
  74. }
  75.  
  76. // set path to cache directory (default is ./tmp)
  77. // this can be changed to a different location
  78. $cache_dir = '../img/thumbnails_portada/tmp';
  79.  
  80. // get mime type of src
  81. $mime_type = mime_type($src);
  82.  
  83. // check to see if this image is in the cache already
  84. check_cache ($cache_dir, $mime_type);
  85.  
  86. // if not in cache then clear some space and generate a new file
  87. cleanCache();
  88.  
  89. ini_set('memory_limit', "50M");
  90.  
  91. // make sure that the src is gif/jpg/png
  92. if(!valid_src_mime_type($mime_type)) {
  93.     displayError("Invalid src mime type: " .$mime_type);
  94. }
  95.  
  96. if(strlen($src) && file_exists($src)) {
  97.  
  98.     // open the existing image
  99.     $image = open_image($mime_type, $src);
  100.     if($image === false) {
  101.         displayError('Unable to open image : ' . $src);
  102.     }
  103.  
  104.     // Get original width and height
  105.     $width = imagesx($image);
  106.     $height = imagesy($image);
  107.    
  108.     // generate new w/h if not provided
  109.     if( $new_width && !$new_height ) {
  110.        
  111.         $new_height = $height * ( $new_width / $width );
  112.        
  113.     } elseif($new_height && !$new_width) {
  114.        
  115.         $new_width = $width * ( $new_height / $height );
  116.        
  117.     } elseif(!$new_width && !$new_height) {
  118.        
  119.         $new_width = $width;
  120.         $new_height = $height;
  121.        
  122.     }
  123.    
  124.     // create a new true color image
  125.     $canvas = imagecreatetruecolor( $new_width, $new_height );
  126.     imagealphablending($canvas, false);
  127.     // Create a new transparent color for image
  128.     $color = imagecolorallocatealpha($canvas, 0, 0, 0, 127);
  129.     // Completely fill the background of the new image with allocated color.
  130.     imagefill($canvas, 0, 0, $color);
  131.     // Restore transparency blending
  132.     imagesavealpha($canvas, true);
  133.  
  134.     if( $zoom_crop ) {
  135.  
  136.         $src_x = $src_y = 0;
  137.         $src_w = $width;
  138.         $src_h = $height;
  139.  
  140.         $cmp_x = $width  / $new_width;
  141.         $cmp_y = $height / $new_height;
  142.  
  143.         // calculate x or y coordinate and width or height of source
  144.  
  145.         if ( $cmp_x > $cmp_y ) {
  146.  
  147.             $src_w = round( ( $width / $cmp_x * $cmp_y ) );
  148.             $src_x = round( ( $width - ( $width / $cmp_x * $cmp_y ) ) / 2 );
  149.  
  150.         } elseif ( $cmp_y > $cmp_x ) {
  151.  
  152.             $src_h = round( ( $height / $cmp_y * $cmp_x ) );
  153.             $src_y = round( ( $height - ( $height / $cmp_y * $cmp_x ) ) / 2 );
  154.  
  155.         }
  156.        
  157.         imagecopyresampled( $canvas, $image, 0, 0, $src_x, $src_y, $new_width, $new_height, $src_w, $src_h );
  158.  
  159.     } else {
  160.  
  161.         // copy and resize part of an image with resampling
  162.         imagecopyresampled( $canvas, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
  163.  
  164.     }
  165.    
  166.     if ($filters != '' && function_exists('imagefilter') && defined('IMG_FILTER_NEGATE')) {
  167.         // apply filters to image
  168.         $filterList = explode("|", $filters);
  169.         foreach($filterList as $fl) {
  170.             $filterSettings = explode(",", $fl);
  171.             if(isset($imageFilters[$filterSettings[0]])) {
  172.            
  173.                 for($i = 0; $i < 4; $i ++) {
  174.                     if(!isset($filterSettings[$i])) {
  175.                         $filterSettings[$i] = null;
  176.                     }
  177.                 }
  178.                
  179.                 switch($imageFilters[$filterSettings[0]][1]) {
  180.                
  181.                     case 1:
  182.                    
  183.                         imagefilter($canvas, $imageFilters[$filterSettings[0]][0], $filterSettings[1]);
  184.                         break;
  185.                    
  186.                     case 2:
  187.                    
  188.                         imagefilter($canvas, $imageFilters[$filterSettings[0]][0], $filterSettings[1], $filterSettings[2]);
  189.                         break;
  190.                    
  191.                     case 3:
  192.                    
  193.                         imagefilter($canvas, $imageFilters[$filterSettings[0]][0], $filterSettings[1], $filterSettings[2], $filterSettings[3]);
  194.                         break;
  195.                    
  196.                     default:
  197.                    
  198.                         imagefilter($canvas, $imageFilters[$filterSettings[0]][0]);
  199.                         break;
  200.                        
  201.                 }
  202.             }
  203.         }
  204.     }
  205.    
  206.     // output image to browser based on mime type
  207.     show_image($mime_type, $canvas, $cache_dir);
  208.    
  209.     // remove image from memory
  210.     imagedestroy($canvas);
  211.    
  212. } else {
  213.  
  214.     if(strlen($src)) {
  215.         displayError("image " . $src . " not found");
  216.     } else {
  217.         displayError("no source specified");
  218.     }
  219.    
  220. }
  221.  
  222. /**
  223.  *
  224.  */
  225. function show_image($mime_type, $image_resized, $cache_dir) {
  226.  
  227.     global $quality;
  228.  
  229.     // check to see if we can write to the cache directory
  230.     $is_writable = 0;
  231.     $cache_file_name = $cache_dir . '/' . get_cache_file();
  232.  
  233.     if (touch($cache_file_name)) {
  234.        
  235.         // give 666 permissions so that the developer
  236.         // can overwrite web server user
  237.         chmod ($cache_file_name, 0666);
  238.         $is_writable = 1;
  239.        
  240.     } else {
  241.        
  242.         $cache_file_name = NULL;
  243.         header ('Content-type: ' . $mime_type);
  244.        
  245.     }
  246.  
  247.     switch ($mime_type) {
  248.    
  249.         case 'image/jpeg':
  250.             imagejpeg($image_resized, $cache_file_name, $quality);
  251.             break;
  252.        
  253.         default :
  254.             $quality = floor ($quality * 0.09);
  255.             imagepng($image_resized, $cache_file_name, $quality);
  256.            
  257.     }
  258.    
  259.     if ($is_writable) {
  260.         show_cache_file ($cache_dir, $mime_type);
  261.     }
  262.  
  263.     imagedestroy ($image_resized);
  264.    
  265.     displayError ("error showing image");
  266.  
  267. }
  268.  
  269. /**
  270.  *
  271.  */
  272. function get_request( $property, $default = 0 ) {
  273.    
  274.     if( isset($_REQUEST[$property]) ) {
  275.    
  276.         return $_REQUEST[$property];
  277.        
  278.     } else {
  279.    
  280.         return $default;
  281.        
  282.     }
  283.    
  284. }
  285.  
  286. /**
  287.  *
  288.  */
  289. function open_image($mime_type, $src) {
  290.  
  291.     $mime_type = strtolower($mime_type);
  292.    
  293.     if (stristr ($mime_type, 'gif')) {
  294.    
  295.         $image = imagecreatefromgif($src);
  296.        
  297.     } elseif (stristr($mime_type, 'jpeg')) {
  298.    
  299.         @ini_set ('gd.jpeg_ignore_warning', 1);
  300.         $image = imagecreatefromjpeg($src);
  301.        
  302.     } elseif (stristr ($mime_type, 'png')) {
  303.    
  304.         $image = imagecreatefrompng($src);
  305.        
  306.     }
  307.    
  308.     return $image;
  309.  
  310. }
  311. ?>