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

46,933 Responses

  1. In that vein, it’s important to take note of what toys are made ofラブドール av when buying them. Silicone toys, for instance, should never be used with silicone lube,

  2. another lover of mine (D and I were in an open relationship) brought us into a whole community of BDSM aficionados. 女性 用 ラブドールWith D, it was all about intensifying sex and bonding together;

  3. ‘Gay men are no more or less masculine than straight men or bi men,’ says Vaneet.女性 用 ラブドール ‘But the idea exists that by having sex with men,

  4. If a literal card isn’t your style, Marla Renee Stewart,ラブドール av MA, a sexologist and sexpert for Lovers, suggests saying something like,

  5. エロ 人形表示:

    The first couple of tries were discouraging because his penis would not go in. He tried,オナドール I tried, and after a seemingly endless amount of times, I felt the ridiculousness of it.

  6. エロ 人形表示:

    Chinwe knew that she wanted to lose her virginity and how. セックス ロボットWhen she had sex for the first-time she was sure to tell her sexual partner when something wasn’t okay for her.

  7. So you’re a virgin,” he says with an eyebrow up. First time to Jamaica? Yes. ラブドール オナニー“A double virgin!” Oh god. So this is where I am.

  8. Feeling unsure? Read our guide to knowing if you’re ready to have sex ロボット セックス it can help make it clear in your own mind.

  9. By highlighting the way that men are expected to behave sexually, we can also examine ラブドール 女性 用what false narratives have been fed to women, and how they, in turn, can be broken down.

  10. 人形 エロ表示:

    One level of pretense falls away and small talk becomes less smallえろ 人形. It’s that sensation of finding your people and thus not having to pretend.

  11. 人形 エロ表示:

    Dressed and wearing what I think is the right amount of makeup for ラブドール オナニーJamaican humidity, I head to our meeting spot at the bar, where a woman in a pageboy wig and a dress cut to her belly button comes up to me immediately and says my name

  12. Beau表示:

    Wow that was unusual. I just wrote an very long comment
    but after I clicked submit my comment didn’t show
    up. Grrrr… well I’m not writing all that over again. Anyways,
    just wanted to say superb blog!

  13. Nine Things That Your Parent Teach You About Truck Accidents Lawyer Truck Accidents

  14. Jens表示:

    Hi there, I found your site by means of Google even as looking for a comparable matter, your
    website got here up, it seems good. I have bookmarked it in my google bookmarks.

    Hello there, just was aware of your weblog via Google, and located that it is really informative.
    I’m going to watch out for brussels. I’ll be grateful in the event you proceed this in future.
    Lots of people will likely be benefited out of your writing.
    Cheers!

  15. Window Repair Near Me Tools To Help You Manage Your Daily Life Window Repair Near Me
    Trick That Every Person Should Learn window repair Near me

  16. Gigatree.Eu表示:

    What’s The Job Market For Upvc Repairs Near Me Professionals?

    Upvc Repairs Near Me (Gigatree.Eu)

  17. You’ll Never Guess This Cerebral Palsy Lawyers’s Benefits
    Cerebral Palsy Lawyers

  18. エロ 人形表示:

    Engle tells us that period sex prep is “so much simpler than people think.ラブドール sex All you need to do is “get yourself a period-specific towel and lay it down before you get down.

  19. What You Must Forget About Making Improvements
    To Your Upvc Door Panel types of Upvc door hinges

  20. 로또디비表示:

    Heya i’m for the primary time here. I came across this board and I find It truly helpful & it helped me out a
    lot. I am hoping to present one thing again and aid others such as
    you helped me.

  21. 人形 エロ表示:

    I feel extremely comfortable with these middle-aged people. They ask what I want out of the trip, andラブドール オナニー I tell them about my quest to find out if I’m a naked person,

  22. Jere Alas表示:

    20 Insightful Quotes On Double Glazing Window Locks Repairs Jere Alas

  23. If you’re still feeling nervous,オナドール talking about how you feel with your partner can help, you might also want to talk to a friend or family member you trust.

  24. What, afraid I’m going to knock you upオナドール I was caught off guard, and responded with, ‘Sir please don’t!’ and the vibe did a sexy-as-hell 180. We seamlessly shifted into a

  25. Brianbob表示:

    Bernardo Mota Veiga de Carvalho e Silva https://manchestercity.bernardo-silva-cz.com Portuguese footballer, club midfielder Manchester City and the Portuguese national team.

  26. The Most Underrated Companies To Follow In The Railroad Injuries Legal Industry railroad Injuries law firm

  27. Crystal表示:

    Hi to every body, it’s my first visit of this weblog; this website consists of remarkable and really good stuff in favor of readers.

  28. エロ 人形表示:

    Now I have met a wonderful guy, we first set eyes on each other 2 years ago.オナドール He is sweet, caring and really loves me. However, the issue of sex has come up again.

  29. How Semi Truck Lawyer Rose To Become The #1 Trend In Social Media semi truck Accident Attorney

  30. So, once you’re comfortable with how to use your vibrator the traditional way,ラブドール sex see how it feels on other erogenous zones, like the nipples, base of your neck, or back of your knee.

發佈留言

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