Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- try
- {
- $background = array(brightness=>-18,contrast=>1,hue=>275,saturation=>0.0234375,lightness=>1.09375);
- $primary = array(brightness=>-2,contrast=>1,hue=>135,saturation=>0.546875,lightness=>0.976563);
- $secondary = array(brightness=>4,contrast=>1,hue=>240,saturation=>0.625,lightness=>1.09375);
- $image1 = new Imagick("./emblems/58a.png");
- $image2 = new Imagick("./emblems/58b.png");
- $imagebk = new Imagick("./backgrounds/0.png");
- // Re-colour Emblem Primary Part
- $it = $image1->getPixelIterator();
- foreach( $it as $row => $pixels )
- {
- foreach ( $pixels as $column => $pixel )
- {
- $alpha = $pixel->getColorValue(imagick::COLOR_ALPHA);
- if($alpha>0){
- $colour = $pixel->getColor();
- $baseRGB = array($colour['r'],$colour['g'],$colour['b']);
- $rgb = compositeColorShiftRgb($primary, $baseRGB);
- $r = $rgb[0];
- $g = $rgb[1];
- $b = $rgb[2];
- $pixel->setColor( "rgba($r,$g,$b,$alpha)" );
- }
- }
- $it->syncIterator();
- }
- // Re-colour Emblem Secondary Part
- $it = $image2->getPixelIterator();
- foreach( $it as $row => $pixels )
- {
- foreach ( $pixels as $column => $pixel )
- {
- $alpha = $pixel->getColorValue(imagick::COLOR_ALPHA);
- if($alpha>0){
- $colour = $pixel->getColor();
- $baseRGB = array($colour['r'],$colour['g'],$colour['b']);
- $rgb = compositeColorShiftRgb($secondary, $baseRGB);
- $r = $rgb[0];
- $g = $rgb[1];
- $b = $rgb[2];
- $pixel->setColor( "rgba($r,$g,$b,$alpha)" );
- }
- }
- $it->syncIterator();
- }
- // Re-colour Emblem Background Part
- $it = $imagebk->getPixelIterator();
- foreach( $it as $row => $pixels )
- {
- foreach ( $pixels as $column => $pixel )
- {
- $alpha = $pixel->getColorValue(imagick::COLOR_ALPHA);
- if($alpha>0){
- $colour = $pixel->getColor();
- $baseRGB = array($colour['r'],$colour['g'],$colour['b']);
- $rgb = compositeColorShiftRgb($background, $baseRGB);
- $r = $rgb[0];
- $g = $rgb[1];
- $b = $rgb[2];
- $pixel->setColor( "rgba($r,$g,$b,$alpha)" );
- }
- }
- $it->syncIterator();
- }
- $image1->compositeImage( $image2, $image2->getImageCompose(), 0, 0 );
- $imagebk->compositeImage( $image1, $image1->getImageCompose(), 0, 0 );
- header("Content-Type: image/png");
- echo $imagebk;
- }
- catch(Exception $e)
- {
- echo $e->getMessage();
- }
- // Function matrix_multiply by smiley: https://gist.github.com/codemasher/b869faa7603e1934c28d
- function matrix_multiply($m1, $m2){
- $r = count($m1);
- $c = count($m2[0]);
- $p = count($m2);
- if(count($m1[0]) != $p){
- return false; //incompatible matrix
- }
- $m3 = array();
- for($i = 0; $i < $r; $i++){
- for($j = 0; $j < $c; $j++){
- $m3[$i][$j] = 0;
- for($k = 0; $k < $p; $k++){
- $m3[$i][$j] += $m1[$i][$k]*$m2[$k][$j];
- }
- }
- }
- return ($m3);
- }
- // Function compositeColorShiftRgb by smiley: https://gist.github.com/codemasher/b869faa7603e1934c28d
- function compositeColorShiftRgb($hslbc, $base){
- //colors from the response .json -> material
- $h = ($hslbc['hue']*pi())/180;
- $s = $hslbc['saturation'];
- $l = $hslbc['lightness'];
- $b = $hslbc['brightness']/128;
- $c = $hslbc['contrast'];
- // 4x4 identity matrix
- $matrix = array(
- array(1, 0, 0, 0),
- array(0, 1, 0, 0),
- array(0, 0, 1, 0),
- array(0, 0, 0, 1)
- );
- if($b != 0 || $c != 1){
- // process brightness and contrast
- $t = 128*(2*$b+1-$c);
- $mult = array(
- array($c, 0, 0, $t),
- array( 0, $c, 0, $t),
- array( 0, 0, $c, $t),
- array( 0, 0, 0, 1)
- );
- $matrix = matrix_multiply($mult, $matrix);
- }
- if($h != 0 || $s != 1 || $l != 1){
- // transform to HSL
- $multRgbToHsl = array(
- array( 0.707107, 0, -0.707107, 0),
- array(-0.408248, 0.816497, -0.408248, 0),
- array( 0.577350, 0.577350, 0.577350, 0),
- array( 0, 0, 0, 1)
- );
- $matrix = matrix_multiply($multRgbToHsl, $matrix);
- // process adjustments
- $cosHue = cos($h);
- $sinHue = sin($h);
- $mult = array(
- array( $cosHue * $s, $sinHue * $s, 0, 0),
- array(-$sinHue * $s, $cosHue * $s, 0, 0),
- array( 0, 0, $l, 0),
- array( 0, 0, 0, 1)
- );
- $matrix = matrix_multiply($mult, $matrix);
- // transform back to RGB
- $multHslToRgb = array(
- array( 0.707107, -0.408248, 0.577350, 0),
- array( 0, 0.816497, 0.577350, 0),
- array(-0.707107, -0.408248, 0.577350, 0),
- array( 0, 0, 0, 1)
- );
- $matrix = matrix_multiply($multHslToRgb, $matrix);
- }
- // apply the color transformation
- $bgrVector = array(
- array($base[2]),
- array($base[1]),
- array($base[0]),
- array(1)
- );
- $bgrVector = matrix_multiply($matrix,$bgrVector);
- $rgb = array($bgrVector[2][0], $bgrVector[1][0], $bgrVector[0][0]);
- // clamp the values
- $rgb[0] = floor(max(0, min(255, $rgb[0])));
- $rgb[1] = floor(max(0, min(255, $rgb[1])));
- $rgb[2] = floor(max(0, min(255, $rgb[2])));
- return $rgb;
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement