Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Essa classe mantém a transparência de gif e pngs
<?php
/**
* Classe para gerar thumbs.
* @version 1.0
* @author Wagner Brandão Soares
*/
class Thumb
{
public function geraImagem($strNomeImagem,$_width,$_height)
{
# Pega onde está a imagem
$image_file = "imagens/".$strNomeImagem;
$image_path = $image_file;
# Carrega a imagem
$img = null;
$extensao = strtolower(end(explode('.',$image_path)));
if ($extensao == 'jpg' || $extensao == 'jpeg') {
$img = @imagecreatefromjpeg($image_path);
} else if ($extensao == 'png') {
$img = @imagecreatefrompng($image_path);
// Se a versão do GD incluir suporte a GIF, mostra...
} else if ($extensao == 'gif') {
$img = @imagecreatefromgif($image_path);
}
// 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($_width/$width, $_height/$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);
if($extensao == 'gif' || $extensao == 'png')
{
// If we have a specific transparent color
if ($extensao == 'gif')
{
$trnprt_indx = imagecolortransparent($img);
// Get the original image's transparent color's RGB values
$trnprt_color = imagecolorsforindex($img, $trnprt_indx);
// Allocate the same color in the new image resource
$trnprt_indx = imagecolorallocate($tmp_img, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']);
// Completely fill the background of the new image with allocated color.
imagefill($tmp_img, 0, 0, $trnprt_indx);
// Set the background color for new image to transparent
imagecolortransparent($tmp_img, $trnprt_indx);
}
else if ($extensao == 'png')
{
// Turn off transparency blending (temporarily)
imagealphablending($tmp_img, false);
// Create a new transparent color for image
$color = imagecolorallocatealpha($tmp_img, 0, 0, 0, 127);
// Completely fill the background of the new image with allocated color.
imagefill($tmp_img, 0, 0, $color);
// Restore transparency blending
imagesavealpha($tmp_img, true);
}
}
// 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;
}
}
// Cria uma imagem de erro se necessário
if (!$img) {
$img = imagecreate($_width, $_height);
imagecolorallocate($img, 204, 204, 204);
$c = imagecolorallocate($img, 153, 153, 153);
$c1 = imagecolorallocate($img, 0, 0, 0);
imageline($img, 0, 0, $_width, $_height, $c);
imageline($img, $_width, 0, 0, $_height, $c);
imagestring($img, 2, 35, 35, 'Imagem', $c1);
imagestring($img, 2, 20, 55, 'Indisponível', $c1);
}
// Mostra a imagem
if($extensao == 'gif')
{
header('Content-type: image/gif');
imagegif($img,"",80);
}
if($extensao == 'png')
{
header('Content-type: image/png');
imagepng($img,"",80);
}
if($extensao == 'jpg')
{
header('Content-type: image/jpeg');
imagejpeg($img,"",80);
}
}
}
$nomeImagem = $_GET['imagem'];
$wt = $_GET['wt'];
$ht = $_GET['ht'];
$imagem = new Thumb();
$imagem->geraImagem($nomeImagem,$wt,$ht);
?>
como usar <img src="thumb.php?imagem=nome_da_imagem.jpg&wt=100&ht=100"> onde wt é width e ht heightCarregando comentários...