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

45,524 Responses

  1. LeonardFaulk表示:

    lisinopril 104: lisinopril 5mg tablets – lisinopril 10 12.5 mg tablets

  2. JamesCof表示:

    amoxicillin 500mg without prescription: generic amoxicillin cost – amoxicillin 825 mg

  3. Stephenpem表示:

    http://amoxil.cheap/# where to buy amoxicillin 500mg without prescription

  4. JamesCof表示:

    buy amoxicillin 500mg canada: can you purchase amoxicillin online – amoxicillin 500mg price in canada

  5. Uubynw表示:

    buy priligy 90mg generic dapoxetine online order misoprostol online

  6. Stephenpem表示:

    http://stromectol.fun/# buy minocycline 50 mg

  7. My brother recommended I might like this website. He was entirely right.
    This put up actually made my day. You can not believe just how a lot time I had spent for this
    information! Thanks!

  8. JamesCof表示:

    lasix for sale: furosemide 40 mg – lasix uses

  9. DavidRak表示:

    lisinopril 10 best price lisinopril in usa lisinopril 20 mg generic

  10. DavidRak表示:

    lisinopril 80mg cost for 20 mg lisinopril lisinopril 10

  11. JamesCof表示:

    lasix: Buy Lasix – lasix side effects

  12. DavidRak表示:

    ivermectin 1 topical cream ivermectin tablet 1mg stromectol 3 mg tablet

  13. JamesCof表示:

    lisinopril cost 5mg: cheapest price for lisinopril india – prinivil medication

  14. LeonardFaulk表示:

    prednisone 2.5 mg tab: can you buy prednisone online uk – average cost of prednisone

  15. JamesCof表示:

    lisinopril 40 mg discount: lisinopril tablets for sale – zestril 5mg

  16. Stephenpem表示:

    http://buyprednisone.store/# prednisone 10mg tabs

  17. JamesCof表示:

    furosemide 40mg: lasix tablet – lasix dosage

  18. DavidRak表示:

    where to buy lisinopril 2.5 mg zestril tablet price cheap lisinopril

  19. DavidRak表示:

    ivermectin where to buy stromectol generic name stromectol 6 mg tablet

  20. DavidRak表示:

    purchase amoxicillin online buy cheap amoxicillin online amoxicillin 500mg for sale uk

  21. JamesCof表示:

    prednisone 54899: prednisone 1 mg daily – prednisone 10 mg coupon

  22. Stephenpem表示:

    https://furosemide.guru/# generic lasix

  23. JamesCof表示:

    lasix: Buy Lasix No Prescription – furosemida

  24. JamesCof表示:

    ivermectin 10 mg: stromectol tab 3mg – ivermectin gel

  25. Stephenpem表示:

    https://furosemide.guru/# lasix uses

  26. ram表示:

    One piece at a time is more than enough to make the statement you’re looking for. Choose one piece of animal print at a time and keep the rest of the outfit mild. Choosing a tiger print handbag or wearing python pumps is all that is needed to make a bold statement. Choose one piece of animal print at a time: one bag, one piece of clothing, or one pair of shoes, and stop there. You can create a glam going-out outfit with a wide assortment of animal print bodysuits like leopard, snake, and cheetah. These sexy bodysuits feature lace-up details, daring open backs, tie-fronts, and tie waist belts to accent cropped hems. Style animal print bodysuits with faux leather or faux suede mini skirts and mules to create a night-out outfit that brings major appeal. In fact, I found 242 styles of leopard print shoes at Neiman Marcus in heels, boots, sneakers, and sandals. You can browse them all here.
    https://www.inquirer.com/news/fashion-philadelphia-post-pandemic-local-designers-20210713.html
    Today’s designers target an increasingly broad audience with their boundary-crossing work, and their tendency to play off of each other’s ideas means that every walk down the runway is also a walk through an entire history of fashion design and dress craftsmanship. Designer One Shoulder Ruffle Party Wear Gown Heavy Net “I’m convinced this is the cutest swimsuit on the planet. It’s absolutely precious! Also RuffleButts is the only swimsuit brand I’ve found so far that doesn’t have cheeky cut bottoms and I LOVE that.” PRIVATE SALES I EARLY ACCESS I POINTS FOR EVERY $1 Already have an account? Sign In Here Wisteria Ruffled Maxi Dress BCBGMAXAZRIA offers an array of ruffle dresses with unique design placement: from asymmetrical ruffles for a modern contemporary edge, to tiered ruffles for a romantic look, to flounce hems for a playful vibe. Shop the collection to find a ruffle dress for all your moods.

  27. LeonardFaulk表示:

    buy lisinopril canada: lisinopril 2019 – buy lisinopril

  28. ram表示:

    By selecting opt-in, you agree to the styleREWARDS program Terms & Conditions. To see how we may use your information, take a look at our privacy policy. You may withdraw from the program at any time. By clicking ‘Become a member’, I agree to the H&M Membership Terms and conditions. Thank you for registering. This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply. ]] Selkie is a no waste company, WRAP and BSCI production certified, offering an inclusive size range. You can email the site owner to let them know you were blocked. Please include what you were doing when this page came up and the Cloudflare Ray ID found at the bottom of this page. Fuchsia Strapless Contemporary Jumpsuit Have your MERABI moment in our luxury partywear. Our unique outfits are handmade to the smallest, intricate detail, making you feel as luxurious and you look. If you have any questions regarding our collections, please head over to our FAQ page. Alternatively, if you can’t find what you’re looking for, contact our friendly team.  
    https://claytonuzdh108753.bloggip.com/23138405/conjunto-lenceria-encaje
    Dresses are a natural choice for business casual attire. It’s one item on and off you go to work. With dresses, you don’t have to worry about what pants match which top or vice versa. They also work well with business casual cardigans and blazers when the weather is chillier. Necklines and sleeves should follow the rules noted above for blouses. Hemlines for both skirts and dresses can range from a few inches above the knee to just below the knee. Minis, high-low, and asymmetrical hemlines are best left in the closet during the week. No! There is no “perfect” business casual outfit for the office because it’s all about finding the right fit and comfort level for the environment you’re working in. Just as there is no one-size-fits-all equation for each style of dress, each office will vary, too. What matters most is that your workwear wardrobe is a reflection of your personal style and makes you feel confident when entering the office.

  29. JamesCof表示:

    furosemide 40mg: lasix side effects – furosemide 40 mg

發佈留言

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