Advertisement
Guest User

Untitled

a guest
Dec 16th, 2014
324
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 5.61 KB | None | 0 0
  1. <?php
  2. class Outfiter {
  3.  
  4.     protected static $instance = null;
  5.     protected static $_outfit_lookup_table = array(
  6.         0xFFFFFF, 0xFFD4BF, 0xFFE9BF, 0xFFFFBF, 0xE9FFBF, 0xD4FFBF,
  7.         0xBFFFBF, 0xBFFFD4, 0xBFFFE9, 0xBFFFFF, 0xBFE9FF, 0xBFD4FF,
  8.         0xBFBFFF, 0xD4BFFF, 0xE9BFFF, 0xFFBFFF, 0xFFBFE9, 0xFFBFD4,
  9.         0xFFBFBF, 0xDADADA, 0xBF9F8F, 0xBFAF8F, 0xBFBF8F, 0xAFBF8F,
  10.         0x9FBF8F, 0x8FBF8F, 0x8FBF9F, 0x8FBFAF, 0x8FBFBF, 0x8FAFBF,
  11.         0x8F9FBF, 0x8F8FBF, 0x9F8FBF, 0xAF8FBF, 0xBF8FBF, 0xBF8FAF,
  12.         0xBF8F9F, 0xBF8F8F, 0xB6B6B6, 0xBF7F5F, 0xBFAF8F, 0xBFBF5F,
  13.         0x9FBF5F, 0x7FBF5F, 0x5FBF5F, 0x5FBF7F, 0x5FBF9F, 0x5FBFBF,
  14.         0x5F9FBF, 0x5F7FBF, 0x5F5FBF, 0x7F5FBF, 0x9F5FBF, 0xBF5FBF,
  15.         0xBF5F9F, 0xBF5F7F, 0xBF5F5F, 0x919191, 0xBF6A3F, 0xBF943F,
  16.         0xBFBF3F, 0x94BF3F, 0x6ABF3F, 0x3FBF3F, 0x3FBF6A, 0x3FBF94,
  17.         0x3FBFBF, 0x3F94BF, 0x3F6ABF, 0x3F3FBF, 0x6A3FBF, 0x943FBF,
  18.         0xBF3FBF, 0xBF3F94, 0xBF3F6A, 0xBF3F3F, 0x6D6D6D, 0xFF5500,
  19.         0xFFAA00, 0xFFFF00, 0xAAFF00, 0x54FF00, 0x00FF00, 0x00FF54,
  20.         0x00FFAA, 0x00FFFF, 0x00A9FF, 0x0055FF, 0x0000FF, 0x5500FF,
  21.         0xA900FF, 0xFE00FF, 0xFF00AA, 0xFF0055, 0xFF0000, 0x484848,
  22.         0xBF3F00, 0xBF7F00, 0xBFBF00, 0x7FBF00, 0x3FBF00, 0x00BF00,
  23.         0x00BF3F, 0x00BF7F, 0x00BFBF, 0x007FBF, 0x003FBF, 0x0000BF,
  24.         0x3F00BF, 0x7F00BF, 0xBF00BF, 0xBF007F, 0xBF003F, 0xBF0000,
  25.         0x242424, 0x7F2A00, 0x7F5500, 0x7F7F00, 0x557F00, 0x2A7F00,
  26.         0x007F00, 0x007F2A, 0x007F55, 0x007F7F, 0x00547F, 0x002A7F,
  27.         0x00007F, 0x2A007F, 0x54007F, 0x7F007F, 0x7F0055, 0x7F002A,
  28.         0x7F0000,
  29.     );
  30.  
  31.     public static function instance() {
  32.         if (!isset(self::$instance))
  33.             self::$instance = new self();
  34.         return self::$instance;
  35.     }
  36.  
  37.     protected function prepareOutfit($_file1, $_file2, &$_image_template, &$_image_outfit) {
  38.         $_image_template = imagecreatefrompng($_file1);
  39.         $_image_outfit = imagecreatefrompng($_file2);
  40.         imagealphablending($_image_outfit, false);
  41.         imagesavealpha($_image_outfit, true);
  42.     }
  43.  
  44.     protected function outfit($outfit, $addons, $head, $body, $legs, $feet) {
  45.         if ($addons != 0 && (!file_exists('./outfits/template/' . $outfit . '_2_0_' . $addons . '.png') || !file_exists('./outfits/outfit/' . $outfit . '_2_0_' . $addons . '.png'))) {
  46.             $addons = 0;
  47.             if (!file_exists('./outfits/template/' . $outfit . '_2_0_' . $addons . '.png') || !file_exists('./outfits/outfit/' . $outfit . '_2_0_' . $addons . '.png'))
  48.                 return; //error file not found
  49.         } else if (!file_exists('./outfits/template/' . $outfit . '_2_0_' . $addons . '.png') || !file_exists('./outfits/outfit/' . $outfit . '_2_0_' . $addons . '.png')) {
  50.             return; //error file not found
  51.         }
  52.         $image_template = null;
  53.         $image_outfit = null;
  54.         $this->prepareOutfit('./outfits/template/' . $outfit . '_2_0_' . $addons . '.png', './outfits/outfit/' . $outfit . '_2_0_' . $addons . '.png', $image_template, $image_outfit);
  55.         $this->colorize($image_template, $image_outfit, $head, $body, $legs, $feet);
  56.         $im = $image_outfit;
  57.         imagedestroy($image_template);
  58.         //imagedestroy($image_outfit);
  59.         return $im;
  60.     }
  61.  
  62.     public function render($outfit, $addons, $head, $body, $legs, $feet) {
  63.         if (!is_numeric($outfit) || !is_numeric($addons) || !is_numeric($head) || !is_numeric($body) || !is_numeric($legs) || !is_numeric($feet))
  64.             return;
  65.  
  66.         $path = 'generated_outfits/' . $outfit . '-' . $addons . '-' . $head . '-' . $legs . '-' . $feet . '.png';
  67.        
  68.         if (!file_exists('./outfits/template/' . $outfit . '_2_0_1.png') &&
  69.             !file_exists('./outfits/template/' . $outfit . '_2_0_2.png') &&
  70.             !file_exists('./outfits/template/' . $outfit . '_2_0_3.png')) {
  71.            
  72.             $path = 'generated_outfits/' . $outfit . '.png';
  73.         }
  74.    
  75.         if (!file_exists($path)) {
  76.             imagepng($this->outfit($outfit, $addons, $head, $body, $legs, $feet), $path);
  77.         }
  78.            
  79.         header('Location: '.$path);
  80.     }
  81.  
  82.     public function save($outfit, $addons, $head, $body, $legs, $feet, $_file) {
  83.         imagepng($this->outfit($outfit, $addons, $head, $body, $legs, $feet), $_file);
  84.     }
  85.  
  86.     protected function colorizePixel($_color, &$_r, &$_g, &$_b) {
  87.         if ($_color < count(self::$_outfit_lookup_table))
  88.             $value = self::$_outfit_lookup_table[$_color];
  89.         else
  90.             $value = 0;
  91.         $ro = ($value & 0xFF0000) >> 16; // rgb outfit
  92.         $go = ($value & 0xFF00) >> 8;
  93.         $bo = ($value & 0xFF);
  94.         $_r = (int) ($_r * ($ro / 255));
  95.         $_g = (int) ($_g * ($go / 255));
  96.         $_b = (int) ($_b * ($bo / 255));
  97.     }
  98.  
  99.     protected function colorize(&$_image_template, &$_image_outfit, $_head, $_body, $_legs, $_feet) {
  100.         for ($i = 0; $i < imagesy($_image_template); $i++) {
  101.             for ($j = 0; $j < imagesx($_image_template); $j++) {
  102.                 $templatepixel = imagecolorat($_image_template, $j, $i);
  103.                 $outfit = imagecolorat($_image_outfit, $j, $i);
  104.  
  105.                 if ($templatepixel == $outfit)
  106.                     continue;
  107.  
  108.                 $rt = ($templatepixel >> 16) & 0xFF;
  109.                 $gt = ($templatepixel >> 8) & 0xFF;
  110.                 $bt = $templatepixel & 0xFF;
  111.                 $ro = ($outfit >> 16) & 0xFF;
  112.                 $go = ($outfit >> 8) & 0xFF;
  113.                 $bo = $outfit & 0xFF;
  114.  
  115.                 if ($rt && $gt && !$bt) { // yellow == head
  116.                     $this->colorizePixel($_head, $ro, $go, $bo);
  117.                 } else if ($rt && !$gt && !$bt) { // red == body
  118.                     $this->colorizePixel($_body, $ro, $go, $bo);
  119.                 } else if (!$rt && $gt && !$bt) { // green == legs
  120.                     $this->colorizePixel($_legs, $ro, $go, $bo);
  121.                 } else if (!$rt && !$gt && $bt) { // blue == feet
  122.                     $this->colorizePixel($_feet, $ro, $go, $bo);
  123.                 } else {
  124.                     continue; // if nothing changed, skip the change of pixel
  125.                 }
  126.  
  127.                 imagesetpixel($_image_outfit, $j, $i, imagecolorallocate($_image_outfit, $ro, $go, $bo));
  128.             }
  129.         }
  130.     }
  131.  
  132. }
  133.  
  134. Outfiter::instance()->render($_GET['id'], $_GET['addons'], $_GET['head'], $_GET['body'], $_GET['legs'], $_GET['feet']);
  135. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement