Exemple #1
0
 function colorizeImage($img, $targetColor, $baseColor)
 {
     $cachefile = $this->_folder . 'colorize' . md5($img) . $targetColor . $baseColor . '.' . $this->_filetype;
     if (!NextendFilesystem::existsFile($cachefile)) {
         $targetHexArr = NextendColor::hex82hex($targetColor);
         $targetColor = $targetHexArr[0];
         $alpha = hexdec($targetHexArr[1]);
         $c1 = NextendColor::hex2hsl($baseColor);
         $c2 = NextendColor::hex2hsl($targetColor);
         $im = imagecreatefrompng($img);
         $width = imagesx($im);
         $height = imagesy($im);
         $this->createIm($width, $height);
         $rgb = NextendColor::rgb2array($targetColor);
         for ($x = 0; $x < $width; $x++) {
             for ($y = 0; $y < $height; $y++) {
                 $rgba = ImageColorAt($im, $x, $y);
                 $rgb = array($rgba >> 16 & 0xff, $rgba >> 8 & 0xff, $rgba & 0xff);
                 $hsl = NextendColor::rgb2hsl($rgb);
                 $a[0] = $hsl[0] + ($c2[0] - $c1[0]);
                 $a[1] = $hsl[1] * ($c2[1] / $c1[1]);
                 if ($a[1] > 1) {
                     $a[1] = 1;
                 }
                 $a[2] = exp(log($hsl[2]) * log($c2[2]) / log($c1[2]));
                 if ($a[2] > 1) {
                     $a[2] = 1;
                 }
                 $rgb = NextendColor::hsl2rgb($a);
                 $A = 0xff - ($rgba >> 24) * 2 & 0xff;
                 $A = (int) ($A * ($alpha / 0xff));
                 if ($A > 0xff) {
                     $A = 0xff;
                 }
                 $A = (int) ((0xff - $A) / 2);
                 imagesetpixel($this->_im, $x, $y, imagecolorallocatealpha($this->_im, $rgb[0], $rgb[1], $rgb[2], $A));
             }
         }
         $this->saveIm($cachefile);
         imagedestroy($im);
     }
     return NextendFilesystem::pathToAbsoluteURL($cachefile);
 }