Beispiel #1
0
  public static function create_icon_sprite($color_icon, $color_icon_2x = "", $overwrite = false, $tints = array("#0F191F", "#0F1900" ) ) {

    if (!is_admin()) {
      return new WOOF_Silent( __("Cannot create icon sprites outside of admin", MASTERPRESS_DOMAIN) );
    }
    
    if (is_object($color_icon) && get_class($color_icon) == "WOOF_Image") {
      $name = $color_icon->basename();
      $color_icon_1x_path = $color_icon->path;
    } else {
      $name = $color_icon;
      $color_icon_1x_path = MASTERPRESS_CONTENT_MENU_ICONS_DIR.$color_icon;
    }

    if (is_object($color_icon_2x) && get_class($color_icon_2x) == "WOOF_Image") {
      $color_icon_2x_path = $color_icon_2x->path;
    } else {
      $color_icon_2x_path = MASTERPRESS_CONTENT_MENU_ICONS_DIR.$color_icon_2x;
    }

    $sprite_icon = self::sprite_filename($name);
    

    $sprite_icon_path = MASTERPRESS_CONTENT_MENU_ICONS_DIR.$sprite_icon;
    $sprite_icon_url = MASTERPRESS_CONTENT_MENU_ICONS_URL.$sprite_icon;
    
    $exists = self::file_exists($color_icon, $color_icon_1x_path);
    $exists_2x = self::file_exists($color_icon_2x, $color_icon_2x_path);
    
    if ( ( $exists || $exists_2x) && ( $overwrite || (!file_exists($sprite_icon_path) ) ) )  {
      
      // determine the mode - if we have both icons, we'll use both, otherwise we'll the one we have up or down
      
      if ( $exists && $exists_2x ) {

        $mode = "both";
        $src_icon_1x = $color_icon_1x_path;
        $src_icon_2x = $color_icon_2x_path;
      } else if ( $exists_2x ) {

        $mode = "2x";
        $src_icon_1x = $color_icon_2x_path;
        $src_icon_2x = $color_icon_2x_path;
      } else {
        $mode = "1x";
        $src_icon_1x = $color_icon_1x_path;
        $src_icon_2x = $color_icon_1x_path;
      }
      
      $size_1x = getimagesize($src_icon_1x);
      $size_2x = getimagesize($src_icon_2x);
      
      
      // create color handles
      
      $color_1x = self::imagecreatefromauto($src_icon_1x);
      $color_2x = self::imagecreatefromauto($src_icon_2x);
      
      // create grayscale images
      
      $grayscale_1x = self::imagecreatefromauto($src_icon_1x);
      $grayscale_2x = self::imagecreatefromauto($src_icon_2x);
      
      imagefilter($grayscale_1x, IMG_FILTER_GRAYSCALE);
      imagefilter($grayscale_2x, IMG_FILTER_GRAYSCALE);

      // create image tints
      
      $tinted_1x = array();
      $tinted_2x = array();
      
      foreach ($tints as $tint) {

        $tint_1x = self::imagecreatefromauto($src_icon_1x);
        $tint_2x = self::imagecreatefromauto($src_icon_2x);
      
        $ci = WOOF_Image::parse_color($tint);
        
        $r = $ci["rgb"][0];
        $g = $ci["rgb"][1];
        $b = $ci["rgb"][2];
        
        imagefilter($tint_1x, IMG_FILTER_GRAYSCALE);
        imagefilter($tint_1x, IMG_FILTER_COLORIZE, $r, $g, $b);
        imagefilter($tint_2x, IMG_FILTER_GRAYSCALE);
        imagefilter($tint_2x, IMG_FILTER_COLORIZE, $r, $g, $b);
        
        $tinted_1x[$tint] = $tint_1x;
        $tinted_2x[$tint] = $tint_2x;
        
      }
      
      // now compose the sprite
      
      
      // calculate dimensions

      
      $spacing = 300;

      $width = 32 + $spacing + 32 + ( count($tints) * (32 + $spacing) );
      $height = 16 + 64 + 32;
      
      
      // create the sprite handle
      
      $sprite = imagecreatetruecolor( $width, $height );
      $transparent = imagecolorallocatealpha( $sprite, 0, 0, 0, 127 ); 
      imagefill( $sprite, 0, 0, $transparent ); 

      $x = 0;

      // place the grayscale 1x and 2x

      imagecopyresampled($sprite, $grayscale_1x, $x * $spacing, 0,  0, 0, 16, 16, $size_1x[0], $size_1x[1]); 
      imagecopyresampled($sprite, $grayscale_2x, $x * $spacing, 64, 0, 0, 32, 32, $size_2x[0], $size_2x[1]); 
       
      // place the color 1x and 2x

      $x++;
       
      imagecopyresampled($sprite, $color_1x, $x * $spacing, 0,  0, 0, 16, 16, $size_1x[0], $size_1x[1]); 
      imagecopyresampled($sprite, $color_2x, $x * $spacing, 64, 0, 0, 32, 32, $size_2x[0], $size_2x[1]); 
      
      // place the tints
      
      foreach ($tints as $tint) {
        $x++;
        imagecopyresampled($sprite, $tinted_1x[$tint], $x * $spacing, 0,  0, 0, 16, 16, $size_1x[0], $size_1x[1]); 
        imagecopyresampled($sprite, $tinted_2x[$tint], $x * $spacing, 64, 0, 0, 32, 32, $size_2x[0], $size_2x[1]); 
      }
      
      // finally save the image
      
      imagesavealpha($sprite, true);
      imagepng($sprite, $sprite_icon_path);

    }

    return new WOOF_Image( $sprite_icon_path, $sprite_icon_url ); 
    

  }