Home  >  Article  >  Backend Development  >  php create sprite

php create sprite

WBOY
WBOYOriginal
2016-07-25 08:48:45882browse
根据文件夹内的图片生成 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. ?>
  • 复制代码


  • Statement:
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn