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

62,422 Responses

  1. but it was *way* too late to turn back.ラブドール えろWe put the bracelets on each other’s wrists,

  2. ダッチワイフalways.We dated for a few months and had intense—if,

  3. 人形 エロ表示:

    was built in 1958,人形 エロthe same year as Mount Airy,

  4. 人形 エロ表示:

    ダッチワイフ1.What looks like a bottle of lube is not always a bottle of lube.

  5. 人形 エロ“Well,I think I know what this will be like,

  6. or be rejected,I didn’t tell my husband to go down on me,ダッチワイフ

  7. エロ 人形表示:

    pleasure-based sex education and consent in schools and at home.Just because the liberal media is all about anal now shouldn’t suggest that anal is an automatic given when you hook up or that young people have learned how to engage in butt stuff safely.人形 セックス

  8. オナドールwas often seen as its own category: maybe you stole something as a child once,or you “borrowed” something knowing that you would never return it (I’ll admit here to both,

  9. リアル ドールbeing around someone else can make a stressful situation feel easier to manage.So if you have to be around someone you dislike,

  10. AlfredHag表示:

    lasix 100mg lasix dosage generic lasix

  11. 人形 エロ表示:

    some family members become more independent or “self-differentiated,高級 オナホ” while others become less so,

  12. I also feel gross at having been kissed before I had the option to say no.By the look of it,人形 エロ

  13. 人形 エロ表示:

    Even if anal is less shrouded in shame than it once was,it’s still not something young people are learning about or talking about openly.人形 セックス

  14. 人形 エロ表示:

    The objective is to construct The perfect artificial companion,reflecting the identity and person style of your owner.人形 セックス

  15. 人形 えろ表示:

    人形 エロhe was the one you loved above all?Today I realize that this ad bears a striking resemblance to a Cialis commercial.But as a pre-sexual child,

  16. 人形 エロ表示:

    オナホ 高級Rollo May was quite insistent that the daimonic is not only about destructiveness,pathology and evil,

  17. Sabina表示:

    A Hyundai Key Fob Success Story You’ll Never Remember
    replacement key for hyundai i20 (Sabina)

  18. JosephWrela表示:

    ventolin nebulizer: buy Ventolin – buy ventolin over the counter nz

  19. エロ 人形表示:

    ラブドール オナホSome encouraged women to support other women.They hold the money and power to control the people (i.

  20. 人形 エロ表示:

    I was silently seething.ラブドール 男It felt like he had gotten away with something,

  21. 人形 エロ表示:

    ラブドール えろbut it changes every time,and the address is sent to you the night before the party.

  22. 高級 オナホOften during the holidays,our overindulgence with alcohol means we don’t bother to work out.

  23. 人形 エロ表示:

    But Bruce doesn’t stop there.ラブドール 中古When chatting casually with other team members,

  24. 人形 エロ表示:

    オナホ ラブドールIn terms of similarities,the women interviewed discussed having a “lonely secret existence.

  25. Carlton表示:

    What’s The Current Job Market For Togel Singapore Professionals?
    togel singapore (Carlton)

  26. AlqaPaish表示:

    The CDC and most doctors recommend waiting until you are healthy to get the vaccine.
    When you buy sildenafil time to work can be as simple as checking review sites.
    We need less drugs in this society, not more drugs, and I would oppose the legalization of marijuana in the country or legalization of marijuana for medicinal purposes because pain management is available from other sources.

  27. AmmyPaish表示:

    Since you’re one of our highest rated Medical Detectives,we’d like to invite you to be the official moderator for this case.
    Choose the perfect sildenafil review less respected than men who don’t use it?
    The ducts are responsible for carrying pancreatic juice to the intestines.

  28. 人形 エロ表示:

    Most would label oral-genital stimulation as “perhaps” sex.Perhaps researchers only consider mutual genital touching as sex if the boy is pubescent or older.高級 ラブドール

發佈留言

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