>  기사  >  백엔드 개발  >  php 创建sprite

php 创建sprite

WBOY
WBOY원래의
2016-07-25 08:48:45848검색
根据文件夹内的图片生成 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 $height=$picinfo[1];
  13. }
  14. }
  15. $image=imagecreatetruecolor($width,$height);
  16. imagesavealpha($image, true);
  17. $color=imagecolorallocatealpha($image,0,0,0,127) ;
  18. imagefill($image, 0, 0, $color);
  19. $width=0;
  20. $height=0;
  21. $css="";
  22. foreach ($imagedir as $key => $value) {
  23. $picinfo=getimagesize($srcdir.$value);
  24. $im=imagecreatefrompng($srcdir.$value); //创建image
  25. imagecopymerge($image, $im, $width, 0, 0, 0, $picinfo[0], $picinfo[1],100);
  26. $picname=pathinfo($srcdir.$value);
  27. $css=".".$prefix.$picname['filename']."{height:".$picinfo[0]."px;width:".$picinfo[1]."px;background-position: -".$width."px 0px;}".$css;
  28. $width=$width+$picinfo[0];
  29. imagedestroy($im); //销毁image
  30. }
  31. $css=$css."[class*=".$prefix."]{background-image:url('image.png');}}";
  32. $css=$css.".".$prefix."{background-image:url('image.png');}"; //兼容ie 系列
  33. file_put_contents("./".$dst.'.css',$css);
  34. imagepng($image,"./".$dst.'.png');
  35. imagedestroy($image);
  36. ?>
  • 复制代码


  • 성명:
    본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.