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

93,591 Responses

  1. The joints are expertly crafted to move smoothly,リアル ドールcom’s customer service is top-tier,

  2. for their superior quality and exceptional customer service.Purchasing a doll from Dream Dolls was one of the best decisions I’ve made.ラブドール 中古

  3. offering a robust foundation for readers to build upon.ダッチワイフThe clarity and coherence of your writing made even the most intricate details easy to understand and apply.

  4. If you’re looking to invest in a doll that is not only beautiful and realistic but also a true reflection of your personal style,中国 エロDon’t miss the chance to experience the exceptional quality and service that this site has to offer.

  5. ラブドール えろIt’s no wonder com is so highly recommended.The combination of exceptional quality,

  6. making the complex details of [specific issue or area] not only accessible but also captivating.ラブドールYour ability to present intricate information in a clear and engaging way is a real strength,

  7. ラブドールThe clear,actionable steps you provided made it easy for me to apply your advice to my own context,

  8. The clarity with which you articulated your ideas,combined with the logical structure of your argument,ラブドール

  9. It’s clear why so many people love The combination of high-quality products,ラブドール 中古excellent customer and a seamless shopping experience makes JP-Dolls a top choice.

  10. com has been a perfect experience.美人 セックスThe website offers a vast array of customization options,

  11. ラブドールYour detailed exploration of [specific issue or area] was especially helpful,as it provided a well-rounded perspective that enhanced my understanding of the topic.

  12. Their unwavering commitment to excellence,ラブドール えろcombined with their superior quality,

  13. The level of detail and realism is remarkable,ラブドール エロwith lifelike skin texture and finely crafted facial features that make the doll look incredibly real.

  14. 海外 エロ表示:

    “Your article on [specific topic] was a remarkable testament to the depth of your research and the applicability of your insights.ダッチワイフThe extensive research you conducted on [specific subtopic] was evident throughout the piece,

  15. I recently purchased a doll from Silicon Wives,ラブドール 中古and I am extremely impressed with the realism and comfort it offers.

  16. The posts are structured with clear headings and subheadings,making it easier for search engines to index the content.ラブドール エロ

  17. From All Over The Web 20 Amazing Infographics About Asbestos Cancer Lawyer Mesothelioma Settlement mesothelioma
    lawsuit, https://digitaltibetan.win/wiki/post:asbestos_attorney_mesothelioma_a_simple_definition,

  18. ダッチワイフThank you for sharing such engaging and illustrative content.and I look forward to more of your engaging work.

  19. The website offers a wide range of customization options,ラブドール 中古allowing me to create a doll that truly reflects my preferences.

  20. ラブドール えろIf you are seeking a doll that is not only beautiful and realistic but also a true reflection of your personal style,I wholeheartedly recommend exploring com.

  21. The website is equipped with robust security measures to protect your personal and payment information,中国 エロwith multiple payment options available to suit your preferences.

  22. 中国 えろ表示:

    Thank you for sharing such clear and practical insights.as I am confident they will continue to offer clarity and practical advice that are both enlightening and actionable.ダッチワイフ

  23. Telegra.ph表示:

    Getting Tired Of Mesothelioma Asbestos Claim? 10 Inspirational Resources To Revive Your Passion mesothelioma lawyer
    (Telegra.ph)

  24. 10 Things We All Love About Amazon Robot Vacuum Robotic Vacuum Cleaner
    Commercial (House-Fisher-4.Technetbloggers.De)

  25. and I am eager to see more of your engaging and insightful writing in the future.”Your article on [specific topic] was a remarkable example of detailed analysis combined with practical advice.ラブドール

  26. You’ll Be Unable To Guess American French Style Fridge Freezer’s
    Tricks american french style Fridge freezer

  27. Your detailed examination of [specific subtopic or issue] was particularly impressive,offering a well-rounded perspective that enhanced my understanding of the topic.ラブドール

  28. ラブドール えろThe website offers a vast selection of customization options,allowing me to create a doll that perfectly suits my preferences.

  29. The 10 Most Scariest Things About Asbestosis Asbestos Mesothelioma Attorney Mesothelioma attorney

發佈留言

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