用php切割大圖片為成規則的小圖

來源:互聯網
上載者:User

標籤: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切割大圖片為成規則的小圖

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.