透過網頁讀取PSD後,依圖層輸出PNG檔案

在前面介紹過網頁可以讀取PSD檔案並擷取到該檔案的資訊,接著我們會希望能透過網頁上傳PSD檔之後,依照圖層來輸出圖片,同樣是利用PSD.js來輔助進行,可以透過我們製作的範例頁面來進行輸出測試。

預設輸出效果


透過PSD.js可將PSD內的各圖層輸出成PNG圖檔,亦可轉成Base 64圖檔格式,惟輸出時每個圖檔的尺寸並不會根據整體PSD檔大小來配置。

圖層配置

Layer 1 (421 x 38)
Layer 2 (490 x 479)
Layer 3 (1024 x 800)

<!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 PSD here</p>
  </div>
  <div id="image"></div>
  <pre id="data"></pre>

  <script type="text/javascript">
  (function () {
    const 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) {
		for (var i = 0; i < psd.layers.length; i ++){
			document.getElementById('image').appendChild(psd.layers[i].image.toPng());
		}
      });
    }
	
  }());
  </script>
</body>
</html>

依照整體PSD配置進行輸出


我們希望讓每個圖片在輸出後能保時相同的尺寸,也就是依照PSD的畫布大小來輸出每一張圖檔,但在原生PSD.js中並不具備這樣的功能,於是我們透過下述的方式來達成:

  1. 擷取PSD資訊並將圖檔轉為Base 64格式
  2. 產生與PSD尺寸大小相同的HTML Canvas
  3. 將產生的Base 64圖檔,依照原始位置放入Canvas中
  4. 將Canvas轉成PNG圖檔
圖層配置
Layer 1 (1024 x 800)
Layer 2 (1024 x 800)
Layer 3 (1024 x 800)

可以參考我們製作的範例頁面

<!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 PSD here</p>
  </div>
  <div id="image"></div>
  <pre id="data"></pre>
  <script type="text/javascript">
  (function () {
    const 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) {
    const PSDWidth = psd.tree().width;
    const PSDHeight = psd.tree().height;
    for (var i = 0; i < psd.layers.length; i ++){
      const img = new Image();
      img.src = psd.layers[i].image.toBase64();
      console.log(psd);
      const layerWidth = psd.layers[i].width;
      const layerHeight = psd.layers[i].height;
      const layerLeft = psd.layers[i].left;
      const layerTop = psd.layers[i].top;
      
      const canvas = document.createElement("canvas");
      canvas.setAttribute('class', "canvas");
      canvas.width = PSDWidth;
      canvas.height = PSDHeight;
      console.log("canvas :", canvas);
      img.onload = function(){
        canvas.getContext("2d").drawImage(img, layerLeft, layerTop, layerWidth, layerHeight);
        document.getElementById('image').appendChild(canvas);
      }
    }
      });
    }
  
  }());
  </script>
</body>
</html>

PSD檔案輸出限制


1. 無法輸出帶有效果的圖片

在PSD中我們可能會針對圖層套用一些效果,例如:陰影、光暈、筆畫…等等,但在輸出後的圖檔將不會帶有這些效果。

圖層設定

解決方法:透過點陣化圖層效果,即可輸出相對應的圖檔。

2. 無法輸出帶有遮色片效果的圖片

圖層設定
Before
After

解決方法:將圖層轉為智慧型物件,即可輸出相對應的圖檔。

3. 無法依圖層混合模式輸出圖片

Layer 1
Layer 2
Layer 3
Layer 4

解決方法:無法解決

You may also like...

47,314 Responses

  1. Superb blog! Do you have any tips for aspiring writers?
    I’m planning to start my own site soon but I’m a little lost on everything.
    Would you recommend starting with a free platform like WordPress or go
    for a paid option? There are so many choices out there that
    I’m totally overwhelmed .. Any tips? Many thanks!

  2. Why viewers still use to read news papers when in this technological world everything is available on net?

  3. 15 Best Twitter Accounts To Discover More About
    Malpractice Attorneys malpractice law firms

  4. This paragraph will help the internet users for building up new weblog or
    even a weblog from start to end.

  5. First off I would like to say wonderful blog! I had a quick question which I’d
    like to ask if you don’t mind. I was curious to know
    how you center yourself and clear your mind before writing.
    I’ve had trouble clearing my mind in getting my ideas out there.
    I do enjoy writing however it just seems like the first
    10 to 15 minutes tend to be wasted simply just trying to figure out how to begin. Any recommendations or tips?
    Cheers!

  6. Good day! I just wish to give you a big thumbs up for the excellent information you’ve got here
    on this post. I’ll be returning to your site for more
    soon.

  7. It’s remarkable in favor of me to have a web page, which is valuable for my experience.
    thanks admin

  8. Howdy! I know this is kind of off topic but I was wondering which blog
    platform are you using for this website? I’m getting fed up of WordPress because
    I’ve had problems with hackers and I’m looking at options for another platform.
    I would be great if you could point me in the direction of a good platform.

  9. Everyone loves what you guys tend to be up too. This type of
    clever work and coverage! Keep up the very good works guys I’ve you guys to my
    own blogroll.

  10. Greetings! Very helpful advice within this article! It’s
    the little changes which will make the largest changes.
    Thanks a lot for sharing!

  11. If you would like to increase your know-how simply keep
    visiting this site and be updated with the hottest news update posted here.

  12. I’m extremely inspired along with your writing abilities as smartly as with the format for
    your weblog. Is this a paid subject or did you modify it your self?
    Anyway keep up the excellent quality writing, it’s uncommon to peer a
    great weblog like this one these days..

  13. Excellent web site. Lots of useful info here. I’m sending it to several pals ans additionally sharing
    in delicious. And obviously, thank you to your sweat!

  14. Hey! I could have sworn I’ve been to this blog before but after
    reading through some of the post I realized it’s new to me.

    Anyways, I’m definitely delighted I found it and I’ll be book-marking and
    checking back often!

  15. Hi there, everything is going sound here and ofcourse every one is sharing information, that’s actually fine, keep up writing.

  16. Attractive section of content. I just stumbled upon your website and in accession capital to assert that
    I get actually enjoyed account your weblog posts. Anyway I will be
    subscribing to your augment and even I fulfillment you get admission to persistently quickly.

  17. It’s nearly impossible to find well-informed people for this topic, but you sound like
    you know what you’re talking about! Thanks

  18. What’s Taking place i am new to this, I stumbled upon this I have found It absolutely useful and
    it has helped me out loads. I am hoping to contribute & aid different users like
    its helped me. Good job.

  19. My brother suggested I might like this web site. He was totally right.
    This post truly made my day. You cann’t imagine simply how much time I had spent
    for this information! Thanks!

  20. It is in point of fact a great and helpful piece of information. I’m glad that
    you just shared this useful info with us. Please stay us up to date like this.
    Thanks for sharing.

  21. jutitiu.site表示:

    Hello! Someone in my Myspace group shared this website with us so I came to look it over.
    I’m definitely loving the information. I’m bookmarking and will be tweeting this to my followers!
    Outstanding blog and wonderful style and design.

  22. This is very interesting, You’re a very skilled blogger.
    I have joined your rss feed and look forward to seeking more of your great post.
    Also, I have shared your website in my social networks!

  23. Very soon this site will be famous amid all blogging and site-building viewers, due
    to it’s fastidious articles or reviews

  24. joew.site表示:

    It’s amazing to go to see this website and reading the views of all friends concerning this paragraph, while
    I am also eager of getting familiarity.

  25. What a information of un-ambiguity and preserveness of precious know-how on the topic of unexpected feelings.

  26. Thanks to my father who stated to me about this weblog, this
    blog is truly amazing.

  27. You need to take part in a contest for one of the best blogs on the web.
    I most certainly will highly recommend this site!

  28. I have been exploring for a little for any high-quality articles or blog posts on this kind of
    house . Exploring in Yahoo I ultimately stumbled upon this website.

    Studying this info So i am happy to show that I’ve a very excellent uncanny feeling I found
    out exactly what I needed. I most definitely will make certain to don?t omit this
    website and provides it a glance on a constant basis.

  29. Can you tell us more about this? I’d love to find out more details.

  30. Hey there! Do you use Twitter? I’d like to
    follow you if that would be okay. I’m undoubtedly enjoying your blog and look forward
    to new posts.

發佈留言

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