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

38,709 Responses

  1. You can learn to rewire your brain by making small changes repeated over and over again.女性 用 ラブドールYou can practice new ways of thinking,

  2. He thought that his ex was the 渓ove of his life.?Now,ラブドール エロ

  3. 19.sex ドールYou’re my favourite person to text… 97 times a day.

  4. agen99bet表示:

    I’d like to find out more? I’d love to find out more details.

  5. Dogs do not just “fit in” to a space made by humans,although there are pressures on them to do so.ダッチワイフ エロ

  6. ラブドール セックスparents should expect to continually engage in diverse difficult dialogues with their children.Even ifor when systemic changes are made to minimize or prevent these traumatic or oppressive events,

  7. to say to ourselves in a situation when our partner’s mood or behavior doesn’t quite make sense to us,ラブドール 女性 用“This strong reaction/these intense emotions may not have anything to do with m There may be something else going on.

  8. えろ 人形表示:

    realistic touch,and customizable options,ラブドール アニメ

  9. either ETs would need extraordinary life spans or some form of ultralong hibernation to survive the journey.ラブドール 高級But even with long lifespans or suspended animation,

  10. セックス ドールMost of us think of time as flowing from left to right or from right to left,and this may have something to do with the direction in which we read.

  11. ラブドール セックスYounger children teach their peers new words or conceptsboth good and bad.Meanwhile,

  12. But as you might imagine,ダッチワイフ エロthat raises lots of questions about how these animals are going to be reared and slaughtered.

  13. How To Carry Out Marketing A Web Based Business Like A
    Pro 백링크 사이트

  14. 人形 エロand suicidal ideation but sharing a need to have someone listen to them—preferably someone from outside “the system.” written by Alessandro Camon,

  15. エロドールas well as perceptions of in-group membershipStürmer,Snyder,

  16. 人形 エロ表示:

    , death, hate, violence.ラブドール セックスIf they do decide to initiate these conversations,

  17. realistic to fantasy-inspired,オナホドールthere’s a sex doll out there for just about everyone these days,

  18. such as the times we spend brushing our teeth,play roles that might seem immaterial and play parts in the moving time of one’s life.ラブドール

  19. Just because these larynges produced purring sounds in and of themselves,ラブドール 中古this does not mean that your cat has no control over purring.

  20. ラブドール メーカーTPE is a more affordable rubber-like material.Thermoplastic Elastomer sex dolls bring out the best of the material and,

  21. 人形 エロ表示:

    91.Even though the oceans separate us,ラブドール 値段

  22. Ironically,オナニー 用these men have more in common than you would think at first glance about what we give up of ourselves sexually to be loved.

  23. えろ 人形表示:

    There’s a comfort in knowing that she’s always there,リアル セックスa constant in my otherwise changing world.

  24. it’s hard to tell how she experiences the encounter.ラブドール 通販She may not be experiencing it at all.

  25. ” Researchers further suggest that cat meows are similar to the cries of a human baby and that human preferences in cat vocalizations may have played a role in this similaritye.ラブドール 高級g.

  26. including insects,ラブドール 販売come to the fore.

  27. 人形 エロ表示:

    we might create space by becoming overly critical.ラブドール エロJust as expressing love brings two people closer,

  28. jere alas表示:

    This Is The Advanced Guide To Double Glazing Window Lock Repair jere alas

  29. for our choices,and for our own development.ラブドール と は

發佈留言

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