/** * takes the sprite folder and an image name, and returns the html used on the site to display a sprite * * @param string $sprite_name * @param string $image * @return string * @author Craig Ulliott */ function sprite($sprite_name, $image) { $sprite = new Sprite($sprite_name); $relationship_array = $sprite->getRelationshipArray(); //does this image exist in this sprite if (isset($relationship_array[$image])) { // get the cell id in the sprite for this image $class = $relationship_array[$image]; return '<span class="sprite_' . $sprite_name . ' ' . $class . '"></span>'; } throw new exception($image . ' not present in ' . $sprite . ' sprite'); }
/** * full create function, renders the sprite and saves it as a new file * * @depends testInstantiateSprite */ public function testCreate() { // build the object $sprite = new Sprite('test'); $file_name = $sprite->create(); // assert the new file exists $this->assertFileExists($file_name); }
/** * Render an image */ protected final function renderImage(&$img, $param) { $file_spr = false; $file_act = false; // Nothing to render... if (isset($param['scale'])) { if ($param['scale'] == 0) { return; } } // Use the same path if (!empty($param['path'])) { $file_spr = Client::getFile($param['path'] . '.spr'); $file_act = Client::getFile($param['path'] . '.act'); } else { if (!empty($param['spr']) && !empty($param['act'])) { if ($param['act'] === '.act' || $param['spr'] === '.spr') { return; } $file_spr = Client::getFile($param['spr']); $file_act = Client::getFile($param['act']); // Act file not found, search for the act file near .spr ? if ($file_act === false) { $file_act = Client::getFile(current(explode('.spr', $param['spr'])) . '.act'); } } } // Don't render if there is nothing to render (file not found or not defined) if ($file_spr === false || $file_act === false) { return; } // Always render on top (except for robe on some case) $render_onTop = true; // Calculate action and animation $action = max(0, $this->action * 8) + max(0, $this->direction) % 8; // Doridori mod only with some conditions if (get_class($this) === 'CharacterHeadRender' || get_class($this) === 'CharacterRender' && !empty($param['head']) && ($this->action === CharacterRender::ACTION_IDLE || $this->action === CharacterRender::ACTION_SIT)) { $doridori = $this->doridori % 3; $anim = 0; } else { // All animations are link to the body animation // If we update it, we have to update others. if (!empty($param['body'])) { $anim =& $this->body_animation; // Durty trick with doridori on body. if (get_class($this) === 'CharacterRender' && ($this->action === CharacterRender::ACTION_IDLE || $this->action === CharacterRender::ACTION_SIT)) { $this->doridori %= 3; $anim = $this->doridori; } } else { $anim = $this->body_animation; } $doridori = -1; } // Load spr and act $spr = new Sprite($file_spr); $act = new Action($file_act); $animation = $act->getAnimation($action, $anim, $doridori); // If have palette, load it if (!empty($param['pal'])) { if ($file_pal = Client::getFile($param['pal'])) { $spr->palette = file_get_contents($file_pal); } } $pos = $animation->pos[0]; $reference = isset($param['pos']) ? $param['pos'] : $pos; // Robe can be display behind the character some times if (isset($param['robe'])) { $render_onTop = DB::robe_ontop($this->param['class'], $this->param['sex'], $param['robe'], $action, $anim); } else { if (isset($param['shield'])) { $direction = max(0, $this->direction % 8); $render_onTop = $direction !== self::DIRECTION_NORTHWEST && $direction !== self::DIRECTION_NORTHEAST && $direction !== self::DIRECTION_NORTH; } } // Robe zIndex if (!$render_onTop) { $_img = $img; $img = $this->createImage(); } // Draw all layers foreach ($animation->layers as $layer) { // Avoid rendering empty images if ($layer->index > -1) { // RGBA image is after pal image $index = $layer->index; if ($layer->spr_type === Sprite::TYPE_RGBA) { $index += $spr->rgba_index; } // Build sprite image $image = $spr->getImage($index, $layer->is_mirror, $layer->color, self::$background_color); $width = imagesx($image); $height = imagesy($image); $scale = $layer->scale; // Main scale feature if (isset($param['scale'])) { $scale[0] *= $param['scale']; $scale[1] *= $param['scale']; } // Generate scale if ($scale[0] !== 1.0 && $scale[1] !== 1.0) { // New size $w = $width * $scale[0]; $h = $height * $scale[1]; // Copy image to new layer (resize) $tmp = $this->createImage($w, $h); imagecopyresampled($tmp, $image, 0, 0, 0, 0, $w, $h, $width, $height); imagedestroy($image); $image = $tmp; $width = $w; $height = $h; } else { if (!imageistruecolor($image)) { $tmp = $this->createImage($width, $height); imagecopy($tmp, $image, 0, 0, 0, 0, $width, $height); imagedestroy($image); $image = $tmp; } } // Apply a rotation if (!empty($layer->angle)) { $image = imagerotate($image, -$layer->angle, imagecolortransparent($image), 1); $width = imagesx($image); $height = imagesy($image); } // Calculate the position $x = $this->dot_reference[0] - $width / 2 + $layer->pos[0] + $reference->x - intval($pos->x); $y = $this->dot_reference[1] - $height / 2 + $layer->pos[1] + $reference->y - intval($pos->y); // Copy image to main layer imagecopymerge($img, $image, $x, $y, 0, 0, $width, $height, $layer->color[3] * 100); imagedestroy($image); } } // Robe/shield ontop if (!$render_onTop) { imagecopy($img, $_img, 0, 0, 0, 0, $this->image_size[0], $this->image_size[1]); } // Return its pos for reference return $pos; }
print "Sprite image demo html created successfully.....\n"; } public function createCss($img, $type, $dim) { $name = $img; if (($pos = strrpos($name, "/")) !== false) { $name = substr($name, $pos + 1, strlen($name)); } $name = substr($name, 0, strrpos($name, ".")); $name = preg_replace('/[^a-zA-Z0-9]/', "-", $name); $k = str_replace($this->webDir, "", $img); $className = isset($this->preCss[$k]) ? $this->preCss[$k] : "icon-" . $name; $this->html .= '<img src="web/extjs-3/resources/images/default/s.gif" class="' . $className . '" width="' . $dim[2] . '" height="' . $dim[3] . '"/> ' . "\t" . $dim[0] . 'px -' . $dim[1] . 'px ' . "\t | class:" . $className . "\t | file:" . $img . "<hr>"; $this->css .= "." . $className . ' {background:url("../../' . $this->spriteImage . '.' . $type . '") no-repeat ' . $dim[0] . ' -' . $dim[1] . 'px !important;} '; } public function addFromDir($dir) { $items = scandir($dir); foreach ($items as $item) { $this->add($dir . "/" . $item); } } } $sp = new Sprite(); //Specify the path where the sprite should created $sp->setSpriteImageName("web/images/sprite"); //Specify the path where css file should be created $sp->setSpriteCssName("batch/sprite/sprite"); //$sp->addFromDir("W:\manager\plugins\appFlowerPlugin\web\images"); $sp->create();
public function showSprite() { $type = $_GET['type']; $sprite = new Sprite(C('IMERGE_PATH'), C('SRC.SRC_PATH')); $sprite->output($type); }