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

  1. 11 Creative Ways To Write About Erb’s Palsy Law Erb’s Palsy Attorneys

  2. Five Killer Quora Answers To Upvc Window Repairs upvc Window repair

  3. What a material of un-ambiguity and preserveness of precious familiarity about unpredicted emotions.

  4. Why No One Cares About Replacement Lock For Upvc
    Door replacing lock In Upvc Door

  5. puravive表示:

    I’ve been browsing online greater than 3 hours nowadays, yet I never found any interesting article like
    yours. It’s beautiful price sufficient for me.
    In my view, if all website owners and bloggers made excellent content as you probably did, the
    internet shall be much more helpful than ever before.

  6. The Most Convincing Evidence That You Need Birth Injury Attorney birth injuries

  7. Hi there! Someone in my Myspace group shared this site with us
    so I came to give it a look. I’m definitely loving the information. I’m book-marking and will be tweeting this to my
    followers! Exceptional blog and great design.

  8. Firm表示:

    Why We Enjoy Personal Injury Attorneys (And You Should, Too!) Firm

  9. baskili poset表示:

    Simply wish to say your article is as surprising.
    The clarity on your publish is simply nice and that i could think you are a
    professional in this subject. Fine together with your permission let
    me to take hold of your feed to keep updated with imminent post.
    Thanks one million and please carry on the rewarding work.

  10. Fast Lean Pro表示:

    Hello, I enjoy reading all of your article. I wanted to write a little comment to support you.

  11. The Ultimate Guide To 18 Wheeler Wreck Lawyer 18 wheeler accident attorney

  12. Good article! We will be linking to this great content on our website.
    Keep up the good writing.

  13. Stephaine表示:

    Hi there, You’ve done a fantastic job. I will certainly digg it and personally
    recommend to my friends. I’m confident they will be benefited from this web
    site.

  14. Good day! This is kind of off topic but I need some guidance
    from an established blog. Is it tough to set up your own blog?

    I’m not very techincal but I can figure things out pretty fast.
    I’m thinking about making my own but I’m not
    sure where to start. Do you have any tips or suggestions?
    With thanks

  15. I mean I was already 18, and I hadn’t lost my virginity yet. It wasn’t a bad thing,オナドール and I knew I wanted to wait for that special moment, but unfortunately that wasn’t what happened.

  16. エロ 人形表示:

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

  17. 人形 エロ表示:

    We had planned to have sex that day, ロボット セックスbut it just ended up not working in our favour. The entire time we were out, we kept making out and rubbing crotches and grabbing ass.

  18. えろ 人形表示:

    but they do encourage everyone to get to know their own bodies so that they えろ 人形can better navigate their menstrual cycles.

  19. えろ 人形表示:

    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.

  20. えろ 人形表示:

    He’d stick his fingers in my mouth and I’d bite them オナドール(mostly because we have wild sex at his parents’ home and I’m being too loud

  21. エロ 人形表示:

    Engle adds that if that’s the case, but period sex jydollis important to you, it’s important to express that sentiment to your partner.

  22. says Kris Fretz, co-founder of sex toy brand Emojibator.ラブドール av “Personally, I’m more sensitive to the vibrator’s texture vs. intensity,

  23. 人形 エロ表示:

    There’s a common misconception that spicy activities like kink toys, ラブドール avsex toys, and sex games are only for couples whose routines have gotten stale, but this couldn’t be further from the truth.

  24. I didn’t cry, but the only regret was that we weren’t emotionally tied.オナドール It was good in a way though because we were each other’s first.

  25. He pulled out anyway, but the story made me cum almost immediately.オナドール I still masturbate thinking about it.”5.”He fingered me during my period”

  26. 人形 エロ表示:

    It’s a long blink or a visible shift backward in their seat. Several ask, after a pause,ラブドール オナニー “Are you a naked person?”

  27. One day, while walking to my student residence, セックス ロボットI came across a guy I knew. We weren’t friends but we had a few friends in common and I’d spoken to him several times.

  28. えろ 人形表示:

    Still worried? Dr. Olson says, “You can also try coming out of the shower. ラブドール sexThat may give you some confidence to engage sexually.

  29. えろ 人形表示:

    He pressed to his cock against my ass,セックス ロボット and slid his cock down closer to my ass hole pressing it in between my ass cheeks.

  30. There’s a common misconception that spicy activities like kink toys, ラブドール avsex toys, and sex games are only for couples whose routines have gotten stale, but this couldn’t be further from the truth.

發佈留言

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