PHP easy to read file directory files, generate CSS Spirte pictures

Source: Internet
Author: User
Tags imagecopy transparent color

Because the individual is not familiar with the PS, do not know how to generate a horizontal in the PS Spirte picture, using the "CSS Sprite V4.3" version, the resulting picture will appear compressed picture size, the original author wanted to modify the development of the program, but do not understand C #, had to use PHP GD library to generate CSS Spirte pictures.

1<?PHP2 Header("Content-type:image/png");3 $path= "Output";//This file directory is recommended to put all the images that need to generate CSS Spirte4 5 //Recycle source Files directory6 $handle=Opendir($path);7 $files=Array();8 //read all files in the source directory9  while(false!== ($file=Readdir($handle))) {Ten     if($file! = '. ' &&$file! = ' ... ') { One         $filename=$path. "/"  .$file; A         if(Is_file($filename)) { -             $files[] =$filename; -}Else { the             $files=Array_merge($files, Get_files ($filename)); -         } -     } -}//End While + Closedir($handle); -  + $bgImageSize= 0;//width of transparent background A $bgImageHeight= 0;//the height of the transparent bottom at foreach($files  as $key=$value){ -     //Var_dump (getimagesize ($value)); -     $source[$key[' source '] = Imagecreatefrompng ($value); -     $source[$key[' size '] =getimagesize($value); -     $imageInfo=$source[$key[' Size ']; -     $bgImageSize+=$imageInfo[0]; in     if($imageInfo[1] >$bgImageHeight){ -         $bgImageHeight=$imageInfo[1]; to     } + } -  the $img= Imagecreatetruecolor ($bgImageSize,$bgImageHeight);  *Imagesavealpha ($img,true); $ $color=imagecolorallocate ($img, 255,255,255);Panax NotoginsengImagealphablending ($img,false);//This is important, meaning that the color is not merged, directly replaced with $img image color, including transparent color;  -Imagesavealpha ($img,true);//This is very important, meaning not to lose the transparent color of the $thumb image;  the  + $source=Array(); A  the foreach($files  as $key=$value){ +     //Var_dump (getimagesize ($value)); -     $source[$key[' source '] = Imagecreatefrompng ($value); $     $source[$key[' size '] =getimagesize($value); $ } -  - $DST _y= 0;//the spacing between pictures the  - foreach($source  as $key=$value){Wuyi     //bool Imagecopy (Resource $DST _im, resource $src _im, int $dst _x, int $dst _y, int $src _x, int $src _y, int $src _w , int $src _h) the     $sourceInfo=$source[$key[' Size ']; -     $DST _x=$key*$sourceInfo[0]; WuImagecopy ($img,$source[$key[' Source '],$DST _x,$DST _y, 0, 0,$sourceInfo[0],$sourceInfo[1]); - } About //Set as file name $ $date=Date("Ymd_his"). ' _‘.Mt_rand(0,10); -Imagepng ($img, ' image '.$date.‘. png); -Imagedestroy ($img);

The essay references

    1. Solve the black background problem when PHP clipping thumbnails generate png,gif transparent graphs
    2. PHP 4 ways to traverse files

Note:

1. My output directory is, I put the manual modification to the file name in an orderly manner, so the code does not have the file name ordering section, the picture is from the Flash CS6 edit fla "Export Sprite" obtained.

PHP easy to read file directory files, generate CSS Spirte pictures

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.