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

39,285 Responses

  1. Vicente表示:

    Is Your Company Responsible For An Upvc Sash Windows Budget?

    12 Tips On How To Spend Your Money replacement upvc
    window handles (Vicente)

  2. without a doubt,an interesting one.エロ ラブドール

  3. It’s Time To Extend Your Best American Fridge Freezers Options zackfoxworth.top

  4. negative moments in life and move on,女性 用 ラブドールwhile others of us have brains that don’t let go so quickly and are more wired to hold onto the negative.

  5. Alcohol’s deadly dangers and drawbacks get completely drowned out by that marketing tidal wave.ダッチワイフAn hour of watching Super Bowl ads tells you all you need to know about the potent and relentless promotion of alcohol.

  6. Six percent of the participants had been unable to receive health because they were concerned for their pets,and 4 percent indicated that the costs of pet ownership had affected their ability to pay for their medicines.セックス ドール

  7. ラブドール アニメIt uses virtual reality technology to create a three-dimensional environment where the user can interact with the content in a seemingly real way.Using special,

  8. of times I had heard the word in my four-and-a-half decades of existence.My friend who witnessed the incident checked in on me the next day to see how I was feelin As a true ally to LGBTQ+ people,ラブドール 動画

  9. 高級 ラブドールObsessive-Compulsive Disorder Autistic Spectrum DisorderASD,Attention Deficit Hyperactivity DisorderADHD,

  10. Traumas in childhood can affect the brain into adulthood.Yet your brain neurons have plasticity,女性 用 ラブドール

  11. ダッチワイフ エロDarwin himself thought this! In 1872,he wrote that insects “express anger,

  12. but the winner will take more than $4 million.Two years ago,lovedoll

  13. リアル エロ”The project’s ethical dilemmas were also ahead of their time.Intimate records such as dream narratives and life stories,

  14. But risk is unavoidable for living well and one of the greatest damages trauma inflicts is deep fear of it.ラブドール 通販Since risk is so intertwined with life itself,

  15. Feeling embarrassed herself,ラブドール エロshe shames him and ruins his evening.

  16. Commitment to life,ラブドール 通販determination,

  17. ダッチワイフ エロould we do with what we know? What sorts of choices should we make? We must use what we know on behalf of all animals.I will admit that I get cranky and irritable now and again.

  18. Second,current corporate employees may give up on their boss at the first sign of egocentricity,ラブドール セックス

  19. at this end of the market,the dolls are notorious for being practically ‘thrown together’ using cheap materials and awful,ラブドール メーカー

  20. there are lots of open questions here,but we shouldn’t make too much of our uncertainty.ダッチワイフ エロ

  21. Five Killer Quora Answers To Replacement Bentley Key replacement bentley key

  22. セックス ドールthey represent the space around them geographically.So instead of saying that something is on their left,

  23. 다바오 apk表示:

    Poker Tracker Is A Good Poker Strategy Tool 다바오 apk

  24. there is a growing number of women who also use and enjoy sex dolls.オナホドールJust as men can derive pleasure and satisfaction from the use of sex toys,

  25. it almost feels like there’s someone else here.エロ ラブドールShe’s become a presence in my home in some weird way,

  26. リアル ドールwe are more likely to resort to chemical tools — cannabis.If you plan to stop using cannabis,

  27. But I also hope that we learn something about the people who do this work.How close are we? When reading about an anticipated technological advance,リアル エロ

  28. セックス ドールmakes it easier to pinpoint which of our experiences make it into our long-term memory.It seems that the main factor that influences which experiences make it and which ones will be forgotten forever is whether the experience is followed by an idle period when the subject does very little.

發佈留言

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