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

59,616 Responses

  1. Your baby does not have a fully developed liver and cannot process alcohol.Drinking alcohol during pregnancy increases the risk of miscarriage,エロ コスプレ

  2. there’s no need to worry if they turn onto their tummy or side while sleeping.コスプレ エロ いDo not let your baby’s head become coveredBabies whose heads are covered with bedding are at an increased risk of SIDS.

  3. The Royal Horticultural Society has information on potentially harmful garden plants.Burns and scaldsA baby’s skin burns more easily than an adult’s.エロ い コスプレ

  4. which causes toxoplasmosis.えろ コスプレAvoid lambing or milking ewes,

  5. From November 2022,patients over the age of 16 in England can access more detailed information from their medical records through online apps.コスプレ エッチ

  6. コスプレ エロIt will also help you to cope with labour.Keep up your normal daily physical activity or exercise (sport,

  7. then relax the musclesbreathe normally while doing pelvic floor exercisesat first,えろ コスプレtry to do this exercise quicklythen do it slowly,

  8. コスプレ エロStarchy foods are an important source of energy,some vitamins and fibre,

  9. FrankMar表示:

    Buy compounded semaglutide online: rybelsus – Buy semaglutide pills

  10. Social workers can help you protect your child.コスプレ エッチIf you wish,

  11. エロ コスプレankles,hands and facesevere headache that does not go away with painkillersvomitingproblems with vision,

  12. but you can have it from 16 weeks.To give your baby the best protection against whooping cough,エロ い 下着

  13. エッチ な 下着You may be offered an examination to see if your stitches have healed if you had an episiotomy or caesarean section.If you were due for cervical screening while pregnant,

  14. patches may be a better solution.NRT patches should be used for no more than 16 hours in any 24-hour period.エロ い 下着

  15. If you are in immediate danger or it is an emergency,エロ 下着Information for pregnant women with suspected or confirmed COVID-19If you test positive for COVID-19 outside of a hospital setting,

  16. Always consult your healthcare professional if you have concerns about your baby.Injuries to babies are preventableInjuries are the most common cause of death in childhood beyond the first year of life and are a major reason for children needing medical attention.エロ い コスプレ

  17. But dried fruit can be high in sugar and can be bad for your teeth.Try to swap dried fruit for fresh fruit,コスプレ エロ

  18. your mental healthIt’s important to look after your mental health as well as your physical health.About 1 in 10 women become depressed in the year after having a baby.エッチ な 下着

  19. Be prepared to change routines as your baby grows and enters different stages.And remember,下着 エロ

  20. fit safety catches to low cupboard doors.エロ い コスプレChoose cleaning products that contain a bittering agent.

  21. Most people who get severe yellow fever die from it.えろ コスプレThe yellow fever vaccine is a live vaccine,

  22. エロ い 下着For example,they are more likely to have problems keeping warm and are more likely to get infectionsyou will reduce the risk of sudden infant death syndrome (SIDS),

  23. エッチ な 下着Sometimes you can feel as though life is completely out of control.This can make you feel very tense and frustrated.

  24. Women who find out they’re pregnant after already having drunk in early pregnancy should avoid further drinking for the rest of their pregnancy.they should not worry unnecessarily,エロ コスプレ

  25. then relax.Short squeezes – quickly squeeze the muscles and then let them go immediately.エッチ な 下着

  26. If you are in immediate danger or it is an emergency,エロ 下着Information for pregnant women with suspected or confirmed COVID-19If you test positive for COVID-19 outside of a hospital setting,

  27. えろ コスプレTry swimming.It’s good exercise and also relaxing,

發佈留言

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