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

46,872 Responses

  1. I’ve read this publish and if I may just I wish to suggest you fewattention-grabbing issues or advice.Maybe you can write next articles referringto this article.セックス ドール

  2. 人形 エロ表示:

    only around 25 percent of women are consistently orgasmic from intercourse alone.高級 ラブドールThe other 75 percent need kissing,

  3. if not many times.It’s that feeling like no other of being swept away on a magic carpet ride into the sunset with this one very special person.ラブドール 中古

  4. 人形 エロ表示:

    People criticize Sebastian for exposing herself to risks,ラブドール えろincluding STIs,

  5. java burn表示:

    This post offers clear idea for the new users of blogging, that in fact how to do blogging.

  6. ラブドール えろThe (several) guys who said they’d be sex workers if they were women.have told me if they were a girl they would be doing just what I do.

  7. In the mental health space,ラブドール おすすめthe saying of “fake it till you make it” has a very narrow and specific meanin It is most usually directed towards a specific behavior or pattern of behavior that people engage in—ones that do not serve their overall best interests and might be described as maladaptive.

  8. 人形 エロ表示:

    The obsession with another can go as far as needing to control a partner’s every action—needing them around all the time,keeping tabs on them,ラブドール エロ

  9. ラブドール えろThe fact that I was competent in my job as a teacher was never called into question.Even so,

  10. 人形 エロ表示:

    at a bar,you can sit and wait for a man to approach you,ラブドール えろ

  11. オナホ ラブドールIf boys learned anything from adults,it was that their penis is a private thing and any activity that involved it should not be discussed or shared with others.

  12. 人形 エロ表示:

    Additionally,ラブドール 中古the belief that self-expansion can occur in the future even if a particular day is relatively humdrum could provide an arc that would allow you to remain optimistic that growth with your partner will occur.

  13. you have to apply via the site,ラブドール えろupload some photos,

  14. It’s going to be finish of mine day, but before finish I am reading this impressive piece of writing
    to increase my knowledge.

  15. pokervqq表示:

    It’s actually a cool and useful piece of info. I’m satisfied that you just shared this helpful info with us.
    Please stay us informed like this. Thank you for sharing.

  16. even if you’re feeling anxious.Now,リアル ドール

  17. การ พนัน “หวยฮานอย” เป็นอีกทางเลือก หนึ่งที่ได้รับ ความปรารถนา จาก คนในสังคมไทย ในการ ลุ้นรางวัล เมื่อ เหมือนกับ การเล่น หวยรัฐบาลหรือ”หวยลาว”
    ความ น่าดึงดูด ของ”หวยฮานอย”คือ การ
    จัดการออกรางวัล ทุกวัน ส่งผลให้ผู้ เสี่ยง
    สามารถ ได้รับ ได้บ่อยครั้ง และ มีเหตุผล สร้าง รายได้ใหม่ จากการ ซื้อ หวย

    อย่างไรก็ตาม การเล่น
    “หวยฮานอย” ก็ไม่ ข้อเสีย เนื่องจากผู้ ลุ้น บางรายอาจใช้เงิน มากเกินไปหรือ
    หลงใหล การพนัน ซึ่งอาจส่งผลกระทบ
    ต่อ ชีวิต นอกจากนี้
    ยังมี ความเป็นไปได้ เรื่อง
    การถูกฉ้อราษฎร์บังหลวง จากผู้ที่แสวงหาประโยชน์ โดยมิชอบ

    เพื่อให้การ ลุ้น “หวยฮานอย” เป็นเพียงการ ลุ้นรับรางวัล เพื่อ ความเพลิดเพลิน และ
    ไม่ส่งผลกระทบ จึงควรมีการ กำกับ และ ตรวจสอบ อย่างใกล้ชิด เช่น การ วางแผน ระยะเวลา ในการ ทำ ที่ ถูกต้อง รวมถึงการ
    ลงโทษ ผู้กระทำผิด ทั้งนี้เพื่อให้การ เสี่ยง
    “หวยฮานอย” เป็นส่วนหนึ่งของการ
    ใช้เวลา อย่าง ระลึกถึงผู้อื่น และ ไม่ทำให้เดือดร้อน ชีวิต ของผู้ ซื้อ

    My web site; หวยฮานอย ออกวันไหนบ้าง

  18. pkv games表示:

    You made some really good points there. I checked on the internet to find
    out more about the issue and found most individuals will go along with
    your views on this website.

  19. As a marriage and family therapist in Santa Monica,I treat many people with passive-aggressive anger issues.ラブドール 中古

  20. Puravive表示:

    Magnificent goods from you, man. I have understand your stuff
    previous to and you’re just extremely fantastic. I
    really like what you’ve acquired here, really like what you are stating and the way in which you say it.
    You make it enjoyable and you still take care
    of to keep it sensible. I can not wait to read much more from you.

    This is actually a terrific site.

  21. Checking out our interior fantasies in a safe environment and in a consensual,人形 セックスrespectful manner.

  22. ラブドール オナホSome encouraged women to support other women.They hold the money and power to control the people (i.

  23. Now,let’s ask the “cougar cubs” whether their perceptions of the sexual encounters matched those of the cougar women.高級 ラブドール

  24. the research had limitations.高級 ラブドールImportantly,

  25. Chail was convicted of conspiring to attack the Royals.リアル ドールAlthough his plot never came to fruition,

  26. and/or genital contact.オナホ ラブドールThe young men explicitly referenced girls and women who inspired them,

  27. 人形 エロ表示:

    for this to happen,ダッチワイフparents have to have modeled and implemented cooperative ways of dealing with disagreements and conflict.

  28. 人形 エロ表示:

    ラブドール エロunderstanding its subtle,perplexing,

  29. it is imperative that we learn more about what emotional and cognitive effects dogs can have,ラブドール おすすめand why.

發佈留言

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