透過網頁讀取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...

12,596 Responses

  1. Davidtes表示:

    cost propecia without rx order generic propecia without rx cost of generic propecia for sale

  2. rolap_faet表示:

    ролл ап заказать в москве rollap.ru .

  3. Топовая Букмекерская контора в мире 1win

  4. WilliamRhype表示:

    https://lisinopril.network/# lisinopril 20mg prices

  5. Ronaldpsymn表示:

    http://finasteride.store/# cheap propecia tablets

  6. RandallTaulp表示:

    lisinopril 10mg daily: buy lisinopril 2.5 mg online – lisinopril 420 1g

  7. Davidtes表示:

    tamoxifen bone density tamoxifen headache tamoxifen postmenopausal

  8. Davidtes表示:

    Cytotec 200mcg price buy cytotec online п»їcytotec pills online

  9. RandallTaulp表示:

    cost of propecia no prescription: cheap propecia online – cheap propecia for sale

  10. Ronaldpsymn表示:

    http://cytotec.club/# cytotec online

  11. RandallTaulp表示:

    tamoxifen side effects forum: tamoxifen warning – nolvadex gynecomastia

  12. WilliamRhype表示:

    https://nolvadex.life/# does tamoxifen cause menopause

  13. RandallTaulp表示:

    tamoxifen: nolvadex steroids – tamoxifen moa

  14. Ronaldpsymn表示:

    http://ciprofloxacin.tech/# п»їcipro generic

  15. Davidtes表示:

    tamoxifen headache tamoxifen cyp2d6 clomid nolvadex

  16. WilliamRhype表示:

    https://nolvadex.life/# tamoxifen and uterine thickening

  17. Ronaldpsymn表示:

    http://lisinopril.network/# lisinopril 10mg price in india

  18. Davidtes表示:

    lisinopril online without prescription lisinopril 20 mg 12.5 mg zestril no prescription

  19. WilliamRhype表示:

    https://ciprofloxacin.tech/# ciprofloxacin mail online

  20. RandallTaulp表示:

    buy cytotec online: buy cytotec over the counter – buy cytotec pills online cheap

  21. RandallTaulp表示:

    buy cytotec: cytotec pills buy online – buy cytotec in usa

  22. Davidtes表示:

    lisinopril 25 mg price prinivil online lisinopril 10 mg coupon

  23. RandallTaulp表示:

    cytotec pills buy online: Abortion pills online – buy cytotec

  24. Jamesarbit表示:

    На сайте коллегии юристов http://zpp-1.ru/ вы найдете контакты и сможете связаться с адвокатами. Юрист расскажет о том, как нужно правильно поступить, поможет собрать необходимые документы и будет защищать ваши права в суде. Квалифицированная юридическая и медицинская поддержка призывникам с гарантией!

  25. 1win883piodo表示:

    Топ Букмекерская контора, всем советую 1win

  26. WilliamRhype表示:

    https://finasteride.store/# cost of cheap propecia without insurance

發佈留言

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