0

Code:

<?php
header("Content-type: image/png");

    $hue = rand(0, 360);
    $sat = rand(80,100);
    $lum = rand(50,100);

    $hue /= 360;
    $sat /= 100;
    $lum /= 100;

    $result = ColorHSLToRGB($hue, $sat, $lum);
    $col1 = sprintf('%02x', $result['r']) . sprintf('%02x', $result['g']) . sprintf('%02x', $result['b']);

    $hue2 = $hue + ((mt_rand() / mt_getrandmax()) / 10) - 0.5;
    $sat2 = $sat + ((mt_rand() / mt_getrandmax()) / 10) - 0.5;
    $lum2 = $lum + ((mt_rand() / mt_getrandmax()) / 10) - 0.5;
    $result2 = ColorHSLToRGB($hue2, $sat2, $lum2);
    $col2 = sprintf('%02x', $result2['r']) . sprintf('%02x', $result2['g']) . sprintf('%02x', $result2['b']);


function ColorHSLToRGB($h, $s, $l){

        $r = $l;
        $g = $l;
        $b = $l;
        $v = ($l <= 0.5) ? ($l * (1.0 + $s)) : ($l + $s - $l * $s);
        if ($v > 0){
              $m;
              $sv;
              $sextant;
              $fract;
              $vsf;
              $mid1;
              $mid2;

              $m = $l + $l - $v;
              $sv = ($v - $m ) / $v;
              $h *= 6.0;
              $sextant = floor($h);
              $fract = $h - $sextant;
              $vsf = $v * $sv * $fract;
              $mid1 = $m + $vsf;
              $mid2 = $v - $vsf;

              switch ($sextant)
              {
                    case 0:
                          $r = $v;
                          $g = $mid1;
                          $b = $m;
                          break;
                    case 1:
                          $r = $mid2;
                          $g = $v;
                          $b = $m;
                          break;
                    case 2:
                          $r = $m;
                          $g = $v;
                          $b = $mid1;
                          break;
                    case 3:
                          $r = $m;
                          $g = $mid2;
                          $b = $v;
                          break;
                    case 4:
                          $r = $mid1;
                          $g = $m;
                          $b = $v;
                          break;
                    case 5:
                          $r = $v;
                          $g = $m;
                          $b = $mid2;
                          break;
              }
        }
        return array('r' => $r * 255.0, 'g' => $g * 255.0, 'b' => $b * 255.0);
}

$height = 144;
$width = 144;

$start = $col1;
$end = $col2;
if ($_GET['start']) {
  $start = $_GET['start'];
}
if ($_GET['end']) {
  $end = $_GET['end'];
}

$start_r = hexdec(substr($start, 0, 2));
$start_g = hexdec(substr($start, 2, 2));
$start_b = hexdec(substr($start, 4, 2));
$end_r = hexdec(substr($end, 0, 2));
$end_g = hexdec(substr($end, 2, 2));
$end_b = hexdec(substr($end, 4, 2));
$image = @imagecreate($width, $height);

for($y=0;$y<$height;$y++) {
  for($x=0;$x<$width;$x++) {
    if ($start_r == $end_r) {
      $new_r = $start_r;
    }
    $difference = $start_r - $end_r;
    $new_r = $start_r - intval(($difference / $height) * $y); 
    if ($start_g == $end_g) {
      $new_g = $start_g;
    }
    $difference = $start_g - $end_g;
    $new_g = $start_g - intval(($difference / $height) * $y);         
    if ($start_b == $end_b) {
      $new_b = $start_b;
    }
    $difference = $start_b - $end_b;
    $new_b = $start_b - intval(($difference / $height) * $y);
    $row_color = imagecolorresolve($image, $new_r, $new_g, $new_b);
    imagesetpixel($image, $x, $y, $row_color);
  }    
}

function imagecopymerge_alpha($dst_im, $src_im, $dst_x, $dst_y, $src_x, $src_y, $src_w, $src_h, $pct){ 
        // creating a cut resource 
        $cut = imagecreatetruecolor($src_w, $src_h); 

        // copying relevant section from background to the cut resource 
        imagecopy($cut, $dst_im, 0, 0, $dst_x, $dst_y, $src_w, $src_h); 

        // copying relevant section from watermark to the cut resource 
        imagecopy($cut, $src_im, 0, 0, $src_x, $src_y, $src_w, $src_h); 

        // insert cut resource to destination image 
        imagecopymerge($dst_im, $cut, $dst_x, $dst_y, 0, 0, $src_w, $src_h, $pct); 
    } 

$yagami = imagecreatefrompng('yagami.png');

imagecopymerge_alpha($image, $yagami, 0, 0, 0, 0, 144, 144, 100);

imagepng($image);
imagedestroy($image);
imagedestroy($yagami);

?>

This can be seen at the page I'm using. As you can see, the eyes aren't blue, the black clothing isn't black (it's colored), etc.

An example

The colors of the overlay are changed to match the background, and that's not what I want. How can I fix this?

Aly
  • 847
  • 1
  • 6
  • 30

1 Answers1

0

The gradient image used @createimage not createimagetruecolor. That is why the colors were being converted.

Aly
  • 847
  • 1
  • 6
  • 30