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

  1. JamesZiz表示:

    Latest Diablo news https://diablo-ar.com, detailed game descriptions and guides. Diablo.az – The largest Diablo information portal in Arabic.

  2. See What Double Glazing Repairs Near Me Tricks The Celebs Are Using double glazing repairs near me

  3. Eqogvz表示:

    purchase durex gel sale – order durex gel online cheap purchase zovirax for sale

  4. 3D Files表示:

    Greetings to 3Doo.com, the premier database for 3D printing models.

    At 3Doo.com, you’ll find a vast selection of 3D printer models plus
    customizable models for all your prototype development projects.

    Regardless of whether you’re an amateur or a pro, our expertly curated collection guarantees
    the perfect model for your next project.

    Start exploring now and discover why threedoo.com is your number one
    platform for 3D printer makers.

    We feature an extensive array of genres, including:

    Artistic creations
    Useful prototypes
    Home decor
    Learning aids
    and much more.
    Each design is created with meticulous attention to detail,
    ensuring superior print quality to fulfill all
    your creative projects.

    Join our vibrant community of designers and start printing your next masterpiece effortlessly.

    Explore our easy-to-use interface designed to simplify your creative journey seamless and efficient.

    Discover features like:

    Customizable settings for customized projects
    Live updates on new uploads
    Comprehensive tutorials for beginners
    High-level printing techniques for professionals
    Our committed team consistently updates our site with fresh and
    also exciting creations, making sure you always get
    access to the most advanced resources for your artistic
    pursuits.

    Keep inspired and attain your creative vision through 3D-Print
    Hub.

    Start your design exploration now and begin unleash limitless possibilities using threedoo.com.

    Thank you for opting for threedoo.com!

  5. Here’s A Little-Known Fact Concerning Assessing Adhd In Adults Adult Adhd Assessment

  6. Thanks for every other fantastic article. Where else may anyone get
    that kind of info in such an ideal manner of writing?
    I’ve a presentation subsequent week, and I’m on the search for such information.

    Also visit my web-site … เกร็ดความรู้

  7. Dangerous Drugs Law Firms Tools To Help You Manage Your Everyday Lifethe Only Dangerous Drugs Law Firms
    Trick That Every Person Should Know dangerous drugs lawsuit

  8. Fast Lean Pro表示:

    Excellent article. Keep posting such kind of information on your blog.
    Im really impressed by your blog.
    Hi there, You have performed an excellent job. I will definitely digg it and personally suggest to my friends.
    I am sure they will be benefited from this web site.

  9. You’ll Never Guess This Upvc Door Repairs Near Me’s Benefits upvc door repairs Near me

  10. 免费A片表示:

    免费A片

    Thanks in favor of sharing such a pleasant thinking, paragraph is fastidious, thats why
    i have read it fully

  11. Vernon表示:

    Malpractice Attorneys It’s Not As Hard As You Think malpractice law firms – Vernon,

  12. Google Bombing And Choice Between Inbound Links And Keyword Optimization 검색엔진최적화 회사

  13. bokep hd表示:

    You are so cool! I do not believe I have read through anything like that before.
    So nice to find somebody with a few original thoughts on this subject matter.

    Really.. thank you for starting this up. This site is one thing that is needed on the internet,
    someone with a little originality!

  14. Guide To Double Glazing Repairs Near Me: The Intermediate Guide On Double Glazing Repairs Near Me double glazing repairs Near me

  15. 10 Things That Your Family Taught You About Double Glazed Window Suppliers
    Near Me double glazed window suppliers Near Me

  16. lewd acts表示:

    Heya! I just wanted to ask if you ever have any issues with hackers?
    My last blog (wordpress) was hacked and I ended up losing a few months of hard work
    due to no backup. Do you have any methods to protect
    against hackers?

  17. none表示:

    Today, I went to the beach with my kids. I found a sea shell and gave it to my 4 year old daughter and said “You can hear the ocean if you put this to your ear.” She placed the shell to her ear and screamed.
    There was a hermit crab inside and it pinched her ear. She never wants
    to go back! LoL I know this is completely off topic but I had to tell someone!

  18. I loved as much as you’ll receive carried out right here.
    The sketch is attractive, your authored material stylish.
    nonetheless, you command get got an shakiness over
    that you wish be delivering the following. unwell unquestionably come further formerly again since exactly the same nearly a lot often inside case you shield this increase.

  19. Pretty section of content. I just stumbled upon your website and in accession capital to assert that I acquire in fact
    enjoyed account your blog posts. Any way I’ll be subscribing to your feeds and even I achievement you access consistently rapidly.

  20. 20 Things You Need To Know About Malpractice Attorneys
    malpractice Law firms

  21. Xpert Foundation Repair
    Río Grande Valley, TX 78582, United Ѕtates
    9562653062
    lvl beam installation guide

  22. Hi there! Would you mind if I share your blog with my zynga group?

    There’s a lot of folks that I think would really enjoy your content.
    Please let me know. Cheers

  23. Internet Targeted Traffic – Free Ways Maximize Traffic For
    Your Own Web Site 구글상위노출 업체

  24. This Week’s Most Remarkable Stories Concerning Upvc Window Repair
    Near Me Upvc window repairs near me [Ordermall.co.kr]

  25. Guvenilir bir kumarhane tavsiye ederim Sweet bonanza

  26. Can You Discover A Credit Card Without Any Credit Facts?

    소상공인 대출

  27. Bruce表示:

    Guide To Double Glazing Near Me: The Intermediate Guide The Steps To Double
    Glazing Near Me double glazing near me (Bruce)

發佈留言

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