AyrA

Image Anonymizer in PHP

Dec 14th, 2020
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.71 KB | None | 0 0
  1. <?php
  2.     define('ANONYMIZE_PNG',1);
  3.     define('ANONYMIZE_JPG',ANONYMIZE_PNG+1);
  4.     define('ANONYMIZE_GIF',ANONYMIZE_JPG+1);
  5.    
  6.     //Anonymizes an image by making all color channel values even.
  7.     //It also removes color information from regions that are almost or completely transparent.
  8.     //This defeats steganography in the least significant bits of color channels and steganography
  9.     //that uses the color bytes of completely transparent pixels.
  10.     //
  11.     //Note: Will kill animation in gif files
  12.     //
  13.     //$data(string):               Image data
  14.     //$format(int=ANONYMIZE_PNG):  Output a jpeg instead of PNG. Recommended if the source is jpeg
  15.     //returns:                     Anonymized image data
  16.     function anonymize_image($data,$format=ANONYMIZE_PNG){
  17.         //Default to PNG if the programmer that calls this function is stupid (or a smartass)
  18.         if(!in_array($format,array(ANONYMIZE_PNG,ANONYMIZE_JPG,ANONYMIZE_GIF))){
  19.             $format=ANONYMIZE_PNG;
  20.         }
  21.        
  22.         //Read as image
  23.         $img=@imageCreateFromString($data);
  24.         if(!$img){
  25.             //bail if we failed to load
  26.             return NULL;
  27.         }
  28.         //If we don't do this, PHP will read alpha values, but discards them when writing
  29.         imageSaveAlpha($img,TRUE);
  30.        
  31.         $colors=imageColorsTotal($img);
  32.         if($colors>0){
  33.             //Palette based images are much easier to deal with.
  34.             //We don't set the pixel values, we merely tweak the values in the palette
  35.             //This doesn't makes sense with jpg, so we use GIF instead.
  36.             if($format===ANONYMIZE_JPG){
  37.                 $format=ANONYMIZE_GIF;
  38.             }
  39.             for($i=0;$i<$colors;$i++){
  40.                 $col=imageColorsForIndex($img,$i);
  41.                 if($col['alpha']>0x70){
  42.                     //Make almost completely transparent regions fully transparent
  43.                     imageColorSet($img,$i,0xFF,0xFF,0xFF,0x7F);
  44.                 }
  45.                 elseif($col['alpha']<0x8){
  46.                     //Make almost completely opaque regions fully opaque
  47.                     imageColorSet($img,$i,$col['red']&0xFE,$col['green']&0xFE,$col['blue']&0xFE,0x00);
  48.                 }
  49.                 else{
  50.                     //Make colors even
  51.                     imageColorSet($img,$i,$col['red']&0xFE,$col['green']&0xFE,$col['blue']&0xFE,$col['alpha']&0xFE);
  52.                 }
  53.             }
  54.         }
  55.         else{
  56.             $w=imageSX($img);
  57.             $h=imageSY($img);
  58.             for($x=0;$x<$w;$x++){
  59.                 for($y=0;$y<$h;$y++){
  60.                     //Get color and make RGB even but don't touch alpha channel yet
  61.                     $c=imageColorAt($img,$x,$y)&0xFFFEFEFE;
  62.                     //Format of $c is 0xAARRGGBB
  63.                     //The alpha component can only range from 0x00 (opaque) to 0x7F (transparent) because this is PHP
  64.                     if($c>0xFFFFFF){
  65.                         //This pixel has alpha information.
  66.                         if(($c>>24)>=0x70){
  67.                             //Strip all color information from almost/completely transparent region
  68.                             $c=0x7FFFFFFF;
  69.                         }
  70.                         elseif(($c>>24)<=0x8){
  71.                             //Make fully opaque
  72.                             $c&=0x00FFFFFF;
  73.                         }
  74.                     }
  75.                     imageSetPixel($img,$x,$y,$c);
  76.                 }
  77.             }
  78.         }
  79.         //Return new image data
  80.         ob_start();
  81.         switch($format){
  82.             case ANONYMIZE_JPG:
  83.                 //75 is the default quality. Higher is better
  84.                 imageJpeg($img,NULL,90);
  85.                 break;
  86.             case ANONYMIZE_GIF:
  87.                 //This has no options
  88.                 imageGif($img);
  89.                 break;
  90.             default:
  91.                 //6 is the default compression. Higher is better
  92.                 imagePng($img,NULL,9);
  93.                 break;
  94.         }
  95.         imageDestroy($img);
  96.         return ob_get_clean();
  97.     }
  98.     //Replace the file with one of your own for this demo
  99.     $raw=file_get_contents('F:/test.png');
  100.     $png=anonymize_image($raw,ANONYMIZE_PNG);
  101.     $jpg=anonymize_image($raw,ANONYMIZE_JPG);
  102.     $gif=anonymize_image($raw,ANONYMIZE_GIF);
  103. ?>
  104. <style>*{font-family:sans-serif;}img{border:2px solid #000;margin:0.5em;padding:0.5em;display:block;}</style>
  105. <p>PNG</p>
  106. <img src="data:image/png;base64,<?=base64_encode($png);?>" />
  107. <p>JPG</p>
  108. <img src="data:image/jpg;base64,<?=base64_encode($jpg);?>" />
  109. <p>GIF</p>
  110. <img src="data:image/gif;base64,<?=base64_encode($gif);?>" />
Add Comment
Please, Sign In to add comment