php创建sprite

2015-01-24信息快讯网

本文介绍了使用PHP根据文件夹内的图片生成sprite合成图示例,需要的朋友可以参考下

<?php
$srcdir='./image/';
$prefix="pic11_";
$dst="image";
$imagedir=scandir($srcdir);
array_shift($imagedir);
array_shift($imagedir);
$width=0;
$height=0;
foreach ($imagedir as $key => $value) {
 $picinfo=getimagesize($srcdir.$value);
 $width=$picinfo[0]+$width;
 if ($height<$picinfo[1]) {
  $height=$picinfo[1];
 }
}
$image=imagecreatetruecolor($width,$height);
imagesavealpha($image, true);
$color=imagecolorallocatealpha($image,0,0,0,127) ;
imagefill($image, 0, 0, $color);
$width=0;
$height=0;
$css="";
foreach ($imagedir as $key => $value) {
 $picinfo=getimagesize($srcdir.$value);
 $im=imagecreatefrompng($srcdir.$value);       //创建image
 imagecopymerge($image, $im, $width, 0, 0, 0, $picinfo[0], $picinfo[1],100);
 $picname=pathinfo($srcdir.$value);
 $css=".".$prefix.$picname['filename']."{height:".$picinfo[0]."px;width:".$picinfo[1]."px;background-position: -".$width."px 0px;}".$css;
 $width=$width+$picinfo[0];
 imagedestroy($im);             //销毁image
}
$css=$css."[class*=".$prefix."]{background-image:url('image.png');}}"; 
$css=$css.".".$prefix."{background-image:url('image.png');}";  //兼容ie 系列
file_put_contents("./".$dst.'.css',$css);
imagepng($image,"./".$dst.'.png');
imagedestroy($image);
?>

<link rel="stylesheet" type="text/css" href="./image.css"> <!-- <img src="./image.png"> --> <body style="background-color:#eee"> <div class="pic11_css3"> </div> <div class="pic11_firefox"> </div> <div class="pic11_chrome"> </div> <span class="css3">

</span> <span class="sprite firefox"> </span> </body>

ThinkPHP跳转页success及error模板实例教程
浅谈php安全性需要注意的几点事项
PHP采用XML-RPC构造Web Service实例教程
ThinkPHP应用模式扩展详解
CodeIgniter模板引擎使用实例
PHP以mysqli方式连接类完整代码实例
CodeIgniter实现更改view文件夹路径的方法
PHP关于htmlspecialchars、strip_tags、addslashes的解释
PHP+Memcache实现wordpress访问总数统计(非插件)
CodeIgniter安全相关设置汇总
ThinkPHP模板判断输出Present标签用法详解
PHP把JPEG图片转换成Progressive JPEG的方法
PHP内置过滤器FILTER使用实例
PHP错误Parse error: syntax error, unexpected end of file in test.php on line 12解决方法
PHP微框架Dispatch简介
PHP命名空间(Namespace)简明教程
使用php批量删除数据库下所有前缀为prefix_的表
PHP中spl_autoload_register()和__autoload()区别分析
ASP和PHP实现生成网站快捷方式并下载到桌面的方法
PHP的preg_match匹配字符串长度问题解决方法
codeigniter教程之多文件上传使用示例
PHP循环结构实例讲解
php 获取SWF动画截图示例代码
php导入csv文件碰到乱码问题的解决方法
php判断正常访问和外部访问的示例
php利用单例模式实现日志处理类库
PHP下获取上个月、下个月、本月的日期(strtotime,date)
preg_match_all使用心得分享
基于preg_match_all采集后数据处理的一点心得笔记(编码转换和正则匹配)
分享下页面关键字抓取www.icbase.com站点代码(带asp.net参数的)
PHP类继承 extends使用介绍
php中filter函数验证、过滤用户输入的数据
codeigniter框架批量插入数据
百度站点地图(百度sitemap)生成方法分享
php 模拟 asp.net webFrom 按钮提交事件的思路及代码
wordpress自定义url参数实现路由功能的代码示例
浅析echo(),print(),print_r(),return之间的区别
PHP echo,print,printf,sprintf函数之间的区别与用法详解
PHP SPL使用方法和他的威力
PHP中spl_autoload_register函数的用法总结
©2014-2024 dbsqp.com