Programação
PHP - Converter imagens PNG para formato JPG
//---------------------------------------------------------------------------------------------------------- /** * * converte uma imagem png para jpg * * usa uma função auxiliar chamada HEX2RGB */ function png2jpg( $imagemPng2Jpg ) { $color = "#ffffff"; $sourceFile = 'files/'.$imagemPng2Jpg; list( $sx , $sy ) = getimagesize( $sourceFile ); // Create a bg image from the color and image size variables $bg_image = imagecreatetruecolor( $sx , $sy ); list( $R , $G , $B ) = ( HEX2RGB( $color ) ); $mycolor = ImageColorAllocate( $bg_image , $R , $G , $B ); ImageFill( $bg_image , 0 , 0 , $mycolor ); //Create the PNG image from the $url variable $image = imagecreatefrompng( $sourceFile ); imageAlphaBlending( $image , true ); imageSaveAlpha( $image , true ); //Resample the merging of the background image and the original PNG file imagecopyresampled( $bg_image , $image , 0 , 0 , 0 , 0 , $sx , $sy , $sx , $sy ); // Send out as JPG $novoNome = str_ireplace( 'png' , 'jpg' , $sourceFile ); //mixed str_ireplace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] ) imagejpeg( $bg_image , $novoNome , 75 ); //Remove bg image and PNG from memory imagedestroy( $bg_image ); imagedestroy( $image ); return $novoNome; } function HEX2RGB($color) { $color_array = array(); $hex_color = strtoupper($color); for($i = 0; $i < 6; $i++) { $hex = substr($hex_color,$i,1); switch($hex){ case "A": $num = 10; break; case "B": $num = 11; break; case "C": $num = 12; break; case "D": $num = 13; break; case "E": $num = 14; break; case "F": $num = 15; break; default: $num = $hex; break; } array_push( $color_array , $num ); } $R = (($color_array[0] * 16) + $color_array[1]); $G = (($color_array[2] * 16) + $color_array[3]); $B = (($color_array[4] * 16) + $color_array[5]); return array( $R , $G , $B ); unset( $color_array , $hex , $R , $G , $B ); } //png2jpg('image.png'); //FIM //----------------------------------------------------------------------------------------------------------