Create sprite in php

Source: Internet
Author: User
Create sprite in php
Generate a sprite based on the image in the folder

  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,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); // create an 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); // destroy the image
  31. }
  32. $ Css = $ css. "[class * =". $ prefix. "] {background-image: url('image.png ');}}";
  33. $ Css = $ css. ".". $ prefix. "{background-image: url('image.png ') ;}"; // Compatible with ie series
  34. File_put_contents ("./" .20.dst.'.css ', $ css );
  35. Imagepng ($ image, "./" .20.dst.'.png ');
  36. Imagedestroy ($ image );
  37. ?>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.