Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Based on http://codigofonte.uol.com.br/codigos/gerando-thumbnails-de-imagens-com-php
- Usage:
- http://yourdomain.com/thumbs/800x600/imagename.jpg
- Where:
- 800x600 is the resolution
- 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)
- Create a folder named "cache" and CHMOD 777 it.
- File ".htaccess" (on the index/www-root/public_html/webserver root):
- RewriteEngine On
- RewriteRule ^^thumbs\/([0-9]*)x([0-9]*)\-([^\.]*)\.(jpg|png|gif|bmp|JPG|BMP|GIF|PNG)$ thumb.php?x=$1&y=$2&img=$3.$4
- File ".htaccess" (on the cache folder)
- Order Allow,Deny
- Deny from All
- File "thumb.php":
- <?php
- $original_images_folder = 'photos'; // set it here
- function gera_thumb($nome_img, $lar_maxima, $alt_maxima, $qualidade) {
- $ext = end(explode(".", $nome_img));
- if(!preg_match("/^(jpg|png|gif|bmp|JPG|PNG|GIF|BMP)$/", $ext)) {
- header("Location: /error");
- exit;
- }
- // checa /cache
- $cachename = 'cache/'.md5($nome_img.$lar_maxima.$alt_maxima.$qualidade);
- if(file_exists($cachename)) {
- if($ext=='jpg') $tipo = 'jpeg';
- else $tipo = $ext;
- header("Content-type:image/".$tipo);
- echo file_get_contents($cachename);
- exit;
- } else {
- if($qualidade == ''){ $qualidade = 100; }
- $size = getimagesize($nome_img);
- $tipo = $size[2];
- # Pega onde está a imagem e carrega
- if($tipo == 2){ // 2 é o JPG
- $img = imagecreatefromjpeg($nome_img);
- } if($tipo == 1){ // 1 é o GIF
- $img = imagecreatefromgif($nome_img);
- } if($tipo == 3){ // 3 é PNG
- $img = imagecreatefrompng($nome_img);
- }
- // Se a imagem foi carregada com sucesso, testa o tamanho da mesma
- if ($img) {
- // Pega o tamanho da imagem e proporção de resize
- $width = imagesx($img);
- $height = imagesy($img);
- $scale = min($lar_maxima/$width, $alt_maxima/$height);
- // Se a imagem é maior que o permitido, encolhe ela!
- if ($scale < 1) {
- $new_width = floor($scale*$width);
- $new_height = floor($scale*$height);
- // Cria uma imagem temporária
- $tmp_img = imagecreatetruecolor($new_width, $new_height);
- // Copia e resize a imagem velha na nova
- imagecopyresampled ($tmp_img, $img, 0, 0, 0, 0,
- $new_width, $new_height, $width, $height);
- // imagedestroy($img);
- $img = $tmp_img;
- }
- }
- if($ext=='jpg') $tipo = 'jpeg';
- else $tipo = $ext;
- header("Content-type:image/".$tipo);
- $ext_sub = strtolower($ext);
- switch($ext_sub) {
- case "jpg":
- imagejpeg($img, $cachename,$qualidade);
- // imagejpeg($img, null, $qualidade);
- break;
- case "png":
- imagepng($img, $cachename, $qualidade);
- // imagepng($img, null, $qualidade);
- break;
- case "gif":
- imagegif($img, $cachename, $qualidade);
- // imagegif($img, null, $qualidade);
- break;
- }
- imagedestroy($img);
- }
- }
- 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