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

54,284 Responses

  1. JerryMig表示:

    mexico drug stores pharmacies reputable mexican pharmacies online п»їbest mexican online pharmacies

  2. JerryMig表示:

    buying prescription drugs in mexico online medicine in mexico pharmacies medication from mexico pharmacy

  3. JerryMig表示:

    mexican drugstore online mexican mail order pharmacies mexican drugstore online

  4. Louishycle表示:

    mexico pharmacies prescription drugs best mexican online pharmacies best online pharmacies in mexico

  5. JerryMig表示:

    п»їbest mexican online pharmacies п»їbest mexican online pharmacies best online pharmacies in mexico

  6. Louishycle表示:

    mexican online pharmacies prescription drugs medicine in mexico pharmacies buying from online mexican pharmacy

  7. TravisNOb表示:

    https://mexicanph.com/# mexican rx online
    medication from mexico pharmacy

  8. JerryMig表示:

    reputable mexican pharmacies online mexican pharmacy pharmacies in mexico that ship to usa

  9. WilliamSoorp表示:

    https://medicinefromindia.store/# legitimate online pharmacies india

  10. Eugenehaupe表示:

    canada ed drugs legal canadian pharmacy online best canadian online pharmacy

  11. WilliamSoorp表示:

    https://certifiedpharmacymexico.pro/# mexican drugstore online

  12. Eugenehaupe表示:

    best online pharmacy india Online medicine order india pharmacy mail order

  13. Eugenehaupe表示:

    non prescription erection pills ed pills without doctor prescription viagra without a doctor prescription

  14. Pxbnsu表示:

    buy cheap rybelsus order rybelsus 14mg pill buy rybelsus online cheap

  15. WilliamSoorp表示:

    https://canadianinternationalpharmacy.pro/# 77 canadian pharmacy

  16. WilliamSoorp表示:

    http://edwithoutdoctorprescription.pro/# meds online without doctor prescription

  17. Eugenehaupe表示:

    medication from mexico pharmacy mexican mail order pharmacies reputable mexican pharmacies online

  18. BennyNib表示:

    viagra without doctor prescription: cialis without a doctor prescription canada – legal to buy prescription drugs from canada

  19. Eugenehaupe表示:

    prescription meds without the prescriptions ed pills without doctor prescription prescription drugs

  20. Sssevi表示:

    acticlate cheap order doxycycline sale vibra-tabs cheap

  21. beaxy表示:

    I like the Sixers here. If you’re rolling with the Sixers here as well, take advantage of our special. Bet $1 on any team this round and win a $1 for every point that team scores. Go to BetMGM Yahoo. BetMGM is giving new users $200 if either the 76ers or Hawks hit a 3-pointer on Thursday. Click ➡️ here ⬅️ to get yours. The 76ers opened as 6.5-point chalk for Game 5 even after their meltdown on Monday, a missed opportunity that cost them a chance to put the Hawks on the ropes. Even if C Joel Embiid continues to be sidelined for Philadelphia, 76ERS (-105) is an enticing wager with the Hawks struggling to win games recently. Philadelphia has gotten fantastic contributions from G Shake Milton, G De’Anthony Melton, and F Tobias Harris amid Embiid’s absence. Jimmy Butler averages 19.3 points per game in 33.8 minutes per contest. He averages 4.1 assists per contest and 5.1 rebounds per game. His field goal percentage is 47% for the year while his free throw percentage is 86%. Ben Simmons comes into this contest averaging 17.1 points per game while playing 34.5 minutes per night. He pulls down 9.0 rebounds per game and dishes out 7.8 assists per game as well. His field goal percentage is 57% while his free throw percentage is at 60%.
    http://zoom-wiki.win/index.php?title=Trustworthy_betting_site_1win
    *18+. Play Safe. From 00:01 on 18.10.2022. 4x £10 free bets. Free bets expire after 30 days. New customers only. Minimum £10 stake on odds of 1 2 (1.5) or greater on sportsbook (excluding Virtual markets). Further terms apply. Enjoy our Half Rack RibFix for just $12.99 every Thursday after 5pm. CLICK HERE TO LEARN MORE. *18+. Play Safe. From 00:01 on 18.10.2022. 4x £10 free bets. Free bets expire after 30 days. New customers only. Minimum £10 stake on odds of 1 2 (1.5) or greater on sportsbook (excluding Virtual markets). Further terms apply. Nike Promo Code The Premier League fixtures for the 2022-23 season will be released at 9am BST on Thursday, June 16. DISCLAIMER: Offer valid for Bonus Club members. Offer valid in stores only. Offer valid on Birthday Treat Bear only. Offer subject to change without notice. Limit one per guest, must be present for offer. Cannot be combined with any other offer. While supplies last. Outfits and accessories sold separately. Minimum price £1. See an associate for details

  22. Eugenehaupe表示:

    mexican drugstore online medication from mexico pharmacy mexico drug stores pharmacies

  23. WilliamSoorp表示:

    https://edpill.cheap/# new treatments for ed

  24. Hi there, I discovered your web site by means of Google whilst looking for a comparable subject, your web site came up, it appears good. I have bookmarked it in my google bookmarks.

  25. WilliamSoorp表示:

    http://edpill.cheap/# ed pills for sale

  26. Eugenehaupe表示:

    maple leaf pharmacy in canada canada drug pharmacy canadian king pharmacy

  27. Ded表示:

    There are so many great RTG casinos to choose from. What makes these top RTG casinos different from one another is the bonus offers and other ongoing promotions for both new & existing players. In this section you will find the latest exclusive no deposit bonuses for 2023 at some of the top online casinos powered by RTG. All the bonuses on our list are safe and 100% free to claim. Not only can you use them to play the games available for free but you stand a chance of winning real cash prizes too. Red Dog Casino accepts all major credit cards and banking institutions, plus a few cryptocurrencies. Unlike many online casinos, you won’t have to pay transfer fees on most credit card or crypto deposits, aside from Ethereum and Tether. Most of Red Dog Casino’s payout percentages remain healthily above 95%.
    https://signinvault.com/de/superlenny.html
    Online Availability: The best real money slots will be available at multiple legal online casinos in your state. Whether you have just one or several casino accounts, you’ll be able to find a broad range of great slots. Slots are the most played games of all. As a result, most real money online casinos offer hundreds and even thousands of slot games. Slots account for the majority of games at these online gambling sites, usually three-quarters or more of the total games library. Cafe Casino offers hundreds of online slot games with high payout percentages, impressive bonus features, and large progressive jackpot slots. If you want to play free online slots to get started, Cafe Casino may be for you. The online slots casino offers an excellent mobile app for gambling on the go, free spins, and the opportunity to win real money with every game.

  28. WilliamPaw表示:

    http://medicinefromindia.store/# online pharmacy india

  29. BennyNib表示:

    ed meds online without doctor prescription: generic cialis without a doctor prescription – buy prescription drugs online

發佈留言

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