当前位置:Gxlcms > PHP教程 > php创建sprite

php创建sprite

时间:2021-07-01 10:21:17 帮助过:1人阅读

根据文件夹内的图片生成 sprite合成图
  1. $srcdir='./image/';
  2. $prefix="pic11_";
  3. $dst="image";
  4. $imagedir=scandir($srcdir);
  5. array_shift($imagedir);
  6. array_shift($imagedir);
  7. $width=0;
  8. $height=0;
  9. foreach ($imagedir as $key => $value) {
  10. $picinfo=getimagesize($srcdir.$value);
  11. $width=$picinfo[0]+$width;
  12. if ($height<$picinfo[1]) {
  13. $height=$picinfo[1];
  14. }
  15. }
  16. $image=imagecreatetruecolor($width,$height);
  17. imagesavealpha($image, true);
  18. $color=imagecolorallocatealpha($image,0,0,0,127) ;
  19. imagefill($image, 0, 0, $color);
  20. $width=0;
  21. $height=0;
  22. $css="";
  23. foreach ($imagedir as $key => $value) {
  24. $picinfo=getimagesize($srcdir.$value);
  25. $im=imagecreatefrompng($srcdir.$value); //创建image
  26. imagecopymerge($image, $im, $width, 0, 0, 0, $picinfo[0], $picinfo[1],100);
  27. $picname=pathinfo($srcdir.$value);
  28. $css=".".$prefix.$picname['filename']."{height:".$picinfo[0]."px;width:".$picinfo[1]."px;background-position: -".$width."px 0px;}".$css;
  29. $width=$width+$picinfo[0];
  30. imagedestroy($im); //销毁image
  31. }
  32. $css=$css."[class*=".$prefix."]{background-image:url('image.png');}}";
  33. $css=$css.".".$prefix."{background-image:url('image.png');}"; //兼容ie 系列
  34. file_put_contents("./".$dst.'.css',$css);
  35. imagepng($image,"./".$dst.'.png');
  36. imagedestroy($image);
  37. ?>

人气教程排行