透過網頁讀取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,194 Responses

  1. Today, I went to the beach front with my kids. I found a sea shell
    and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She put the shell
    to her ear and screamed. There was a hermit crab inside and it pinched her ear.
    She never wants to go back! LoL I know this is totally off topic but I had
    to tell someone!

  2. Your style is very unique compared to other people I have read stuff from.
    Many thanks for posting when you’ve got the
    opportunity, Guess I’ll just bookmark this site.

  3. Good respond in return of this query with solid arguments and describing the whole thing regarding that.

  4. My brother suggested I would possibly like this website. He used to be entirely right.
    This post truly made my day. You can not consider
    simply how so much time I had spent for this info!
    Thanks!

  5. I blog often and I truly thank you for your information. This great article has
    really peaked my interest. I am going to bookmark your website and keep checking for new details about once
    per week. I opted in for your RSS feed as well.

  6. Hello! I know this is kinda off topic but I was wondering if you knew where I could find a captcha
    plugin for my comment form? I’m using the same blog platform as yours
    and I’m having difficulty finding one? Thanks a lot!

  7. Hi there, all is going well here and ofcourse every one is sharing information, that’s in fact excellent, keep
    up writing.

  8. Howdy I am so happy I found your site, I really found you by
    accident, while I was searching on Google for something else, Regardless I am here now and would just like to say many thanks for a tremendous post and a all
    round enjoyable blog (I also love the theme/design), I don’t have time to read through it all at the moment but I
    have book-marked it and also added in your
    RSS feeds, so when I have time I will be back to read a great deal more, Please do keep
    up the awesome work.

  9. kulor.site表示:

    An impressive share! I have just forwarded this onto a friend who had been doing
    a little homework on this. And he actually bought me breakfast simply
    because I found it for him… lol. So allow me to
    reword this…. Thanks for the meal!! But yeah, thanx for
    spending time to discuss this subject here on your blog.

  10. As the admin of this site is working, no doubt very quickly it will
    be renowned, due to its feature contents.

  11. Useful info. Fortunate me I found your website by chance, and I am shocked why this twist of
    fate didn’t happened earlier! I bookmarked it.

  12. I think this is among the such a lot important info for me.
    And i am glad studying your article. However wanna
    remark on some common issues, The web site style is perfect,
    the articles is truly great : D. Excellent task, cheers

  13. 7 Things About Malpractice Attorneys You’ll Kick Yourself For Not Knowing Medical Malpractice law Firm

  14. Thanks for sharing your thoughts about https://jenisfrankos.site. Regards

  15. Heya just wanted to give you a brief heads up and let you know a
    few of the images aren’t loading properly. I’m not sure why but I think its
    a linking issue. I’ve tried it in two different internet browsers and both
    show the same outcome.

  16. Hello, i believe that i noticed you visited my web site so i came to go back the prefer?.I’m trying to
    to find things to enhance my website!I guess its good enough to use some of your ideas!!

  17. Yes! Finally someone writes about https://moonlitapp.site.

  18. Отличный сатй! Всем рекомендую!вскрытие замков

  19. May I simply say what a comfort to uncover somebody who actually understands what they’re talking
    about over the internet. You actually know how
    to bring an issue to light and make it important.
    More people need to read this and understand this side of the story.

    I was surprised that you’re not more popular given that you certainly possess the gift.

  20. You’re so interesting! I do not suppose I’ve read
    through something like that before. So great to discover
    someone with some unique thoughts on this subject matter.

    Seriously.. thanks for starting this up. This web site is something
    that is required on the web, someone with some originality!

  21. Great post but I was wondering if you could write a litte more on this subject?
    I’d be very thankful if you could elaborate a little bit more.

    Thank you!

  22. hey表示:

    My partner and I stumbled over here from a different website
    and thought I might as well check things out.
    I like what I see so i am just following you. Look forward to exploring your web page for a second time.

  23. Excellent way of explaining, and pleasant post to
    obtain facts on the topic of my presentation subject, which i am going to present in college.

  24. It’s appropriate time to make some plans for the future and it’s time to be happy.
    I have read this post and if I could I wish to suggest you some interesting things or tips.
    Maybe you could write next articles referring to this article.
    I wish to read more things about it!

  25. Hi it’s me, I am also visiting this web page on a
    regular basis, this website is really fastidious and the people are
    actually sharing good thoughts.

  26. What Is The Reason? Veterans Disability Claim Is Fast Becoming The Most Popular Trend In 2023?
    Veterans Disability Attorneys

  27. Excellent article. I am dealing with some of these issues as well..

  28. 15 Current Trends To Watch For Upvc Door Panel Upvc Door Replacement Panel

發佈留言

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