在AWS中使用GraphicsMagick将PSD和EPS转换为PNG或JPG中的node.js?

时间:2015-09-23 13:56:10

标签: javascript node.js imagemagick graphicsmagick

我正在开发一个托管在AWS中的DAM。用户可以将繁重的文件上传到系统。在幕后,当上传图像时,有一个AWS Lambda函数为每个图像创建缩略图。

显然,具有典型HTML img项目的浏览器无法显示格式为.psd和.eps的文件。这就是为什么我需要将这些文件格式转换为.png或.jpg。

也许另一种解决方案是直接在.png中拍摄“即时截图”。我不知道这是否可能。

在Lambda函数上运行的Node.js代码与此处的代码非常相似:http://docs.aws.amazon.com/lambda/latest/dg/walkthrough-s3-events-adminuser-create-test-function-create-function.html

先谢谢你的帮助!!

1 个答案:

答案 0 :(得分:1)

我对AWS,Lambda和Node.js了解不多,但可能对ImageMagick方面有所帮助......

要使用ImageMagick将图像从一种格式转换为另一种格式,您基本上可以在终端或命令行中使用具有相应文件扩展名的 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.0 20120330//EN" "http://jats.nlm.nih.gov/archiving/1.0/JATS-archivearticle1.dtd"[]> <?xml-stylesheet type='text/xsl' href='jats-html.xsl'?> <article xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:mml="http://www.w3.org/1998/Math/MathML" article-type="research-article"><?properties open_access?>程序:

<!-- XSL file -->

    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:xlink="http://www.w3.org/1999/xlink"
      xmlns:mml="http://www.w3.org/1998/Math/MathML"
      exclude-result-prefixes="xlink mml">

      <xsl:output doctype-public="-//W3C//DTD HTML 4.01 Transitional//EN"
        doctype-system="http://www.w3.org/TR/html4/loose.dtd"
        encoding="UTF-8"/>

      <xsl:strip-space elements="*"/>

      <!-- Space is preserved in all elements allowing #PCDATA -->
      <xsl:preserve-space
        elements="abbrev abbrev-journal-title access-date addr-line
                  aff alt-text alt-title article-id article-title
                  attrib award-id bold chapter-title chem-struct
                  collab comment compound-kwd-part compound-subject-part
                  conf-acronym conf-date conf-loc conf-name conf-num
                  conf-sponsor conf-theme contrib-id copyright-holder
                  copyright-statement copyright-year corresp country
                  date-in-citation day def-head degrees disp-formula
                  edition elocation-id email etal ext-link fax fpage
                  funding-source funding-statement given-names glyph-data
                  gov inline-formula inline-supplementary-material
                  institution isbn issn-l issn issue issue-id issue-part
                  issue-sponsor issue-title italic journal-id
                  journal-subtitle journal-title kwd label license-p
                  long-desc lpage meta-name meta-value mixed-citation
                  monospace month named-content object-id on-behalf-of
                  overline p page-range part-title patent person-group
                  phone prefix preformat price principal-award-recipient
                  principal-investigator product pub-id publisher-loc
                  publisher-name related-article related-object role
                  roman sans-serif sc season self-uri series series-text
                  series-title sig sig-block size source speaker std
                  strike string-name styled-content std-organization
                  sub subject subtitle suffix sup supplement surname
                  target td term term-head tex-math textual-form th
                  time-stamp title trans-source trans-subtitle trans-title
                  underline uri verse-line volume volume-id volume-series
                  xref year
                  mml:annotation mml:ci mml:cn mml:csymbol mml:mi mml:mn 
                  mml:mo mml:ms mml:mtext"/>

      
      <xsl:param name="transform" select="'jats-html.xsl'"/>

      <xsl:param name="css" select="'jats-preview.css'"/>
      
      <xsl:param name="report-warnings" select="'no'"/>
      
      <xsl:variable name="verbose" select="$report-warnings='yes'"/>
      
      <!-- Keys -->

      <!-- To reduce dependency on a DTD for processing, we declare
           a key to use instead of the id() function. -->
      <xsl:key name="element-by-id" match="*[@id]" use="@id"/>

      <!-- Enabling retrieval of cross-references to objects -->
      <xsl:key name="xref-by-rid" match="xref" use="@rid"/>
      
      <!-- ============================================================= -->
      <!--  ROOT TEMPLATE - HANDLES HTML FRAMEWORK                       -->
      <!-- ============================================================= -->

      <xsl:template match="/">
        <html>
          <!-- HTML header -->
          <xsl:call-template name="make-html-header"/>
          <body>
            <xsl:apply-templates/>
          </body>
        </html>
      </xsl:template>


      <xsl:template name="make-html-header">
        <head>
          <title>
            <xsl:variable name="authors">
              <xsl:call-template name="author-string"/>
            </xsl:variable>
            <xsl:value-of select="normalize-space(string($authors))"/>
            <xsl:if test="normalize-space(string($authors))">: </xsl:if>
            <xsl:value-of
              select="/article/front/article-meta/title-group/article-title[1]"/>
          </title>
          <link rel="stylesheet" type="text/css" href="{$css}"/>
          <!-- XXX check: any other header stuff? XXX -->
        </head>
      </xsl:template>


    <!-- ============================================================= -->
    <!--  TOP LEVEL                                                    -->
    <!-- ============================================================= -->

      <!--
          content model for article:
             (front,body?,back?,floats-group?,(sub-article*|response*))
          
          content model for sub-article:
             ((front|front-stub),body?,back?,floats-group?,
              (sub-article*|response*))
          
          content model for response:
             ((front|front-stub),body?,back?,floats-group?) -->
      
      <xsl:template match="article">
        <xsl:call-template name="make-article"/>
      </xsl:template>

      <xsl:template match="sub-article | response">
        <hr class="part-rule"/>
        <xsl:call-template name="make-article"/>
      </xsl:template>


    <!-- ============================================================= -->
    <!--  "make-article" for the document architecture                 -->
    <!-- ============================================================= -->

      <xsl:template name="make-article">
        <!-- Generates a series of (flattened) divs for contents of any
    	       article, sub-article or response -->

        <!-- variable to be used in div id's to keep them unique -->
        <xsl:variable name="this-article">
          <xsl:apply-templates select="." mode="id"/>
        </xsl:variable>

        <div id="{$this-article}-front" class="front">
          <xsl:apply-templates select="front | front-stub"/>
        </div>

        <!-- body -->
        <xsl:for-each select="body">
          <div id="{$this-article}-body" class="body">
            <xsl:apply-templates/>
          </div>
        </xsl:for-each>

        <xsl:if test="back | $loose-footnotes">
          <!-- $loose-footnotes is defined below as any footnotes outside
               front matter or fn-group -->
          <div id="{$this-article}-back" class="back">
            <xsl:call-template name="make-back"/>
          </div>
        </xsl:if>

        <xsl:for-each select="floats-group | floats-wrap">
          <!-- floats-wrap is from 2.3 -->
          <div id="{$this-article}-floats" class="back">
            <xsl:call-template name="main-title">
              <xsl:with-param name="contents">
                <span class="generated">Floating objects</span>
              </xsl:with-param>
            </xsl:call-template>
            <xsl:apply-templates/>
          </div>
        </xsl:for-each>

        <!-- more metadata goes in the footer -->
        <div id="{$this-article}-footer" class="footer">
          <xsl:call-template name="footer-metadata"/>
          <xsl:call-template name="footer-branding"/>
        </div>

        <!-- sub-article or response (recursively calls
    		     this template) -->
        <xsl:apply-templates select="sub-article | response"/>

      </xsl:template>


    <!-- ============================================================= -->
    <!--  End stylesheet                                               -->
    <!-- ============================================================= -->

    </xsl:stylesheet>

EPS文件

使用convert这是一种矢量格式,你通常应该首先设置密度,否则ImageMagick将使用72 dpi,这会产生可怕的质量,所以对于convert input.jpg output.png # convert a JPEG to a PNG 尝试类似:

EPS

PSD文件

使用Photoshop EPS文件时,通常会有一个预览图像以及之后的所有多个图层,因此,如果您希望获得预览,则应使用此样式的命令来处理图层0​​预览在PSD文件中:

convert -density 144 input.eps output.png

如果你想减小图像的大小,你可以在加载后调整大小:

PSD

使其不超过512像素宽或256像素高。

您可能想要做的另一件事是从图像中删除元数据(时间/日期,相机模型,创建应用程序,相机的GPS位置),为此,在输出之前添加convert input.psd[0] output.png 文件名。

不确定我还能提供哪些帮助,但希望能让你开始。