透過網頁讀取Photoshop PSD檔案內容

在專案中有需要透過網頁來讀取Photoshop PSD檔案的內部資訊,我們透過PSD.js備用載點)來實現這樣的功能,在官方提供的功能展示頁中,我們可以看到在頁面中只需引入psd.min.js後,加入透過少量的Javascript就可以針對PSD進行解析。

<!DOCTYPE html>
<html>
<head>
  <title>psd.js image example</title>
  <style type="text/css">
  body, html {
    padding: 0;
    margin: 0;
  }
  #dropzone {
    width: 500px;
    height: 100px;
    border: 1px #ababab dashed;
    margin: 50px auto;
  }
  #dropzone p {
    text-align: center;
    line-height: 100px;
    margin: 0;
    padding: 0;
  }
  #image {
    text-align: center;
  }
  </style>
  <script type="text/javascript" src="psd.min.js"></script>
</head>
<body>
  <div id="dropzone">
    <p>Drop here</p>
  </div>
  <div id="image"></div>
  <pre id="data"></pre>
  <script type="text/javascript">
  (function () {
    var PSD = require('psd');
    document.getElementById('dropzone').addEventListener('dragover', onDragOver, true);
    document.getElementById('dropzone').addEventListener('drop', onDrop, true);
    function onDragOver(e) {
      e.stopPropagation();
      e.preventDefault();
      e.dataTransfer.dropEffect = 'copy';
    }
    function onDrop(e) {
      e.stopPropagation();
      e.preventDefault();
      PSD.fromEvent(e).then(function (psd) {
        var data = JSON.stringify(psd.tree().export(), undefined, 2);
        document.getElementById('data').innerHTML = data;
        document.getElementById('image').appendChild(psd.image.toPng());
      });
    }
  }());
  </script>
</body>
</html>

我們透過下圖這樣的一個PSD檔案對頁面進行測試,檔案中共有三個圖層,包含一個文字圖層。

頁面將PSD分析出結果後,以json格式顯示在頁面上,可以看到文字圖層的部分,不僅能讀到圖層名稱也包含文字內容和字型名稱。

{
  "children": [
    {
      "type": "layer",
      "visible": true,
      "opacity": 1,
      "blendingMode": "normal",
      "name": "Photoshop Text",
      "left": 302,
      "right": 723,
      "top": 90,
      "bottom": 128,
      "height": 38,
      "width": 421,
      "mask": {},
      "text": {
        "value": "THIS IS TEXT LAYER",
        "font": {
          "name": "MyriadPro-Regular\u0000",
          "sizes": [
            53
          ],
          "colors": [
            [
              0,
              0,
              0,
              255
            ]
          ],
          "alignment": [
            "center"
          ]
        },
        "left": 0,
        "top": 0,
        "right": 0,
        "bottom": 0,
        "transform": {
          "xx": 1,
          "xy": 0,
          "yx": 0,
          "yy": 1,
          "tx": 513,
          "ty": 127
        }
      },
      "image": {}
    },
    {
      "type": "layer",
      "visible": true,
      "opacity": 1,
      "blendingMode": "normal",
      "name": "PSD Icon",
      "left": 287,
      "right": 738,
      "top": 209,
      "bottom": 669,
      "height": 460,
      "width": 451,
      "mask": {},
      "image": {}
    },
    {
      "type": "layer",
      "visible": true,
      "opacity": 1,
      "blendingMode": "normal",
      "name": "Background",
      "left": 0,
      "right": 1024,
      "top": 0,
      "bottom": 800,
      "height": 800,
      "width": 1024,
      "mask": {},
      "image": {}
    }
  ],
  "document": {
    "width": 1024,
    "height": 800,
    "resources": {
      "layerComps": [],
      "guides": [],
      "slices": []
    }
  }
}

You may also like...

41,226 Responses

  1. Williamuniot表示:

    пинап казино – пин ап казино

  2. Williamuniot表示:

    пин ап вход – пин ап казино зеркало

  3. Dwayneseisa表示:

    Officiele Kamagra van Nederland Kamagra Kopen Kamagra

  4. Williamuniot表示:

    пин ап вход – пинап казино

  5. Williamuniot表示:

    pinup 2025 – пин ап зеркало

  6. Dwayneseisa表示:

    cialis without a doctor prescription Buy Tadalafil 5mg TadalafilEasyBuy.com

  7. Jamesavata表示:

    пин ап казино зеркало – пин ап казино

  8. mostbet表示:

    Hráči ocenují rychlé výběry u Mostbet | Nejlepší automaty najdete v Mostbet casino cz | Mostbet se českými hráči opravdu počítá mostbet official website.

  9. by88表示:

    Thanks for your write-up. Another factor is that to be a photographer entails not only difficulty in taking award-winning photographs but in addition hardships in getting the best digicam suited to your needs and most especially hardships in maintaining the grade of your camera. This really is very true and evident for those photography fans that are into capturing the actual nature’s engaging scenes – the mountains, the forests, the wild or the seas. Visiting these daring places undoubtedly requires a camera that can surpass the wild’s hard settings.

  10. JesseDex表示:

    пинап казино: https://pinupkz.life/

  11. Jamesavata表示:

    pinup 2025 – пин ап казино зеркало

  12. Jamesavata表示:

    пин ап казино – пин ап зеркало

  13. Davidbop表示:

    Upshift Finance is a next-generation decentralized trading platform designed to provide secure, fast, and efficient crypto transactions. With smart contract automation, low transaction fees, and seamless integration with DeFi protocols, Upshift Finance empowers traders to swap digital assets and execute trades with maximum security. Whether you’re a beginner or an experienced trader, Upshift Finance offers a powerful, transparent, and user-friendly trading ecosystem. https://upshift.ink

  14. JesseDex表示:

    пинап казино: https://pinupkz.life/

  15. TerryDus表示:

    https://tadalafileasybuy.com/# TadalafilEasyBuy.com

  16. Jamesimini表示:

    UnagiSwap is a cutting-edge decentralized exchange (DEX) that provides fast, secure, and transparent crypto trading. Designed for traders looking to swap digital assets efficiently without intermediaries, UnagiSwap offers low fees, deep liquidity, and seamless smart contract execution. Whether you’re a casual trader or a professional investor, UnagiSwap’s non-custodial platform ensures full control over your assets in a decentralized environment. https://unagiswap.org

  17. Davidhunty表示:

    Perena is a cutting-edge blockchain development platform designed to empower decentralized applications (dApps) with scalable and secure solutions. Built for Web3 innovators, Perena blockchain solutions offer seamless integration, high-performance smart contracts, and decentralized infrastructure. Whether you’re building DeFi protocols, NFT marketplaces, or enterprise blockchain applications, Perena provides the tools needed to scale with confidence. https://perena.tech

  18. GregoryJough表示:

    Generic 100mg Easy: buy generic 100mg viagra online – Generic 100mg Easy

  19. GregoryJough表示:

    Kamagra: kamagra 100mg kopen – kamagra 100mg kopen

  20. WilliamNop表示:

    TadalafilEasyBuy.com: TadalafilEasyBuy.com – Generic Cialis without a doctor prescription

  21. WilliamNop表示:

    Viagra without a doctor prescription Canada: Buy Viagra online cheap – Generic100mgEasy

  22. Williamviacy表示:

    Nucleus Earn is revolutionizing DeFi staking and passive income generation by offering secure, high-yield crypto rewards. With smart contract-powered staking pools, Nucleus Earn allows users to earn rewards effortlessly while maintaining full control over their assets. Whether you’re a beginner or an experienced investor, Nucleus Earn’s decentralized staking platform ensures transparency, security, and optimal returns in the fast-growing world of DeFi. https://nucleusearn.org

  23. ArthurMen表示:

    Kra30.cc – кракен ссылка, kra30 сс

  24. TerryDus表示:

    https://tadalafileasybuy.com/# TadalafilEasyBuy.com

  25. TerryDus表示:

    http://tadalafileasybuy.com/# cialis without a doctor prescription

  26. GregoryJough表示:

    kamagra kopen nederland: Kamagra Kopen – kamagra pillen kopen

  27. TerryDus表示:

    https://kamagrakopen.pro/# kamagra kopen nederland

  28. Kennethslity表示:

    https://generic100mgeasy.shop/# Viagra without a doctor prescription Canada

  29. WillieSor表示:

    kra30 cc – kra32, кра ссылка

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。