透過網頁讀取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,684 Responses

  1. 人形 エロ表示:

    But I decide to go to Hedonism II in Negril, ラブドール オナニーJamaica—a clothing-optional resort that bills itself as

  2. Ccwzyh表示:

    buy ascorbic acid 500 mg sale – purchase ciprodex ophthalmic solution generic order compro pills

  3. エロ 人形表示:

    He was a virgin too, so I thought that because we messed around a few times, オナドールthat it would be okay to have both our virginities taken by each other.

  4. Veterans Disability Lawyers Tools To Ease Your Daily Life Veterans Disability Lawyers
    Trick Every Individual Should Be Able To Veterans disability lawyer

  5. エロ 人形表示:

    I discovered pony play seven years ago, at one of my very first BDSM events—女性 用 ラブドールa private play party at a community member’s home.

  6. えろ 人形表示:

    This is a common concern and it’s totally normal to worry about it. ロボット セックスLuckily you can do something about it. Using a condom is the best way to protect yourself and your partner from STIs and prevent unplanned pregnancy.

  7. 人形 エロ表示:

    He’s already occupied a manly role in my life, at just 18. オナドールIt still doesn’t change my mind. I know I might disappoint him, but my virginity feels like that one thing that adds tremendous value to my life.”

  8. エロ 人形表示:

    “Bi men are better in every single way,”女性 用 ラブドール all that’s doing is painting people with a broad brush again.

  9. えろ 人形表示:

    At the end of the meal, I feel those nerves that I get at the end of any first date. ラブドール オナニーHow do I end this and is the person going to kiss me and do I want them to?

  10. エロ 人形表示:

    the vibrator reigns supreme as the most consistently popularラブドール sex and well-known style of all time.

  11. えろ 人形表示:

    alongside their stylish clothing range, in department stores where the shop assistants tell me that many of their customers will go out, ラブドール 女性 用brandishing their bag, as if it’s a status symbol. And recently, the brand launched here in Selfridges.

  12. 9 Lessons Your Parents Taught You About Upvc Window Repair Near Me Window repair near me

  13. at first he understood but now claims he’s been waiting オナドールfor me and only me, we would get married later etc.

  14. エロ 人形表示:

    We talked for a while, and I told him thatセックス ロボット I’d been looking to lose my virginity but hadn’t found anyone. His response was, ‘Well, there’s always me.

  15. 24 Hours For Improving Upvc Window Repair Near Me upvc window Repairs near me

  16. bokep indo表示:

    What’s Going down i’m new to this, I stumbled upon this I’ve found It positively useful and it
    has aided me out loads. I am hoping to give
    a contribution & assist other users like its helped me. Good job.

  17. Thanks in support of sharing such a nice idea, post is good, thats why i have read
    it entirely

  18. 人形 エロ表示:

    In the beginning of the school year, this one guy and I hung out A LOT. オナドールWe pretty much were on and off, and at some point, we were alone in his dorm hanging out because his roommate went elsewhere that night.

  19. えろ 人形表示:

    Since the first UK issue hit the news stands in 1972, Cosmopolitan has been writing about sex.ラブドール 女性 用 We pride ourselves on pushing for female pleasure,

  20. 人形 エロ表示:

    Frye-Nekrasova also recommends “jydollhaving a candid conversation to dispel any myths or misunderstandings about periods.” She notes,

  21. えろ 人形表示:

    Me and my bf were having sweet, kissy, missionary sex and I could feel him getting close. オナドールI kindly reminded him to pull out (I was on the Pill but didn’t want to risk it) and his response was,

  22. えろ 人形表示:

    In my experience, many people with vaginas are also nervous about having period sex, ラブドール sexespecially if they’ve never done it before,

  23. エロ 人形表示:

    we still have a long way to go. And in my experience, えろ 人形one of the last frontiers in sexual wellness is period sex.

  24. えろ 人形表示:

    Breaking those statistics down further, a 2018 Stonewall report found that three in 10 bi men, 女性 用 ラブドールin comparison with less than one in 10 bi women, say they can’t be open about their sexuality with their friends.

  25. 人形 エロ表示:

    Every vibrator, no matter the size, operates with a motor inside. ラブドール sexThis explains one reason why there are several different types of vibrators.

  26. 人形 エロ表示:

    He put the condom on, and I was on top. オナドールForeplay was really important because it made it easier for him to slide in me without blood being involved.

  27. 人形 エロ表示:

    However, the best way to contain the mess ラブドール sexand make clean-up as easy as possible is to prepare in advance.

  28. えろ 人形表示:

    Sex on the kitchen floor, on the sofa, in the hallway.When Beth Ashley moved in with her partner, aged 21, ラブドール 女性 用she assumed they’d have sex ‘everywhere, and all the time’.

  29. Star Trek Slot Machine, Taking Online Game To Another Level 에볼루션 티비

  30. I encourage asking them what about it grosses them out,jydoll Frye-Nekrasova advises. “A lot of the apprehension people have is because they don’t fully understand the period, so offering some additional knowledge can help.

發佈留言

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