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

  1. How To Find The Perfect Accident Claim Lawyers
    Online accident Attorney No injury

  2. 10 Tips For Pram Pushchair 2 In 1 That Are Unexpected Stroller Rocker 2 in 1

  3. Greetings, companion enthusiast. I must applaud you on your discerning analysis of the subject matter
    presented in this blog post. Your sharp observations and fluent commentary
    have struck a chord with me, and I ascertain myself in alignment with the
    convictions you have proclaimed.

    Given your manifest involvement in the realm of internet
    finance, I would be delinquent if I did not present
    an overture for you to discover the stimulating world of Crypto Casino.
    This novel platform offers a rare and absorbing
    escapade , fusing the fervor of established casino gaming with the groundbreaking
    infrastructure of decentralized networks . I consider
    you would encounter it a most spellbinding journey .
    I urge you to engage with us and unveil the boundless avenues that are present you.

    Also visit my page – online casino email marketing

  4. 15 Trends To Watch In The New Year Travel Electric Wheelchair
    electric Mobility chairs for Adults

  5. You’ll Never Guess This Demo Slot Pragmatic Hades’s Tricks Demo Slot Pragmatic Hades (https://Glamorouslengths.Com/Author/Oboesteam1/)

  6. Benjamin表示:

    Hello There. I found your blog using msn.
    This is a really well written article. I’ll make sure to bookmark it and return to read more of your
    useful info. Thanks for the post. I’ll certainly comeback.

  7. Jani表示:

    Thanks for sharing your thoughts about Natural Creations Supplements.
    Regards

  8. I do not even understand how I ended up right here, however I
    assumed this publish used to be great. I don’t recognize who you’re however definitely
    you’re going to a well-known blogger when you are not already.
    Cheers!

  9. Agnes表示:

    Saved as a favorite, I really like your website!

  10. Fantastic Response to Internet Log Input

    Superb entry! I’m certainly delighting in the text on this site.
    Own you in the past considered about getting into cyberspace betting playing?
    Evolution Gaming is a incredible service comprising
    a extensive collection of high-quality immersive croupier products.
    The whole encounter is unbelievably riveting and true, it
    seems like you’re squarely there part of the wagering.

    Provided you’re inclined pertaining to trying the service out, I’d be quite thrilled to hand
    over the guidance link. The Evolution Gaming Platform includes a superb onboarding reward for interested new users.

    It surely certainly helpful looking into more provided
    you’re looking for a changed web-based gambling
    outing.

    Thankfulness again on behalf of the wonderful blog information. Continue with the the fantastic work!

    Also visit my blog post – gambling VIP programs – https://www.cheaperseeker.com/

  11. Five Pod And Capsule Coffee Machines Projects To Use For Any Budget coffee pod machines
    (https://emplois.fhpmco.fr)

  12. you can buy tickets–which are usually £40.00 for girls and £160.ラブドール えろ

  13. and heterosexual women.ラブドール オナホLittle is known,

  14. ラブドール エロMinimal Oiling One of many critical advancements more than TPE could be the significantly decreased oil secretion,generating servicing easier and much more practical.

  15. ラブドール エロBut over time,this intensity lessens if it is just infatuation,

  16. えろ 人形表示:

    Sometimes we can’t stand a person because we can’t stand how they make us feel about ourselves.We feel put down by them,リアル ドール

  17. Gender roles play a key role in sexual desirA new study shows couples who reject traditional roles and embrace equality experience greater sexual desirThe implications are important in light of the growing “trad wife” trend.オナドールIt has long been held that a confluence of factors affects sexual desir The discrepancy in sexual desire remains one of the most common yet complex issues treated by sex and relationship therapists.

  18. At the end of the massage (during which we both fell into deep,deep sleeps),ラブドール えろ

  19. they will remain solidly connected to a foundation of life that provides great strength and stability.ラブドール おすすめAnd this seems to insulate people from a great deal of stress while optimizing their biopsychosocial health.

  20. but run for President herself.Marriage is one of life’s greatest adventures,リアル ラブドール

  21. that by the time they’re married they’ve been conditioned to react against intimacy.人形 エロOf course this doesn’t happen 100 of the time,

  22. which helps them climax.And most cubs appreciate having experienced teachers who clue them into the fine points of pleasuring women and helping them come.高級 ラブドール

  23. 人形 エロ表示:

    “”I really hate any position that does not involve me not lying on my back or cowgirl.It feels awkward and I end up concentrating on where my legs/arms are vs how it actually feels.人形 エロ

  24. 744232.xyz表示:

    What Freud Can Teach Us About Table Top Fridges For Sale
    744232.xyz

  25. えろ 人形表示:

    and external (exogenous or extrinsic) motivation.Or what psychodynamic psychotherapists call primary and secondary gain.ラブドール エロ

  26. Nice weblog here! Additionally your site a lot up fast!
    What web host are you the use of? Can I am getting your associate hyperlink in your host?
    I desire my web site loaded up as quickly as yours lol

    Here is my blog; the billionaire brain wave

  27. ラブドール おすすめIt cannot be emphasized enough how important it is that your spouse is on the same page as you.Setting healthy boundaries as a couple also includes agreeing on how much contact your minor children will have with your in-laws.

  28. You might even turn this into a recreational research project as you seek ways to grow.Many of us have fallen madly in love,ラブドール 中古

發佈留言

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