ruimcosta 0 Posted April 1, 2011 Report Share Posted April 1, 2011 Biba, Cá vai mais uma questão para 10 pontos. Tenho uma classe (não foi feita por mim) para redimensionar imagens gif, jpg e png. O problema é que ao redimensionar um png's que tenha transparência, aplica à transparência um fundo preto. <?php class SimpleImage { var $image; var $image_type; function load($filename) { $image_info = getimagesize($filename); $this->image_type = $image_info[2]; if( $this->image_type == IMAGETYPE_JPEG ) { $this->image = imagecreatefromjpeg($filename); } elseif( $this->image_type == IMAGETYPE_GIF ) { $this->image = imagecreatefromgif($filename); } elseif( $this->image_type == IMAGETYPE_PNG ) { $this->image = imagecreatefrompng($filename); } } function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) { if( $image_type == IMAGETYPE_JPEG ) { imagejpeg($this->image,$filename,$compression); } elseif( $image_type == IMAGETYPE_GIF ) { imagegif($this->image,$filename); } elseif( $image_type == IMAGETYPE_PNG ) { imagepng($this->image,$filename); } if( $permissions != null) { chmod($filename,$permissions); } } function output($image_type=IMAGETYPE_JPEG) { if( $image_type == IMAGETYPE_JPEG ) { imagejpeg($this->image); } elseif( $image_type == IMAGETYPE_GIF ) { imagegif($this->image); } elseif( $image_type == IMAGETYPE_PNG ) { imagepng($this->image); } } function getWidth() { return imagesx($this->image); } function getHeight() { return imagesy($this->image); } function resizeToHeight($height) { $ratio = $height / $this->getHeight(); $width = $this->getWidth() * $ratio; $this->resize($width,$height); } function resizeToWidth($width) { $ratio = $width / $this->getWidth(); $height = $this->getheight() * $ratio; $this->resize($width,$height); } function scale($scale) { $width = $this->getWidth() * $scale/100; $height = $this->getheight() * $scale/100; $this->resize($width,$height); } function resize($width,$height) { $new_image = imagecreatetruecolor($width, $height); imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight()); $this->image = $new_image; } } ?> Já vasculhei o google em busca de soluções, mas nenhuma das sugeridas, resolveram o meu problema. Alguém já conseguiu ultrapassar esta dificuldade? Abraços e beijinhos,Rui Costa Link to post Share on other sites
Rodrigo Graça 0 Posted April 2, 2011 Report Share Posted April 2, 2011 Ainda não percebo muito de classes mas vou tentar ajudar Na função "save" diz "$image_type=IMAGETYPE_JPEG" ou seja diz que a variavel "$image_type" vai guardar o tipo de imagem "IMAGETYPE_JPEG" e depois vai verificar qual tipo de imagem que a imagem tem e salva ou seja o que me parece é que ele diz logo que é JPEG e depois ao verificar vai sempre para o JPEG e vai ser salvo como JPEG e assim perderá a transparencia não será isso? Como referi não percebo muito de classes apenas o básico para me orientar. Sem outro assunto os meus cordiais cumprimentos. Link to post Share on other sites
yoda 126 Posted April 2, 2011 Report Share Posted April 2, 2011 Como estás a usar a classe? Parece-me bastante básica .. before you post, what have you tried? - http://filipematias.info sense, purpose, direction Link to post Share on other sites
Lfscoutinho 2 Posted April 3, 2011 Report Share Posted April 3, 2011 Boas, Muita vezes o problema é são saber pesquisar e utilizar o manual do PHP correctamente. Aqui tens a solução que também estou a utilizar numa classe que estou a criar. <?php /** * PNG ALPHA CHANNEL SUPPORT for imagecopymerge(); * This is a function like imagecopymerge but it handle alpha channel well!!! **/ // A fix to get a function like imagecopymerge WITH ALPHA SUPPORT // Main script by aiden dot mail at freemail dot hu // Transformed to imagecopymerge_alpha() by rodrigo dot polo at gmail dot com function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){ if(!isset($pct)){ return false; } $pct /= 100; // Get image width and height $w = imagesx( $src_im ); $h = imagesy( $src_im ); // Turn alpha blending off imagealphablending( $src_im, false ); // Find the most opaque pixel in the image (the one with the smallest alpha value) $minalpha = 127; for( $x = 0; $x < $w; $x++ ) for( $y = 0; $y < $h; $y++ ){ $alpha = ( imagecolorat( $src_im, $x, $y ) >> 24 ) & 0xFF; if( $alpha < $minalpha ){ $minalpha = $alpha; } } //loop through image pixels and modify alpha for each for( $x = 0; $x < $w; $x++ ){ for( $y = 0; $y < $h; $y++ ){ //get current alpha value (represents the TANSPARENCY!) $colorxy = imagecolorat( $src_im, $x, $y ); $alpha = ( $colorxy >> 24 ) & 0xFF; //calculate new alpha if( $minalpha !== 127 ){ $alpha = 127 + 127 * $pct * ( $alpha - 127 ) / ( 127 - $minalpha ); } else { $alpha += 127 * $pct; } //get the color index with new alpha $alphacolorxy = imagecolorallocatealpha( $src_im, ( $colorxy >> 16 ) & 0xFF, ( $colorxy >> 8 ) & 0xFF, $colorxy & 0xFF, $alpha ); //set pixel with the new color + opacity if( !imagesetpixel( $src_im, $x, $y, $alphacolorxy ) ){ return false; } } } // The image copy imagecopy($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h); } // USAGE EXAMPLE: $img_a = imagecreatefrompng('image1.png'); $img_b = imagecreatefrompng('wm2.png'); // SAME COMMANDS: imagecopymerge_alpha($img_a, $img_b, 10, 10, 0, 0, imagesx($img_b), imagesy($img_b),50); // OUTPUT IMAGE: header("Content-Type: image/png"); imagesavealpha($img_a, true); imagepng($img_a, NULL); ?> Link to post Share on other sites
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now