104 lines
2.9 KiB
PHP
104 lines
2.9 KiB
PHP
<?php
|
|
if(is_file('sprite.css'))
|
|
unlink('sprite.css');
|
|
if(is_file('sprite.html'))
|
|
unlink('sprite.html');
|
|
if(is_file('sprite.png'))
|
|
unlink('sprite.png');
|
|
if(is_file('sprite@2x.png'))
|
|
unlink('sprite@2x.png');
|
|
|
|
$files = glob('*.png');
|
|
|
|
define('CSS_TPL', '.fmi.fmi-%1$s { background-position: %2$dpx %3$dpx; }' . PHP_EOL);
|
|
$css = <<<CSS
|
|
.fmi {
|
|
background-image: url('sprite.png');
|
|
width: 25px;
|
|
height: 25px;
|
|
background-size: %1\$dpx %2\$dpx;
|
|
}
|
|
@media only screen and (-webkit-min-device-pixel-ratio: 2), only screen and (min-device-pixel-ratio: 2) {
|
|
.fmi { background-image: url('sprite@2x.png'); background-size: %1\$dpx %2\$dpx; }
|
|
}
|
|
|
|
CSS;
|
|
|
|
define('HTML_TPL', '<div class="fmi fmi-%1$s" style="border: 1px solid #0f0; margin: 5px; display: inline-block;" title="%1$s"></div>');
|
|
define('HTML_WRAP', '<!doctype html><link href="sprite.css" type="text/css" rel="stylesheet"/><div style="background-color: #000;">%1$s</div><div style="background-color: #fff;">%1$s</div>');
|
|
$html = '';
|
|
|
|
$lWidth2x = 0;
|
|
$lWidth1x = 0;
|
|
$lHeight2x = 0;
|
|
$lHeight1x = 0;
|
|
|
|
$width2x = 0;
|
|
$height2x = 0;
|
|
|
|
$dropFiles = [];
|
|
|
|
foreach($files as $file) {
|
|
$imgSize = getimagesize($file);
|
|
if($imgSize === false || $imgSize[2] !== IMAGETYPE_PNG) {
|
|
$dropFiles[] = $file;
|
|
continue;
|
|
}
|
|
|
|
if($lWidth2x < $imgSize[0])
|
|
$lWidth2x = $imgSize[0];
|
|
if($lHeight2x < $imgSize[1])
|
|
$lHeight2x = $imgSize[1];
|
|
|
|
$width2x += $imgSize[0];
|
|
if($height2x < $imgSize[1])
|
|
$height2x = $imgSize[1];
|
|
}
|
|
|
|
if($width2x < 1 || $height2x < 1)
|
|
die('did nothing' . PHP_EOL);
|
|
|
|
$files = array_diff($files, $dropFiles);
|
|
|
|
$lWidth1x = ceil($lWidth2x / 2);
|
|
$lHeight1x = ceil($lHeight2x / 2);
|
|
$width1x = ceil($width2x / 2);
|
|
$height1x = ceil($height2x / 2);
|
|
|
|
$offset = 0;
|
|
|
|
$sprite1x = new Imagick;
|
|
$sprite1x->newImage($width1x, $height1x, 'none');
|
|
$sprite1x->setImageFormat('png');
|
|
$sprite2x = new Imagick;
|
|
$sprite2x->newImage($width2x, $height2x, 'none');
|
|
$sprite2x->setImageFormat('png');
|
|
|
|
foreach($files as $file) {
|
|
$fileName = pathinfo($file, PATHINFO_FILENAME);
|
|
$html .= sprintf(HTML_TPL, $fileName);
|
|
$css .= sprintf(CSS_TPL, $fileName, $width1x - ($offset * $lWidth1x), 0);
|
|
|
|
$imagick = new Imagick($file);
|
|
|
|
$sprite2x->compositeImage($imagick, Imagick::COMPOSITE_DEFAULT, $offset * $lWidth2x, 0);
|
|
|
|
$imagick->resizeImage(
|
|
ceil($imagick->getImageWidth() / 2),
|
|
ceil($imagick->getImageHeight() / 2),
|
|
Imagick::FILTER_LANCZOS, 1
|
|
);
|
|
|
|
$sprite1x->compositeImage($imagick, Imagick::COMPOSITE_DEFAULT, $offset * $lWidth1x, 0);
|
|
|
|
$imagick->destroy();
|
|
$offset++;
|
|
}
|
|
|
|
$sprite1x->writeImage('sprite.png');
|
|
$sprite1x->destroy();
|
|
$sprite2x->writeImage('sprite@2x.png');
|
|
$sprite2x->destroy();
|
|
|
|
file_put_contents('sprite.css', sprintf($css, $width1x, $height1x, $width2x, $height2x));
|
|
file_put_contents('sprite.html', sprintf(HTML_WRAP, $html));
|