'); define('HTML_WRAP', '
%1$s
%1$s
'); $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));