Advertisement
Guest User

Guild Emblems

a guest
Jun 4th, 2013
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <?php
  2.     try
  3.     {
  4.         $background = array(brightness=>-18,contrast=>1,hue=>275,saturation=>0.0234375,lightness=>1.09375);
  5.         $primary = array(brightness=>-2,contrast=>1,hue=>135,saturation=>0.546875,lightness=>0.976563);
  6.         $secondary = array(brightness=>4,contrast=>1,hue=>240,saturation=>0.625,lightness=>1.09375);
  7.        
  8.         $image1 = new Imagick("./emblems/58a.png");
  9.         $image2 = new Imagick("./emblems/58b.png");
  10.         $imagebk = new Imagick("./backgrounds/0.png");
  11.        
  12.         // Re-colour Emblem Primary Part
  13.         $it = $image1->getPixelIterator();
  14.         foreach( $it as $row => $pixels )
  15.         {
  16.             foreach ( $pixels as $column => $pixel )
  17.             {
  18.                 $alpha = $pixel->getColorValue(imagick::COLOR_ALPHA);
  19.                 if($alpha>0){
  20.                     $colour = $pixel->getColor();
  21.                     $baseRGB = array($colour['r'],$colour['g'],$colour['b']);
  22.                     $rgb = compositeColorShiftRgb($primary, $baseRGB);
  23.                     $r = $rgb[0];
  24.                     $g = $rgb[1];
  25.                     $b = $rgb[2];
  26.                     $pixel->setColor( "rgba($r,$g,$b,$alpha)" );
  27.                 }
  28.             }
  29.             $it->syncIterator();
  30.         }
  31.        
  32.         // Re-colour Emblem Secondary Part
  33.         $it = $image2->getPixelIterator();
  34.         foreach( $it as $row => $pixels )
  35.         {
  36.             foreach ( $pixels as $column => $pixel )
  37.             {
  38.                 $alpha = $pixel->getColorValue(imagick::COLOR_ALPHA);
  39.                 if($alpha>0){
  40.                     $colour = $pixel->getColor();
  41.                     $baseRGB = array($colour['r'],$colour['g'],$colour['b']);
  42.                     $rgb = compositeColorShiftRgb($secondary, $baseRGB);
  43.                     $r = $rgb[0];
  44.                     $g = $rgb[1];
  45.                     $b = $rgb[2];
  46.                     $pixel->setColor( "rgba($r,$g,$b,$alpha)" );
  47.                 }
  48.             }
  49.             $it->syncIterator();
  50.         }
  51.        
  52.         // Re-colour Emblem Background Part
  53.         $it = $imagebk->getPixelIterator();
  54.         foreach( $it as $row => $pixels )
  55.         {
  56.             foreach ( $pixels as $column => $pixel )
  57.             {
  58.                 $alpha = $pixel->getColorValue(imagick::COLOR_ALPHA);
  59.                 if($alpha>0){
  60.                     $colour = $pixel->getColor();
  61.                     $baseRGB = array($colour['r'],$colour['g'],$colour['b']);
  62.                     $rgb = compositeColorShiftRgb($background, $baseRGB);
  63.                     $r = $rgb[0];
  64.                     $g = $rgb[1];
  65.                     $b = $rgb[2];
  66.                     $pixel->setColor( "rgba($r,$g,$b,$alpha)" );
  67.                 }
  68.             }
  69.             $it->syncIterator();
  70.         }
  71.        
  72.         $image1->compositeImage( $image2, $image2->getImageCompose(), 0, 0 );
  73.         $imagebk->compositeImage( $image1, $image1->getImageCompose(), 0, 0 );
  74.         header("Content-Type: image/png");
  75.         echo $imagebk;
  76.     }
  77.     catch(Exception $e)
  78.     {
  79.         echo $e->getMessage();
  80.     }
  81.    
  82.     // Function matrix_multiply by smiley: https://gist.github.com/codemasher/b869faa7603e1934c28d
  83.     function matrix_multiply($m1, $m2){
  84.         $r = count($m1);
  85.         $c = count($m2[0]);
  86.         $p = count($m2);
  87.         if(count($m1[0]) != $p){
  88.             return false; //incompatible matrix
  89.         }
  90.         $m3 = array();
  91.         for($i = 0; $i < $r; $i++){
  92.             for($j = 0; $j < $c; $j++){
  93.                 $m3[$i][$j] = 0;
  94.                 for($k = 0; $k < $p; $k++){
  95.                     $m3[$i][$j] += $m1[$i][$k]*$m2[$k][$j];
  96.                 }
  97.             }
  98.         }
  99.         return ($m3);
  100.     }
  101.    
  102.    
  103.     // Function compositeColorShiftRgb by smiley: https://gist.github.com/codemasher/b869faa7603e1934c28d
  104.     function compositeColorShiftRgb($hslbc, $base){
  105.         //colors from the response .json -> material
  106.         $h = ($hslbc['hue']*pi())/180;
  107.         $s = $hslbc['saturation'];
  108.         $l = $hslbc['lightness'];
  109.         $b = $hslbc['brightness']/128;
  110.         $c = $hslbc['contrast'];
  111.      
  112.         // 4x4 identity matrix
  113.         $matrix = array(
  114.             array(1, 0, 0, 0),
  115.             array(0, 1, 0, 0),
  116.             array(0, 0, 1, 0),
  117.             array(0, 0, 0, 1)
  118.         );
  119.      
  120.         if($b != 0 || $c != 1){
  121.             // process brightness and contrast
  122.             $t = 128*(2*$b+1-$c);
  123.             $mult = array(
  124.                 array($c,  0,  0, $t),
  125.                 array( 0, $c,  0, $t),
  126.                 array( 0,  0, $c, $t),
  127.                 array( 0,  0,  0,  1)
  128.             );
  129.             $matrix = matrix_multiply($mult, $matrix);
  130.         }
  131.      
  132.         if($h != 0 || $s != 1 || $l != 1){
  133.             // transform to HSL
  134.             $multRgbToHsl = array(
  135.                 array( 0.707107, 0,        -0.707107, 0),
  136.                 array(-0.408248, 0.816497, -0.408248, 0),
  137.                 array( 0.577350, 0.577350,  0.577350, 0),
  138.                 array( 0,        0,         0,        1)
  139.             );
  140.             $matrix = matrix_multiply($multRgbToHsl, $matrix);
  141.      
  142.             // process adjustments
  143.             $cosHue = cos($h);
  144.             $sinHue = sin($h);
  145.             $mult = array(
  146.                 array( $cosHue * $s, $sinHue * $s,  0, 0),
  147.                 array(-$sinHue * $s, $cosHue * $s,  0, 0),
  148.                 array(            0,            0, $l, 0),
  149.                 array(            0,            0,  0, 1)
  150.             );
  151.             $matrix = matrix_multiply($mult, $matrix);
  152.      
  153.             // transform back to RGB
  154.             $multHslToRgb = array(
  155.                 array( 0.707107, -0.408248, 0.577350, 0),
  156.                 array( 0,         0.816497, 0.577350, 0),
  157.                 array(-0.707107, -0.408248, 0.577350, 0),
  158.                 array( 0,         0,        0,        1)
  159.             );
  160.             $matrix = matrix_multiply($multHslToRgb, $matrix);
  161.         }
  162.      
  163.         // apply the color transformation
  164.         $bgrVector = array(
  165.             array($base[2]),
  166.             array($base[1]),
  167.             array($base[0]),
  168.             array(1)
  169.         );
  170.         $bgrVector =  matrix_multiply($matrix,$bgrVector);
  171.         $rgb = array($bgrVector[2][0], $bgrVector[1][0], $bgrVector[0][0]);
  172.      
  173.         // clamp the values
  174.         $rgb[0] = floor(max(0, min(255, $rgb[0])));
  175.         $rgb[1] = floor(max(0, min(255, $rgb[1])));
  176.         $rgb[2] = floor(max(0, min(255, $rgb[2])));
  177.      
  178.         return $rgb;
  179.     }
  180. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement