【问题标题】:Creating a Horizontal Sprite From Function?从函数创建水平精灵?
【发布时间】:2014-05-26 18:17:33
【问题描述】:

此函数将根据文件类型和尺寸从目录中的所有图像创建一个精灵。我在脚本中面临的唯一问题是我需要精灵是水平的,而这个函数只输出一个垂直的图像。有关进行所需更改的任何建议?任何帮助将不胜感激。

<?php
class images_to_sprite{
function images_to_sprite($folder,$output,$x,$y){
$this->folder = ($folder = 'myfolder'); // Folder name to get images from, i.e. 
$this->filetypes = array('jpg'=>true,'png'=>true,'jpeg'=>true,'gif'=>true); // Acceptable file extensions
$this->output = ($output ? $output : 'mysprite'); // Output filenames, mysprite.png and mysprite.css
$this->x = '100'; // Width of images to consider
$this->y = '100'; // Heigh of images to consider
$this->files = array();
}

function create_sprite(){
$basedir = $this->folder;
$files = array();
 // Read through the directory for suitable images
    if($handle = opendir($this->folder)) 
    {
        while (false !== ($file = readdir($handle)))
        {
        $split = explode('.',$file);
        // Ignore non-matching file extensions
            if($file[0] == '.' || !isset($this->filetypes[$split[count($split)-1]]))
                continue;
        // Get image size and ascertain it has the correct dimensions
        $output = getimagesize($this->folder.'/'.$file);
            if($output[0] != $this->x && $output[1] != $this->y)
        continue;
        // Image will be added to sprite, add to array
        $this->files[$file] = $file;
        }
    closedir($handle);
    }

// yy is the height of the sprite to be created, basically X * number of images
$this->yy = $this->y * count($this->files);
$im = imagecreatetruecolor($this->x,$this->yy);
// Add alpha channel to image (transparency)
imagesavealpha($im, true);
$alpha = imagecolorallocatealpha($im, 0, 0, 0, 127);
imagefill($im,0,0,$alpha);

// Append images to sprite and generate CSS lines
$i = $ii = 0;
$fp = fopen($this->output.'.css','w');
fwrite($fp,'.'.$this->output.' { width: '.$this->x.'px; height: '.$this->y.'px; background-image: url('.$this-        >output.'.png); text-align:center; }'."\n");
    foreach($this->files as $key => $file)
    {
    fwrite($fp,'.'.$this->output.(++$ii).' { background-position: -0px -'.($this->y*$i).'px; }'."\n");
    $im2 = imagecreatefrompng($this->folder.'/'.$file);
    imagecopy($im,$im2,0,($this->y*$i),0,0,$this->x,$this->y);
    $i++;
    }
fclose($fp);
imagepng($im,$this->output.'.png'); // Save image to file
imagedestroy($im);
}}
$class = new images_to_sprite('imagefolder','sprite',63,63);
$class->create_sprite();?>

【问题讨论】:

    标签: php gd sprite imagecreatefrompng


    【解决方案1】:

    基本上你正在创建一个 100 x 的图像(100 x 图像), 例如,如果您有 5 张图像,则为 100 x 500,这就是您有垂直图像的原因;但是你需要创建一个 500 x 100 的图像,所以改变

    $this->yy = $this->y * count($this->files);
    $im = imagecreatetruecolor($this->x,$this->yy);
    

    通过

    $this->xx = $this->x * count($this->files);
    $im = imagecreatetruecolor($this->xx,$this->y);
    

    imagecopy($im,$im2,0,($this->y*$i),0,0,$this->x,$this->y);
    

    通过

    imagecopy($im,$im2,($this->x*$i),0,0,0,$this->x,$this->y);
    

    所以,我做了这些小改动

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-02
      • 2011-07-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多