0){ //Palette based images are much easier to deal with. //We don't set the pixel values, we merely tweak the values in the palette //This doesn't makes sense with jpg, so we use GIF instead. if($format===ANONYMIZE_JPG){ $format=ANONYMIZE_GIF; } for($i=0;$i<$colors;$i++){ $col=imageColorsForIndex($img,$i); if($col['alpha']>0x70){ //Make almost completely transparent regions fully transparent imageColorSet($img,$i,0xFF,0xFF,0xFF,0x7F); } elseif($col['alpha']<0x8){ //Make almost completely opaque regions fully opaque imageColorSet($img,$i,$col['red']&0xFE,$col['green']&0xFE,$col['blue']&0xFE,0x00); } else{ //Make colors even imageColorSet($img,$i,$col['red']&0xFE,$col['green']&0xFE,$col['blue']&0xFE,$col['alpha']&0xFE); } } } else{ $w=imageSX($img); $h=imageSY($img); for($x=0;$x<$w;$x++){ for($y=0;$y<$h;$y++){ //Get color and make RGB even but don't touch alpha channel yet $c=imageColorAt($img,$x,$y)&0xFFFEFEFE; //Format of $c is 0xAARRGGBB //The alpha component can only range from 0x00 (opaque) to 0x7F (transparent) because this is PHP if($c>0xFFFFFF){ //This pixel has alpha information. if(($c>>24)>=0x70){ //Strip all color information from almost/completely transparent region $c=0x7FFFFFFF; } elseif(($c>>24)<=0x8){ //Make fully opaque $c&=0x00FFFFFF; } } imageSetPixel($img,$x,$y,$c); } } } //Return new image data ob_start(); switch($format){ case ANONYMIZE_JPG: //75 is the default quality. Higher is better imageJpeg($img,NULL,90); break; case ANONYMIZE_GIF: //This has no options imageGif($img); break; default: //6 is the default compression. Higher is better imagePng($img,NULL,9); break; } imageDestroy($img); return ob_get_clean(); } //Replace the file with one of your own for this demo $raw=file_get_contents('F:/test.png'); $png=anonymize_image($raw,ANONYMIZE_PNG); $jpg=anonymize_image($raw,ANONYMIZE_JPG); $gif=anonymize_image($raw,ANONYMIZE_GIF); ?>
PNG
JPG
GIF