String[] strSrcs=ListUtil.listToStringArray(_srcs, ',');
Resource[] srcs=new Resource[strSrcs.length];
Image[] images=new Image[strSrcs.length];
for(int i=0;i<srcs.length;i++){
srcs[i]=ResourceUtil.toResourceExisting(pageContext, strSrcs[i]);
images[i] = new Image(srcs[i]);
}
// TODO use the same resource as for cfimage
PageSource ps = pageContext.getCurrentTemplatePageSource();
Resource curr = ps.getResource();
Resource dir = curr.getParentResource();
Resource cssDir = dir.getRealResource("css");
Resource pathdir = cssDir;
cssDir.mkdirs();
//the base name for the files we are going to create as a css and image
String baseRenderedFileName = MD5.getDigestAsString(_ids);
Resource cssFileName = cssDir.getRealResource(baseRenderedFileName+".css");
Resource imgFileName = pathdir.getRealResource(baseRenderedFileName+"."+ResourceUtil.getExtension(src,""));
//if the files don't exist, then we create them, otherwise
boolean bCreate = !cssFileName.isFile() || !imgFileName.isFile();
//Are we going to create it, let's say no
String css = "";
if(bCreate){
int imgMaxHeight = 0;
int imgMaxWidth = 0;
Image img;
int actualWidth,actualHeight;
//Setup the max height and width of the new image.
for(int i=0;i<srcs.length;i++){
img = images[i];
//set the image original height and width
actualWidth = img.getWidth();;
actualHeight = img.getHeight();
//Check if there is a height,
imgMaxHeight += actualHeight;
if(actualWidth > imgMaxWidth) imgMaxWidth = actualWidth;
}
//Create the new image (hence we needed to do two of these items)
Image spriteImage = (Image) ImageNew.call(pageContext,"", ""+imgMaxWidth,""+imgMaxHeight, "argb");
int placedHeight = 0;
//Loop again but this time, lets do the copy and paste
for(int i=0;i<srcs.length;i++){
img = images[i];
spriteImage.paste(img,1,placedHeight);
css += "#"+ids[i]+" {\n\tbackground: url("+baseRenderedFileName+"."+ResourceUtil.getExtension(strSrcs[i],"")+") 0px -"+placedHeight+"px no-repeat; width:"+img.getWidth()+"px; height:"+img.getHeight()+"px;\n} \n";
placedHeight += img.getHeight();
}