Tratamento de imagem com php, gerar miniatura de uma imagem.

<?

class EasyThumbnail

{

private $debug= false;

private $errflag= false;

private $ext;

private $origem;

private $destino;

private $errormsg;

function __construct($imagem, $destino, $aprox)

{

// se o arquivo não existir, erro

if (!file_exists($imagem))

{

$this->errormsg= “Arquivo não encontrado.”;

return false;

}

else

{

$this->origem= $imagem;

$this->destino= $destino;

}

// obtém a extensão do arquivo

if (!$this->ext= $this->getExtension($imagem))

{

$this->errormsg= “Tipo de arquivo inválido.”;

return false;

}

// gera a imagem do thumbnail com o caminho e nome do arquivo especificados

$this->createThumbImg($aprox);

}

// retorna as dimensões (x,y) do thumbnail a ser gerado

public function getThumbXY($x, $y, $aprox)

{

if ($x >= $y)

{

if ($x > $aprox)

{

$x1= (int)($x * ($aprox/$x));

$y1= (int)($y * ($aprox/$x));

}

else

{

$x1= $x;

$y1= $y;

}

}

else

{

if ($y > $aprox)

{

$x1= (int)($x * ($aprox/$y));

$y1= (int)($y * ($aprox/$y));

}

// Caso a imagem seja menor do que

// deve ser aproximado, mantém tamanho original para o thumb.

else

{

$x1= $x;

$y1= $y;

}

}

$vet= array(“x” => $x1, “y” => $y1);

return $vet;

}

// cria a imagem do thumbnail

private function createThumbImg($aprox)

{

// imagem de origem

$img_origem= $this->createImg();

// obtém as dimensões da imagem original

$origem_x= ImagesX($img_origem);

$origem_y= ImagesY($img_origem);

// obtém as dimensões do thumbnail

$vetor= $this->getThumbXY($origem_x, $origem_y, $aprox);

$x= $vetor['x'];

$y= $vetor['y'];

// cria a imagem do thumbnail

$img_final = ImageCreateTrueColor($x, $y);

ImageCopyResampled($img_final, $img_origem, 0, 0, 0, 0, $x+1, $y+1, $origem_x, $origem_y);

// o arquivo é gravado

if ($this->ext == “png”)

imagepng($img_final, $this->destino);

elseif ($this->ext == “jpg”)

imagejpeg($img_final, $this->destino);

}

// cria uma imagem a partir do arquivo de origem

private function createImg()

{

// imagem de origem

if ($this->ext == “png”)

$img_origem= imagecreatefrompng($this->origem);

elseif ($this->ext == “jpg” || $this->ext == “jpeg”)

$img_origem= imagecreatefromjpeg($this->origem);

return $img_origem;

}

// obtém a extensão do arquivo

private function getExtension($imagem)

{

// isso é para obter o mime-type da imagem.

$mime= getimagesize($imagem);

if ($mime[2] == 2)

{

$ext= “jpg”;

return $ext;

}

else

if ($mime[2] == 3)

{

$ext= “png”;

return $ext;

}

else

return false;

}

// mensagem de erro

public function getErrorMsg()

{

return $this->errormsg;

}

public function isError()

{

return $this->errflag;

}

}

?>

Como Usar:

<?

$destino =”img”; #diretoria para imagens

new EasyThumbnail($destino.”/”.$upload->getNome(), $destino.”/”.$upload->getNome(), ‘530′);

?>

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • Add to favorites
  • BarraPunto
  • Bitacoras.com
  • BlinkList
  • blogmarks
  • Blogosphere News
  • blogtercimlap
  • connotea
  • Current
  • Design Float
  • Diggita
  • Diigo
  • DotNetKicks
  • DZone
  • eKudos
  • email
  • Fark
  • Faves
  • Fleck
  • FriendFeed
  • FSDaily
  • Global Grind
  • Gwar
  • HackerNews
  • Haohao
  • HealthRanker
  • HelloTxt
  • Hemidemi
  • Hyves
  • Identi.ca
  • IndianPad
  • Internetmedia
  • Kirtsy
  • laaik.it
  • LaTafanera
  • LinkaGoGo
  • LinkArena
  • LinkedIn
  • Linkter
  • Live
  • Meneame
  • MisterWong
  • MisterWong.DE
  • MOB
  • MSN Reporter
  • muti
  • MyShare
  • MySpace
  • N4G
  • Netvibes
  • Netvouz
  • NewsVine
  • NuJIJ
  • PDF
  • Ping.fm
  • Posterous
  • Propeller
  • QQ书签
  • Ratimarks
  • Rec6
  • Reddit
  • RSS
  • Scoopeo
  • Segnalo
  • SheToldMe
  • Simpy
  • Slashdot
  • Socialogs
  • SphereIt
  • StumbleUpon
  • Suggest to Techmeme via Twitter
  • Technorati
  • ThisNext
  • Tipd
  • Tumblr
  • Twitter
  • Upnews
  • viadeo FR
  • Webnews.de
  • Webride
  • Wikio
  • Wikio FR
  • Wikio IT
  • Wists
  • Wykop
  • Xerpi
  • Yahoo! Bookmarks
  • Yahoo! Buzz
  • Yigg
  • 豆瓣
  • 豆瓣九点

0 comments

There are no comments yet...

Kick things off by filling out the form below.

Leave a Comment