transparency - Convert to alpha using PHP GD -
i'm trying achieve close fireworks using convert alpha filter (see http://erskinedesign.com/blog/fireworks-tips-convert-alpha/). possible using php gd functions?
my code looks like:
$img = imagecreatefromstring(file_get_contents('...')); imagealphablending($img, true); $transparentcolour = imagecolorallocate($img, 255,255,255); imagecolortransparent($img, $transparentcolour); imagefilter($img, img_filter_grayscale); $w = imagesx($img); $h = imagesy($img); ($x=0; $x<$w; $x++) { ($y=0; $y<$h; $y++) { $color = imagecolorsforindex($img, imagecolorat($img, $x, $y)); if ($color['alpha'] == 0) continue; } } imagepng($img); exit;
my idea convert grayscale, measure how 'dark' pixel is, convert black alpha, seem confusing myself.
curiously looking same thing when found question. ended building function, think works more less way want it.
function n2_image_make_alpha( $im , $percentage ) { imagefilter( $im , img_filter_grayscale ); $width = imagesx( $im ); $height = imagesy( $im ); imagealphablending( $im , false ); imagesavealpha( $im , true ); $newim = imagecreatetruecolor( $width , $height ); imagealphablending( $newim , false ); imagesavealpha( $newim , true ); //loop through pixels ( $x = 0 ; $x < $width ; $x++ ) { ( $y = 0 ; $y < $height ; $y++ ) { //get color of pixel $color = imagecolorat( $im , $x , $y ); //get rgba of color $rgba = imagecolorsforindex( $im , $color ); $alpha = $rgba['alpha']; if ( $alpha < 127 ) { $base_alpha = 127 - $alpha; //100% of difference between transparent , current alpha $percentage_to_increase_alpha = intval( $percentage * $base_alpha / 100 ); $alpha = $alpha + $percentage_to_increase_alpha; } $new_color = imagecolorallocatealpha ( $newim , $rgba['red'] , $rgba['green'] , $rgba['blue'] , $alpha ); imagesetpixel ( $newim , $x , $y , $new_color ); } } return $newim; }
Comments
Post a Comment