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

101,527 Responses

  1. it helps promote openness and vulnerability between you and your partner,人形 エロwhich can lead to an overall sense of mental and physical well-being.

  2. and she gets the “pleasure” of having pleased him.人形 えろAccording to Conley and Klein,

  3. it’s apparent which the authors typically intend to エロ 人形discover something Completely wrong, immoral, or risky with regards to the homeowners.

  4. 柔軟にお客様のニーズ?時代の変化に適応して、ラブドール エロ常に進化し続けています。

  5. The researchers asked when participants had experienced four events: First solo sex after toddlerhood First orgasm First partnered genital touch: hand massage (hand jobs) and oral sex (fellatio,cunnilingus) First intercourseUsually,ラブドール 中古

  6. ラブドールが好きな方は一度は高級な人形 えろラブドールを購入してみたいと思ったことはないでしょうか?

  7. The challenge here is to fashion a social consciousness that’s neither a dismissal of—nor apologia for—our biology.ラブドール エロHow can such social change be achieved? Two general approaches are available.

  8. 海外 エロ表示:

    Nearly all of its obtainable heads are Asian or white,初音 ミク ラブドール devoid of the extra skin tone solutions supplied by other manufacturers.

  9.  sexual arousal dampens women’s disgust response,keeping men in the gamAnd men are not just dirtier—they are more ill-mannered and foul as well.ラブドール 中古

  10. 中国 えろ表示:

    ラブドール オナニーdating apps,and relationship studies?AT: The “strong Black woman” stereotype creates an unrealistic expectation of strength for Black girls and women in all areas of their lives.

  11. Shirley表示:

    Where Will How To Repair A Upvc Door 1 Year From Now?
    repair hole in upvc door [Shirley]

  12. Wonderful goods from you, man. I’ve understand your stuff previous
    to and you are just extremely wonderful. I actually like what you have acquired here, certainly
    like what you are saying and the way in which you say it.
    You make it enjoyable and you still take care
    of to keep it smart. I can’t wait to read far more from you.

    This is really a terrific site.

  13. 7 Things You’ve Never Known About American Fridge Freezer shallow depth american Fridge freezer (http://mnogootvetov.ru/Index.Php?qa=User&qa_1=lumberpeony9)

  14. Abel表示:

    Test: How Much Do You Know About Suzuki Keys? suzuki
    flip key (Abel)

  15. You’ll Never Guess This Lawyer Injury Accident’s Tricks lawyer injury accident (squareblogs.net)

  16. This Is The History Of Three Wheel Pushchair In 10 Milestones best lightweight 3 Wheel stroller

  17. Seo Agency Uk表示:

    What’s The Job Market For Best SEO Agency Uk Professionals Like?
    Seo Agency Uk

  18. See What Case Opening Battles Tricks The Celebs Are Using Case opening Battle

  19. 5 Common Phrases About Retro Fridge Freezer White You Should Avoid side by side fridge freezer under counter Uk

  20. Charlesbot表示:

    https://fastpillsformen.com/# Sildenafil Citrate Tablets 100mg

  21. What’s The Job Market For French Door Fridge With Ice Maker Professionals Like?

    french door fridge

  22. 9 . What Your Parents Teach You About Lost Key
    Replacement Car Key Replacement Car

  23. The 10 Most Scariest Things About Injury Attorneys Injury Attorney

  24. Charlesbot表示:

    http://maxpillsformen.com/# Generic Cialis without a doctor prescription

  25. Begin By Meeting With The Steve Jobs Of The ADHD Signs In Adults Industry
    hidden symptoms of adhd in adults (maps.google.com.ua)

  26. Auto Collision Lawyer Tips From The Top In The Industry good lawyers for car accidents near me

  27. 10 Websites To Help You Become An Expert In American Style Fridge american style fridge freezer integrated

  28. 10 Factors To Know About Battery-Powered Scooters You Didn’t Learn At School portable Electric Mobility scooter

發佈留言

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