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

13,220 Responses

  1. rafgill表示:

    Why do we need a Healthcare Electronic Software?
    Medicine is one of the oldest professions. The first physicians recorded medical data first by using papyrus or parchment to preserve written information. In 1650, one man designed and built a medical computer which is called the EAR. This computer was built by Emmanuel Lebret who was backed by one of the greatest Insulin inventor, Dr Andreas Libavius.

    In modern terms, a clinical knowledge management system refers to any enterprise- https://indiatownship.com/wp-content/uploads/2022/06/caidif.pdf
    ec5d62056f rafgill

  2. orfogran表示:

    The media pendulum is continuously swinging, even if it has been at a slower pace in certain aspects than it would have been when it starts to swing on average cycle. Therefore, now is the right time for digital music fans who are looking for a solution that allows them to personalize their streaming experience by offering customizable, multi-room Plex Home Theater support.

    In 2014 the media pendulum took the average cycle to its extreme by bringing consumer-grade devices into the average home’s http://one-health24.com/wp-content/uploads/2022/06/leirana.pdf
    ec5d62056f orfogran

  3. Exujdd表示:

    hydroxychloroquine 400mg oral – chloroquine online order baricitinib 2mg without prescription

  4. wlyncle表示:

    The product is designed with the remote in mind, so that your mouse can be used to control and adjust volume. As such, the remote is surprisingly well-built. With four buttons, it can be easily used in conjunction with your mouse. It is covered in bright finish to go well with any device. Small, solid and easy to use, it is perfect for those on the move. The shape of the device itself looks somewhat rounded, and also has a USB-A port on https://www.buddhistchurchesofamerica.org/profile/Android-Language-Project-Full-2021/profile
    66cf4387b8 wlyncle

  5. Vayrzp表示:

    order lasix 100mg pill – buy furosemide 100mg pills ivermectin usa

  6. orscar表示:

    Best Free Audio Converter

    The best audio converter is one that will make my audio work and a good one at that, I don’t pay money to convert my music files, I want the work to be done effortlessly without spending a fortune! I have tried a lot of converters and this is by far the best one, in terms of both audio quality and speed. Also, the audio quality after conversion is pristine and I can play my music anywhere! I am a https://www.legumestranslated.eu/profile/grazsisriupringende/profile
    66cf4387b8 orscar

  7. Qplnkh表示:

    purchase lasix – order generic lasix ivermectin for sale online

  8. crismari表示:

    With Xor, enciphering files with a specific key is easy, and can be done by using the /c switch. Within the command line, the user is required to put in the infos they want to be enciphered through the /k switch, and specified as a string.
    After that, a second and third switch follows, to specify that which bits of text need to be enciphered, and using the /x switch. For instance, a string of text https://www.sport-break.fr/profile/Ek-Daav-Dhobi-Pachad-Marathi-Movie-Download-LINK/profile
    66cf4387b8 crismari

  9. ulrhun表示:

    The lack of features keeps the application from being more than good, unless you plan on using it to get a bit of knowledge on a few scales and modes.
    FtEditor is an SFX editor and provides basic functionalities, but only as a template. It can be used to edit the original SFX file contained in a DXF or built in, but this is very limited. You can use it to edit the position of the SFX file in the shot, so we can find https://www.radiodoumdoum.com/profile/asfrantabobricomp/profile
    99d5d0dfd0 ulrhun

  10. Fvactg表示:

    order furosemide 100mg pill – oral doxycycline 100mg stromectol price

  11. Tultdt表示:

    prednisolone 10mg usa – buy gabapentin sale sildenafil 100mg drug

  12. Tujjvs表示:

    prednisolone 5mg canada – generic prednisolone 5mg sildenafil 100mg

  13. Zsvpiq表示:

    cost prednisolone 10mg – purchase gabapentin without prescription buy sildenafil 150mg pills

  14. Kgwotz表示:

    order generic amoxil 250mg – amoxil online order order generic sildenafil 150mg

  15. Bajqqa表示:

    amoxil online – azithromycin 250mg cheap sildenafil 25 mg

  16. Hsgabi表示:

    amoxicillin online – order azithromycin 500mg buy viagra 150mg without prescription

  17. Cjzocp表示:

    deltasone buy online – order prednisone 10mg generic buy accutane 10mg generic

  18. AndrewEcork表示:

    cialis free trial voucher buy tadalafil cialis with dapoxetine to buy uk

  19. Szhvgz表示:

    deltasone 10mg cost – order isotretinoin 20mg generic order isotretinoin 10mg sale

  20. AndrewEcork表示:

    cialis 10mg generic buy tadalafil cialis generic 10 mg

  21. Zznvps表示:

    cost deltasone 40mg – deltasone 10mg ca order isotretinoin 20mg generic

  22. free表示:

    I don’t normally comment but I gotta say appreciate it for the post on this one : D.

  23. live表示:

    Of course, what a great site and informative posts, I will add backlink – bookmark this site? Regards, Reader

發佈留言

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