private function composeImage($color, $icon_data)
 {
     $icon_img = imagecreatefromstring($icon_data);
     $map = CelerityResourceTransformer::getCSSVariableMap();
     $color_string = idx($map, $color, '#ff00ff');
     $color_const = hexdec(trim($color_string, '#'));
     $canvas = imagecreatetruecolor(50, 50);
     imagefill($canvas, 0, 0, $color_const);
     imagecopy($canvas, $icon_img, 0, 0, 0, 0, 50, 50);
     return PhabricatorImageTransformer::saveImageDataInAnyFormat($canvas, 'image/png');
 }
 protected function applyCropAndScale($dst_w, $dst_h, $src_x, $src_y, $src_w, $src_h, $use_w, $use_h, $scale_up)
 {
     // Figure out the effective destination width, height, and offsets.
     $cpy_w = min($dst_w, $use_w);
     $cpy_h = min($dst_h, $use_h);
     // If we aren't scaling up, and are copying a very small source image,
     // we're just going to center it in the destination image.
     if (!$scale_up) {
         $cpy_w = min($cpy_w, $src_w);
         $cpy_h = min($cpy_h, $src_h);
     }
     $off_x = ($dst_w - $cpy_w) / 2;
     $off_y = ($dst_h - $cpy_h) / 2;
     if ($this->shouldUseImagemagick()) {
         $argv = array();
         $argv[] = '-coalesce';
         $argv[] = '-shave';
         $argv[] = $src_x . 'x' . $src_y;
         $argv[] = '-resize';
         if ($scale_up) {
             $argv[] = $dst_w . 'x' . $dst_h;
         } else {
             $argv[] = $dst_w . 'x' . $dst_h . '>';
         }
         $argv[] = '-bordercolor';
         $argv[] = 'rgba(255, 255, 255, 0)';
         $argv[] = '-border';
         $argv[] = $off_x . 'x' . $off_y;
         return $this->applyImagemagick($argv);
     }
     $src = $this->getImage();
     $dst = $this->newEmptyImage($dst_w, $dst_h);
     $trap = new PhutilErrorTrap();
     $ok = @imagecopyresampled($dst, $src, $off_x, $off_y, $src_x, $src_y, $cpy_w, $cpy_h, $src_w, $src_h);
     $errors = $trap->getErrorsAsString();
     $trap->destroy();
     if ($ok === false) {
         throw new Exception(pht('Failed to imagecopyresampled() image: %s', $errors));
     }
     $data = PhabricatorImageTransformer::saveImageDataInAnyFormat($dst, $this->file->getMimeType());
     return $this->newFileFromData($data);
 }
 public function generate($x, $y)
 {
     $image = imagecreatetruecolor($x, $y);
     $this->draw($image, $x, $y);
     return PhabricatorImageTransformer::saveImageDataInAnyFormat($image, 'image/jpeg');
 }
 private function composeImage($color, $icon)
 {
     $color_map = self::getAllColors();
     $color = idx($color_map, $color);
     if (!$color) {
         $fallback = 'backdrop';
         $color = idx($color_map, $fallback);
         if (!$color) {
             throw new Exception(pht('Fallback compose color ("%s") does not exist!', $fallback));
         }
     }
     $color_hex = idx($color, 'color');
     $color_const = hexdec(trim($color_hex, '#'));
     $icon_map = self::getAllIcons();
     $icon = idx($icon_map, $icon);
     if (!$icon) {
         $fallback = 'fa-umbrella';
         $icon = idx($icon_map, $fallback);
         if (!$icon) {
             throw new Exception(pht('Fallback compose icon ("%s") does not exist!', $fallback));
         }
     }
     $path = idx($icon, 'path');
     $data = Filesystem::readFile($path);
     $icon_img = imagecreatefromstring($data);
     $canvas = imagecreatetruecolor(200, 200);
     imagefill($canvas, 0, 0, $color_const);
     imagecopy($canvas, $icon_img, 0, 0, 0, 0, 200, 200);
     return PhabricatorImageTransformer::saveImageDataInAnyFormat($canvas, 'image/png');
 }