php 建立sprite

來源:互聯網
上載者:User
根據檔案夾內的圖片產生 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. ?>
複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.