Created
March 15, 2012 21:25
-
-
Save rbarros/2047075 to your computer and use it in GitHub Desktop.
Thumb with crop image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<? | |
# Arquivo: thumb.php | |
# Autor: Helbert Fernandes - estaleiroweb | |
# baseado no trabalho de Mauricio Wolff | |
include "fileinc.php"; // Inclusão de funcões de Caminho e Diretório | |
// Constantes: variaveis que não mudam em todo o programa | |
define('MAX_WIDTH' , (isset($_REQUEST['w']))?$_REQUEST['w']:700); | |
define('MAX_HEIGHT', (isset($_REQUEST['h']))?$_REQUEST['h']:700); | |
define('CROP_WIDTH', (isset($_REQUEST['cropw']))?$_REQUEST['cropw']:MAX_WIDTH); | |
define('CROP_HEIGHT', (isset($_REQUEST['croph']))?$_REQUEST['croph']:MAX_HEIGHT); | |
define('CROP_X', (isset($_REQUEST['cropx']))?$_REQUEST['cropx']:0); | |
define('CROP_Y', (isset($_REQUEST['cropy']))?$_REQUEST['cropy']:0); | |
define('REDUCE_ONLY', (isset($_REQUEST['reduce_only']))?$_REQUEST['reduce_only']:1); | |
# Pega onde está a imagem | |
$temp=explode('/',$_REQUEST['src']); | |
$image_file = array_pop($temp); | |
$image_path = implode('/',$temp); | |
$image_path=caminhoAtivo().$image_path.(($image_path=='')?'':'/'); | |
# Carrega a imagem | |
if (!file_exists($image_path.$image_file)) $image_file=''; | |
switch (strtolower(end(explode('.',$image_file)))) { | |
case 'jpg': | |
$src = @imagecreatefromjpeg($image_path.$image_file); | |
mostraImg('imagejpeg',$src,MAX_WIDTH,MAX_HEIGHT); | |
break; | |
case 'png': | |
$src = @imagecreatefrompng($image_path.$image_file); | |
mostraImg('imagepng',$src,MAX_WIDTH,MAX_HEIGHT); | |
break; | |
case 'gif': | |
$src = @imagecreatefromgif($image_path.$image_file); | |
mostraImg('imagegif',$src,MAX_WIDTH,MAX_HEIGHT); | |
break; | |
case 'bmp': | |
$src = @imagecreatefromwbmp ($image_path.$image_file); | |
mostraImg('imagewbmp',$src,MAX_WIDTH,MAX_HEIGHT); | |
break; | |
default: | |
$src = imagecreate(160, 120); | |
imagecolorallocate($src, 204, 204, 204); | |
$c = imagecolorallocate($src, 153, 153, 153); | |
$c1 = imagecolorallocate($src, 0, 0, 0); | |
imageline($src, 0, 0, 160, 120, $c); | |
imageline($src, 160, 0, 0, 120, $c); | |
imagestring($src, 5, 60, 20, "ERRO:", $c1); | |
imagestring($src, 5, 65, 50, "Sem", $c1); | |
imagestring($src, 5, 50, 70, "Imagem", $c1); | |
mostraImg('imagejpeg',$src,MAX_WIDTH,MAX_HEIGHT); | |
} | |
function tamanhoImg($src,$w,$h){ | |
// Pega o tamanho da imagem e proporção de resize | |
$width = imagesx($src); | |
$height = imagesy($src); | |
$scale = min($w/$width, $h/$height); | |
// Se a imagem é maior que o permitido, encolhe ela! | |
$new_width=$width; | |
$new_height=$height; | |
if ($scale < 1) | |
{ | |
$new_width = floor($scale * $width); | |
$new_height = floor($scale * $height); | |
} | |
if(REDUCE_ONLY==0){$new_width=MAX_WIDTH;$new_height=MAX_HEIGHT;} | |
// Cria uma imagem temporária | |
$tmp_img = imagecreatetruecolor(min($new_width,CROP_WIDTH), min($new_height,CROP_HEIGHT)); | |
// Copia e resize a imagem velha na nova | |
imagecopyresampled($tmp_img, $src, 0, 0, CROP_X, CROP_Y, $new_width, $new_height, $width, $height); | |
imagedestroy($src); | |
$src = $tmp_img; | |
return $src; | |
} | |
function mostraImg($funcao,$src,$w,$h){ | |
if (!function_exists($funcao)) $funcao='imagejpeg'; | |
switch ($funcao) { | |
case 'imagejpeg': | |
header('Content-type: image/jpeg'); | |
imagejpeg(tamanhoImg($src,$w,$h)); | |
break; | |
case 'imagegif': | |
header('Content-type: image/gif'); | |
imagegif(tamanhoImg($src,$w,$h)); | |
break; | |
case 'imagepng': | |
header('Content-type: image/png'); | |
imagepng(tamanhoImg($src,$w,$h)); | |
break; | |
case 'imagewbmp': | |
header('Content-type: image/vnd.wap.wbmp'); | |
imagewbmp(tamanhoImg($src,$w,$h)); | |
break; | |
} | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment