Estou fazendo uma classe de manipulação de imagem...
Só que está dando um problema, se eu coloco essas linhas no final dessa classe, ocorre tudo bem, conforme o que planegei:
$imagem = new Thumbnail('caminho/da/imagem');
$imagem->Mirror();
$imagem->Show();
mas, se coloco essas mesmas linhas num outro arquivo, e dou um include 'thumb.php' no início não dá certo...
arquivo teste.php - instancia um objeto Thumb
include 'thumb.php'
$imagem = new Thumbnail('caminho/da/imagem');
$imagem->Mirror();
$imagem->Show();
alguém sabe onde estou errando?
Desde já agradeço.
arquivo thumb.php - classe Thumbnail
<?PHP
define("MIRROR_HORIZONTAL", 1);
define("MIRROR_VERTICAL", 2);
define("MIRROR_BOTH", 3);
class Thumbnail {
var $original;
var $thumbnail;
var $textcolor;
/*******************************************************************
Construtor da classe
*******************************************************************/
function Thumbnail($filename) {
//header("Content-Type: image/jpeg");
// cria um thumbnail igual a imagem original
// (caso mostre a imagem, não dá erro)
$this->thumbnail = imagecreatefromjpeg($filename);
// aloca branco para o background
// pelo que li na documentação a primeira cor alocada
// va para o background
$this->SetTextColor(0, 0, 0);
// cria um resource da imagem original
if ($this->original = imagecreatefromjpeg($filename)) {
return 1;
} else {
return 0;
}
}
/*******************************************************************
Faz um espelhamento com a imagem
$tipe define se o espelhamento será horizontal, vertical ou ambos
*******************************************************************/
function Mirror($type = MIRROR_HORIZONTAL) {
$imgsrc = $this->thumbnail;
$width = imagesx($imgsrc);
$height = imagesy($imgsrc);
$imgdest = imagecreate($width, $height);
for ($x=0; $x<$width; $x++) {
for ($y=0; $y<$height; $y++) {
if ($type == MIRROR_HORIZONTAL) imagecopy($imgdest, $imgsrc, $width-$x-1, $y, $x, $y, 1, 1);
if ($type == MIRROR_VERTICAL) imagecopy($imgdest, $imgsrc, $x, $height-$y-1, $x, $y, 1, 1);
if ($type == MIRROR_BOTH) imagecopy($imgdest, $imgsrc, $width-$x-1, $height-$y-1, $x, $y, 1, 1);
}
}
$this->thumbnail = $imgdest;
imagedestroy($imgsrc);
}
/*******************************************************************
Cria uma outra imagem com as cordenadas $x, $y e largura e altura
$w, $h
*******************************************************************/
function SplitImage($x, $y, $w, $h) {
$src = $this->thumbnail;
//$dest = ImageCreateTrueColor($x, $y
}
/*******************************************************************
Modifica a largura somente
*******************************************************************/
function SetWidth($width) {
$img = $this->thumbnail;
$this->thumbnail = imagecreate($width, ImageSY($img));
imagecopyresized($this->thumbnail, $img, 0, 0, 0, 0, $width, ImageSY($img), ImageSX($img), ImageSY($img));
}
/*******************************************************************
Modifica a altura somente
*******************************************************************/
function SetHeight($height) {
$img = $this->thumbnail;
$this->thumbnail = imagecreate(ImageSX($img), $height);
imagecopyresized($this->thumbnail, $img, 0, 0, 0, 0, ImageSX($img), $height, ImageSX($img), ImageSY($img));
}
/*******************************************************************
Cria um thumbnail com uma largura aproximada do valor passado
e modifica também a altura a partir da imagem original
*******************************************************************/
function PWidth($width) {
$div_x = ImageSX($this->original) / $width;
if ($div_x < 1) $div_x = 1;
$thumb_x = floor(ImageSX($this->original) / $div_x);
$thumb_y = floor(ImageSY($this->original) / $div_x);
$this->thumbnail = imagecreate($thumb_x, $thumb_y);
imagecopyresized($this->thumbnail, $this->original, 0, 0, 0, 0, $thumb_x, $thumb_y, ImageSX($this->original),ImageSY($this->original));
}
/*******************************************************************
Cria um thumbnail com uma altura aproximada do valor passado
e modifica também a largura a partir da imagem original
*******************************************************************/
function PHeight($height) {
$div_y = ImageSY($this->original) / $height;
if ($div_y < 1) $div_y = 1;
$thumb_x = floor(ImageSX($this->original) / $div_y);
$thumb_y = floor(ImageSY($this->original) / $div_y);
$this->thumbnail = imagecreate($thumb_x, $thumb_y);
imagecopyresized($this->thumbnail, $this->original, 0, 0, 0, 0, $thumb_x, $thumb_y,
ImageSX($this->original),ImageSY($this->original));
}
/*******************************************************************
Mostra a imagem no navegador
*******************************************************************/
function SetTextColor($r = 0, $g = 0, $b = 0) {
$cor = imagecolorallocate($this->thumbnail, $r, $g, $b);
if ($cor == -1) {
$cor = ImageColorClosest($this->thumbnail, $r, $g, $b);
}
$this->textcolor = $cor;
}
/*******************************************************************
Mostra a imagem no navegador
*******************************************************************/
function SetText($text, $size = 3, $x = 20, $y = 20) { // size de 1 a 5
// escreve o texto na imagem
imagestring($this->thumbnail, $size, $x, $y, $text, $this->textcolor);
}
/*******************************************************************
Volta a imagem original
*******************************************************************/
function OriginalImage() {
$this->thumbnail = $this->original;
}
/*******************************************************************
Mostra a imagem no navegador
*******************************************************************/
function Show() {
//header("Content-Type: image/jpeg");
imagejpeg($this->thumbnail);
}
/*******************************************************************
Salva imagem
*******************************************************************/
function Save($dest) {
header("Content-Type: image/jpeg");
imagejpeg($this->thumbnail, $dest);
}
/*******************************************************************
Destrói a imagem criada
*******************************************************************/
function Destroy() {
imagedestroy($this->thumbnail);
}
} // fecha class
?>
Pergunta
griphon
bom,
Estou fazendo uma classe de manipulação de imagem...
Só que está dando um problema, se eu coloco essas linhas no final dessa classe, ocorre tudo bem, conforme o que planegei:
$imagem = new Thumbnail('caminho/da/imagem'); $imagem->Mirror(); $imagem->Show();mas, se coloco essas mesmas linhas num outro arquivo, e dou um include 'thumb.php' no início não dá certo... arquivo teste.php - instancia um objeto Thumbinclude 'thumb.php' $imagem = new Thumbnail('caminho/da/imagem'); $imagem->Mirror(); $imagem->Show();alguém sabe onde estou errando? Desde já agradeço. arquivo thumb.php - classe Thumbnail<?PHP define("MIRROR_HORIZONTAL", 1); define("MIRROR_VERTICAL", 2); define("MIRROR_BOTH", 3); class Thumbnail { var $original; var $thumbnail; var $textcolor; /******************************************************************* Construtor da classe *******************************************************************/ function Thumbnail($filename) { //header("Content-Type: image/jpeg"); // cria um thumbnail igual a imagem original // (caso mostre a imagem, não dá erro) $this->thumbnail = imagecreatefromjpeg($filename); // aloca branco para o background // pelo que li na documentação a primeira cor alocada // va para o background $this->SetTextColor(0, 0, 0); // cria um resource da imagem original if ($this->original = imagecreatefromjpeg($filename)) { return 1; } else { return 0; } } /******************************************************************* Faz um espelhamento com a imagem $tipe define se o espelhamento será horizontal, vertical ou ambos *******************************************************************/ function Mirror($type = MIRROR_HORIZONTAL) { $imgsrc = $this->thumbnail; $width = imagesx($imgsrc); $height = imagesy($imgsrc); $imgdest = imagecreate($width, $height); for ($x=0; $x<$width; $x++) { for ($y=0; $y<$height; $y++) { if ($type == MIRROR_HORIZONTAL) imagecopy($imgdest, $imgsrc, $width-$x-1, $y, $x, $y, 1, 1); if ($type == MIRROR_VERTICAL) imagecopy($imgdest, $imgsrc, $x, $height-$y-1, $x, $y, 1, 1); if ($type == MIRROR_BOTH) imagecopy($imgdest, $imgsrc, $width-$x-1, $height-$y-1, $x, $y, 1, 1); } } $this->thumbnail = $imgdest; imagedestroy($imgsrc); } /******************************************************************* Cria uma outra imagem com as cordenadas $x, $y e largura e altura $w, $h *******************************************************************/ function SplitImage($x, $y, $w, $h) { $src = $this->thumbnail; //$dest = ImageCreateTrueColor($x, $y } /******************************************************************* Modifica a largura somente *******************************************************************/ function SetWidth($width) { $img = $this->thumbnail; $this->thumbnail = imagecreate($width, ImageSY($img)); imagecopyresized($this->thumbnail, $img, 0, 0, 0, 0, $width, ImageSY($img), ImageSX($img), ImageSY($img)); } /******************************************************************* Modifica a altura somente *******************************************************************/ function SetHeight($height) { $img = $this->thumbnail; $this->thumbnail = imagecreate(ImageSX($img), $height); imagecopyresized($this->thumbnail, $img, 0, 0, 0, 0, ImageSX($img), $height, ImageSX($img), ImageSY($img)); } /******************************************************************* Cria um thumbnail com uma largura aproximada do valor passado e modifica também a altura a partir da imagem original *******************************************************************/ function PWidth($width) { $div_x = ImageSX($this->original) / $width; if ($div_x < 1) $div_x = 1; $thumb_x = floor(ImageSX($this->original) / $div_x); $thumb_y = floor(ImageSY($this->original) / $div_x); $this->thumbnail = imagecreate($thumb_x, $thumb_y); imagecopyresized($this->thumbnail, $this->original, 0, 0, 0, 0, $thumb_x, $thumb_y, ImageSX($this->original),ImageSY($this->original)); } /******************************************************************* Cria um thumbnail com uma altura aproximada do valor passado e modifica também a largura a partir da imagem original *******************************************************************/ function PHeight($height) { $div_y = ImageSY($this->original) / $height; if ($div_y < 1) $div_y = 1; $thumb_x = floor(ImageSX($this->original) / $div_y); $thumb_y = floor(ImageSY($this->original) / $div_y); $this->thumbnail = imagecreate($thumb_x, $thumb_y); imagecopyresized($this->thumbnail, $this->original, 0, 0, 0, 0, $thumb_x, $thumb_y, ImageSX($this->original),ImageSY($this->original)); } /******************************************************************* Mostra a imagem no navegador *******************************************************************/ function SetTextColor($r = 0, $g = 0, $b = 0) { $cor = imagecolorallocate($this->thumbnail, $r, $g, $b); if ($cor == -1) { $cor = ImageColorClosest($this->thumbnail, $r, $g, $b); } $this->textcolor = $cor; } /******************************************************************* Mostra a imagem no navegador *******************************************************************/ function SetText($text, $size = 3, $x = 20, $y = 20) { // size de 1 a 5 // escreve o texto na imagem imagestring($this->thumbnail, $size, $x, $y, $text, $this->textcolor); } /******************************************************************* Volta a imagem original *******************************************************************/ function OriginalImage() { $this->thumbnail = $this->original; } /******************************************************************* Mostra a imagem no navegador *******************************************************************/ function Show() { //header("Content-Type: image/jpeg"); imagejpeg($this->thumbnail); } /******************************************************************* Salva imagem *******************************************************************/ function Save($dest) { header("Content-Type: image/jpeg"); imagejpeg($this->thumbnail, $dest); } /******************************************************************* Destrói a imagem criada *******************************************************************/ function Destroy() { imagedestroy($this->thumbnail); } } // fecha class ?>Link para o comentário
Compartilhar em outros sites
5 respostass a esta questão
Posts Recomendados
Participe da discussão
Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.