標籤:des style blog http ar color 使用 sp for
將根據xml配置,將合并後的大圖切割成一系列小圖
<?php/** * 將大圖片按照配置切割成一定比例的小圖片 * 並按照一定規則給小圖片命名 * * 使用方法: *根據guardians/g1目錄下的Attack_1.xml配置切割Attack_1.png *也可批量切割(我寫的是批量切割) * * 註:需要GD2支援 */// echo "{${phpinfo()}}";header("Cache-Control:no-cache,must-revalidate"); //不使用緩衝for ($i=1; $i < 100; $i++) { $filename="guardians/g".$i."/Attack_1.png";//大圖檔案 $tempdir="temp";//小圖存放目錄 //判斷檔案是否存在 不存在就切割完畢 if(file_exists($filename)){ if(!file_exists($tempdir)) mkdir($tempdir); } $xml=simplexml_load_file("guardians/g".$i."/Attack_1.xml"); echo "guardians/g".$i."/Attack_1.xml<br>"; $j = 1; foreach($xml -> SubTexture as $SubTexture){ $attri = $SubTexture->attributes(); $picW=$attri->frameWidth; //切割小圖的寬 $picH=$attri->frameHeight; //為支援大圖片增加記憶體限制 ini_set( ‘memory_limit‘, ‘220M‘ ); //切割小圖的高 echo $picW.",".$picH."<br>"; list($width, $height, $type, $attr) = getimagesize($filename); $image = imagecreatefrompng($filename); //透明背景 $im = imagecreatetruecolor((int)$picW, (int)$picH) or die("Cannot Initialize new GD image stream");//建立小映像 imagealphablending($im, false); imagesavealpha($im, true); $white = imagecolorallocatealpha($im,255,255,255,127); imagefill($im,0,0,$white); $picX=$attri->width;//擷取截取圖片的寬度 $picY=$attri->height;//擷取截取圖片的高度 echo $picX.",".$picY."<br>"; $frameX = $attri->frameX; $frameY = $attri->frameY; $x = $attri->x; $y = $attri->y; echo $frameX.",".$picY."<br>"; imagecopy ( $im, $image, -(int)$frameX, -(int)$frameY, (int)$x, (int)$y, (int)$picX, (int)$picY );//拷貝大圖片的一部分到小圖片 imagepng($im,$tempdir."/g".$i."_Attack_1_".$j.".png",0, 75);//建立小圖片到磁碟,輸出品質為75(0~100) echo $tempdir."/g".$i."_Attack_1_".$j.".png". "<br>"; $j = $j + 1; imagedestroy($im);//釋放與 $im 關聯的記憶體 imagedestroy($image);//釋放與 $image 關聯的記憶體 }}echo " complate";?>
完成後會在temp目錄下產生小圖,確保temp目錄是要寫的許可權
檔案在這裡
用php切割大圖片為成規則的小圖