PHP Create Sprite

Source: Internet
Author: User
Tags foreach

  This article describes the use of PHP based on the picture in the folder to generate Sprite Composite diagram example, the need for friends can refer to the following

  Code as follows: <?php $srcdir = './image/'; $prefix = "pic11_"; $DST = "image"; $imagedir =scandir ($srcdir); Array_shift ($imagedir); Array_shift ($imagedir); $width = 0; $height = 0; foreach ($imagedir as $key => $value) {  $picinfo =getimagesize ($srcdir. $value);   $width = $picinfo [0]+$ Width  if ($height < $picinfo [1]) {  $height = $picinfo [1]; &nbsp}} $image =imagecreatetruecolor ($width, $height) ; Imagesavealpha ($image, true); $color =imagecolorallocatealpha ($image, 0,0,0,127); Imagefill ($image, 0, 0, $color); $width = 0; $height = 0; $css = ""; foreach ($imagedir as $key => $value) {  $picinfo =getimagesize ($srcdir. $value);   $im =imagecreatefrompng ($ Srcdir. $value);      //Create Image  imagecopymerge ($image, $im, $width, 0, 0, 0, $picinfo [0], $picinfo [1],100);   $picname =pathinfo ($srcdir. $value);   $CSS = ".". $prefix. $picname [' filename ']. " {height: ". $picinfo [0]." Px;width: ". $picinfo [1]." Px;background-position:-". $width." px 0px;} ". $css;   $width =$width+ $picinfo [0];  imagedestroy ($im);            //Destroy image} $css = $css. " [class*= ". $prefix."] {background-image:url (' image.png ');}};   $CSS = $css. ". $prefix. " {background-image:url (' image.png ');} ";  //compatible IE series file_put_contents ("./". $dst. ') CSS ', $css); Imagepng ($image, "./". $dst. " PNG '); Imagedestroy ($image);?>   <link rel= "stylesheet" type= "Text/css" "href="/image.css "> <!--<img src = "./image.png" >--> <body style= "Background-color: #eee" >  <div class= "PIC11_CSS3" > </div > <div class= "Pic11_firefox" > </div> <div class= "Pic11_chrome" > </div> <span class= "CSS3 ">   </span> <span class=" Sprite Firefox > </span> </body>  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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: and provide relevant evidence. A staff member will contact you within 5 working days.