PHP 設置PNG DPI爲300DPI

這個只能是說沒有被識別爲300DPI的 識別爲300DPI 若是本質是72DPI的 是沒有用的哦json

function setDPI($filename){

    $file = file_get_contents($filename);

//數據塊長度爲9
    $len = pack("N", 9);
//數據塊類型標誌爲pHYs
    $sign = pack("A*", "pHYs");
//X方向和Y方向的分辨率均爲300DPI(1像素/英寸=39.37像素/米),單位爲米(0爲未知,1爲米)
    $data = pack("NNC", 300 * 39.37, 300 * 39.37, 0x01);
//CRC檢驗碼由數據塊符號和數據域計算獲得
    $checksum = pack("N", crc32($sign . $data));
    $phys = $len . $sign . $data . $checksum;

    $pos = strpos($file, "pHYs");
    if ($pos > 0) {
        //修改pHYs數據塊
        $file = substr_replace($file, $phys, $pos - 4, 21);
    } else {
        //IHDR結束位置(PNG頭固定長度爲8,IHDR固定長度爲25)
        $pos = 33;
        //將pHYs數據塊插入到IHDR以後
        $file = substr_replace($file, $phys, $pos, 0);
    }

    file_put_contents($filename,$file);
}```

參考文獻:http://www.bejson.com/convert/ox2str/ 【16進制轉ASCII碼】    
https://my.oschina.net/xiaoshuo/blog/209349 【推薦】    
【PNG圖片格式解析】http://book.51cto.com/art/200903/112725.htm
相關文章
相關標籤/搜索