jesobreira

Generate thumb

Nov 6th, 2014
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.17 KB | None | 0 0
  1. Based on http://codigofonte.uol.com.br/codigos/gerando-thumbnails-de-imagens-com-php
  2.  
  3. Usage:
  4. http://yourdomain.com/thumbs/800x600/imagename.jpg
  5. Where:
  6.     800x600 is the resolution
  7.     imagename.jpg is the image name (it shall be on 'images' folder; you can also set the folder name on the second line of thumb.php ($original_images_folder variable)
  8.  
  9. Create a folder named "cache" and CHMOD 777 it.
  10.  
  11. File ".htaccess" (on the index/www-root/public_html/webserver root):
  12. RewriteEngine On
  13. RewriteRule ^^thumbs\/([0-9]*)x([0-9]*)\-([^\.]*)\.(jpg|png|gif|bmp|JPG|BMP|GIF|PNG)$ thumb.php?x=$1&y=$2&img=$3.$4
  14.  
  15. File ".htaccess" (on the cache folder)
  16. Order Allow,Deny
  17. Deny from All
  18.  
  19. File "thumb.php":
  20. <?php
  21. $original_images_folder = 'photos'; // set it here
  22.  
  23. function gera_thumb($nome_img, $lar_maxima, $alt_maxima, $qualidade) {
  24.             $ext = end(explode(".", $nome_img));
  25.             if(!preg_match("/^(jpg|png|gif|bmp|JPG|PNG|GIF|BMP)$/", $ext)) {
  26.                 header("Location: /error");
  27.                 exit;
  28.             }
  29.        
  30.         // checa /cache
  31.         $cachename = 'cache/'.md5($nome_img.$lar_maxima.$alt_maxima.$qualidade);
  32.         if(file_exists($cachename)) {
  33.             if($ext=='jpg') $tipo = 'jpeg';
  34.             else $tipo = $ext;
  35.             header("Content-type:image/".$tipo);
  36.            
  37.             echo file_get_contents($cachename);
  38.             exit;
  39.         } else {
  40.  
  41.             if($qualidade == ''){ $qualidade = 100; }
  42.            
  43.             $size = getimagesize($nome_img);
  44.             $tipo = $size[2];
  45.            
  46.             # Pega onde está a imagem e carrega     
  47.             if($tipo == 2){ // 2 é o JPG
  48.             $img = imagecreatefromjpeg($nome_img);     
  49.             } if($tipo == 1){ // 1 é o GIF
  50.             $img = imagecreatefromgif($nome_img);      
  51.             } if($tipo == 3){ // 3 é PNG
  52.             $img = imagecreatefrompng($nome_img);      
  53.             }
  54.  
  55.  
  56.             // Se a imagem foi carregada com sucesso, testa o tamanho da mesma
  57.             if ($img) {
  58.                  // Pega o tamanho da imagem e proporção de resize
  59.                  $width  = imagesx($img);
  60.                  $height = imagesy($img);
  61.                  $scale  = min($lar_maxima/$width, $alt_maxima/$height);
  62.            
  63.                  // Se a imagem é maior que o permitido, encolhe ela!
  64.                  if ($scale < 1) {
  65.                      $new_width  = floor($scale*$width);
  66.                      $new_height = floor($scale*$height);
  67.                
  68.                      // Cria uma imagem temporária
  69.                      $tmp_img = imagecreatetruecolor($new_width, $new_height);
  70.                            
  71.                      // Copia e resize a imagem velha na nova
  72.                      imagecopyresampled ($tmp_img, $img, 0, 0, 0, 0,
  73.                      $new_width, $new_height, $width, $height);
  74.                                                                          
  75.                     // imagedestroy($img);
  76.                      $img = $tmp_img;
  77.                      
  78.                   }
  79.                  
  80.             }  
  81.             if($ext=='jpg') $tipo = 'jpeg';
  82.             else $tipo = $ext;
  83.             header("Content-type:image/".$tipo);
  84.            
  85.             $ext_sub = strtolower($ext);
  86.             switch($ext_sub) {
  87.                 case "jpg":
  88.                     imagejpeg($img, $cachename,$qualidade);
  89.                     // imagejpeg($img, null, $qualidade);
  90.                     break;
  91.                    
  92.                 case "png":
  93.                     imagepng($img, $cachename, $qualidade);
  94.                     // imagepng($img, null, $qualidade);
  95.                     break;
  96.                    
  97.                 case "gif":
  98.                     imagegif($img, $cachename, $qualidade);
  99.                     // imagegif($img, null, $qualidade);
  100.                     break;
  101.                    
  102.             }
  103.                        
  104.             imagedestroy($img);
  105.        
  106.                  
  107.         }
  108. }
  109.         gera_thumb($original_images_folder.'/'.basename($_GET['img']),(int)$_GET['x'],(int)$_GET['y'],(int)$_GET['q']);
Add Comment
Please, Sign In to add comment