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

136,595 Responses

  1. えろ 人形表示:

     gender non-egalitarian countries.ラブドール オナニーContraception is legal,

  2. 人形 エロ表示:

    ドール アダルトor if he has trouble lasting a fair time during intercourse,Unfortunately,

  3. ラブドール 高級This is what she says.If you read this site for any length of time you will encounter the term “neofeminist.

  4. Tammy encourages others to be cautious when leaping to protect Black men in these relationships,女性 用 ラブドールand invites us all to elevate the voice of these men and their experiences: “The more we can talk about these things openly,

  5. えろ 人形表示:

    ラブドール 女性 用However,the research shows that compulsive use of the Internet,

  6. knowing who he is,being excited to be with meTurn Offs: Ignorance,ラブドール 女性 用

  7. ” (The Department of Economics at Chicago is the best economics department in the world and has produced more Nobel laureates than any other single department.ラブドール 販売)Meanwhile,

  8. Exploring this further,ラブドール 販売psychologist Henry Cloud eloquently stated,

  9.  Despite not being interested in sex at the time,a number of participants described agreeing to have sex with their partner anyway.女性 用 ラブドール

  10. えろ 人形表示:

    ドール アダルトAn informal investigation of these men found that many do not experience this practice as racist.Cuckolding,

  11. The link between alcohol and drug dependence and frequent sex with multiple partners is stronger for women than men.エロ ラブドールThe mental health consequences of having multiple sex partners were long thought to include greater rates of anxiety and depression.

  12. had never encountered being called racial epithets without their consent.When I first wrote about cuckolding in 2009,リアル ラブドール

  13. えろ 人形表示:

    but within that unity,a profound truth is often overlooked: Emotional fusion,ラブドール 高級

  14. エロ ラブドールAt ages and 32,they were given individual assessments on their mental health status in the areas of anxiety,

  15. ラブドール 女性 用They were 5,000 undergraduate students at a major university.

  16.  the reason that some people make you feel uncomfortable may have far less to do with you than with them.ラブドール sexIf you can move on,

  17. you may feel that young people also look at you in a critical or judgmental way,ラブドール sexbut unless they say something,

  18. that’s a totally arbitrary “rule” and it’s one that few people actually seem to follow.女性 用 ラブドールIn fact,

  19. She and her colleagues reviewed data collected from about 20,000 Australian families over a period of 16 years with participants entering the study when the children were 1-year-old.ラブドール 高級

  20. not by forming long-term pair-bonded relationships (“marriages”) in which to raise and invest in children,ラブドールbut by having a large number of extrapair copulations with otherwise mated women and cuckolding their mates.

  21. In a new study just published in The Journal of Sex Research,<a href="https://www.erdoll.com/tag/siliconelovedoll.htmlラブドール 男authors conducted a rapid review of articles published between 1990 and 2020 that explored sexual pleasure during first sexual experiences.

  22. ラブドール 女性 用Guess you might say it’s still going on—sometimes when I am home visiting my folks we just get together for the sex.age 24“He was a football player and wasn’t someone I wanted to be in a relationship with.

  23. ” The intelligent man knows that “free tail is the most expensive kind,” and can make the reasonable and pragmatic decision to spend his money on a “sure thing” rather than chasing women whose price and quality are uncertain.ラブドール 販売

  24. えろ 人形表示:

    when injuries forced him off the ice.When not managing SexSmartFilms’ 50,ラブドール

  25. Men prefer beautiful women for both long-term and short-term mating,ラブドールwhereas women prefer handsome men only for short-term mating (casual affairs and one-night stands),

  26. ドール アダルトmay be a form of misguided saviorism.Tammy Fisher,

  27. ラブドール sexfor companionship,then the sex is somehow sullied,

  28. I was thoroughly impressed by the depth of analysis in your article on [specific topic].ラブドールYou’ve covered all the critical angles and provided a well-rounded view that is both comprehensive and easy to understand.

  29. They argued that a booty-call sits between the most superficial relationship—one-night stands,and the most serious one—long-term romantic relationships.ラブドール 高級

發佈回覆給「ラブドール 安い」的留言 取消回覆

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