jjzjj

php - 如何使用 php 将 VML 转换为 png

coder 2024-04-20 原文

我搜索了两天,但只有一个解决方案 VectorConvetor它似乎不起作用。实际上我正在为 InternetExplorer 使用 excanvas,并希望将该图像保存为 png。 IE 提供 VML,但无法使用 PHP 将其转换为 png。我听说过将 VML 渲染到 IE 并使用 PHP 截取屏幕截图,但也没有找到令人满意的解决方案。

最佳答案

使用vml2svg.xsl and its dependencies作为调用 transformToXML 的对象的导入样式表将 VML 转换为 SVG,然后转换 SVG to PNG使用 readImageBlob 和 imagemagick:

<?xml version="1.0" encoding="ISO-8859-1" ?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns="http://www.w3.org/2000/svg" 
    xmlns:v="urn:schemas-microsoft-com:vml" 
    xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    xmlns:html="http://www.w3.org/1999/xhtml" 
    xmlns:xlink="http://www.w3.org/1999/xlink"
>

<!-- INDEX:
   * template:
        - vml-shape
    * match:
        - /
        - v:group
        - v:shape
-->

<!-- XXXXXXXXXXXXXXXX VARIABILI GLOBALI XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX -->
<xsl:variable name="n-elementi">
    <xsl:value-of select="count(//*)" />
</xsl:variable>

<!-- Queste variabili rappresentano una dimensione "standard" in pixel della parte di
     schermo usabile per rappresentare l'immagine (escludendo la porzione occupata dalla
     barra dei browser), per schermi di 17 pollici, con risoluzione 800x600.
     Si usano come approssimazione quando un immagine non specifica la
     sua dimensioni oppure è espressa tramite percentuale.
-->
<xsl:variable name="schermo-x">
    <xsl:text>750</xsl:text>
</xsl:variable>

<xsl:variable name="schermo-y">
    <xsl:text>400</xsl:text>
</xsl:variable>
<!-- XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX -->

<!-- ********************************************************************************* -->
<!-- ********************************* RADICE **************************************** -->
<!-- ********************************************************************************* -->
<xsl:template match="/">

<!-- creo un elemento svg contenitore, che non influenza nessun altra misura, serve solo
     da contenitore, in quanto svg ha bisogno di un elemento radice.
     Inoltre questo elemento conterrà un elemento defs con tutti i riferimenti contenuti
     nel documento.
-->

<svg preserveAspectRatio="none" 
     overflow="visible">

<!-- NB: da aggiungere un choose per vml non contenuti in un documento html -->

<xsl:for-each select="//html:BODY | //html:body">

    <!-- gestione di defs -->
    <defs>
        <xsl:call-template name="gestione-textpath" />        
        <xsl:call-template name="gestione-gradient" />
        <xsl:call-template name="gestione-pattern" />
    </defs>

    <xsl:apply-templates />

</xsl:for-each>
</svg>
</xsl:template>

<!-- ********************************************************************************* -->
<!-- ********************************************************************************* -->
<!-- ************************ ELEMENTO GROUP ***************************************** -->
<!-- ********************************************************************************* -->
<!-- ********************************************************************************* -->
<xsl:template match="v:group">

<!-- per ogni gruppo trovaro creo un elemento svg -->
<svg>
    <xsl:call-template name="core-attrs" />
    <xsl:call-template name="attributo-viewbox" />
        <!-- svg di default fa clipping, vml non lo fa, quindi devo impostare un attributo
             per segnalare di far vedere il contenuto fuori dai margini
        -->
    <xsl:attribute name="overflow"><xsl:text>visible</xsl:text></xsl:attribute>
    <xsl:call-template name="attributo-title" />

<!-- controllo un eventuale presenza di rotazioni -->
<xsl:variable name="r">
    <xsl:call-template name="attributo-rotation" />
</xsl:variable>

    <xsl:variable name="v-coordsize">
        <xsl:call-template name="valore-coordsize" />
    </xsl:variable>
    <xsl:variable name="vb_x"> 
        <xsl:value-of select="substring-before(normalize-space($v-coordsize),' ')" />
    </xsl:variable>
    <xsl:variable name="vb_y"> 
        <xsl:value-of select="substring-after(normalize-space($v-coordsize),' ')" />
    </xsl:variable>

    <xsl:choose>
        <!-- se ho una rotazione nell'elemento group, creo un gruppo per gestire
             questa rotazione, posizionandomi nel centro della figura, perchè
             svg e vml gestiscono in modo diverso le rotazioni: il primo 
             rispetto al punto in alto a sinistra (salvo diversa disposizione)
             e l'altro rispetto al centro.
        -->
        <xsl:when test="$r != '0'">
            <g>
                <xsl:attribute name="transform">
                    <xsl:text>rotate(</xsl:text>
                    <xsl:value-of select="$r" />
                    <xsl:text>, </xsl:text>
                    <xsl:value-of select="($vb_x div 2)" />
                    <xsl:text>, </xsl:text>
                    <xsl:value-of select="($vb_y div 2)" />
                    <xsl:text>)</xsl:text>
                </xsl:attribute>

                <xsl:apply-templates />
            </g>
        </xsl:when>
        <xsl:otherwise>
            <xsl:apply-templates />
        </xsl:otherwise>
    </xsl:choose>
</svg>

</xsl:template>

<!-- ********************************************************************************* -->
<!-- ********************************************************************************* -->
<!-- ************************ ELEMENTO SHAPE ***************************************** -->
<!-- ********************************************************************************* -->
<!-- ********************************************************************************* -->
<xsl:template match="v:shape">

<!-- per ogni elemento shape creo un elemento svg, impostando opportunamente gli attributi
     e gestendo/creando gli altri elementi per rappresentare eventuali path, testi o 
     immagini esterno (in quanto questi elementi sono esclusivamente contenuti in shape).
     Vengono gestiti inoltre eventuali riferimenti a shapetype, considerando gli 
        attributi sia di quest'elemento shape, sia dei shapetype riferiti, poi si
        impostano i valori dando precedenza agli attributi di shape, poi vengono quelli di
        shapetype, altrimenti si usano i valori di default o quelli ereditati
-->

<!-- cerco un eventuale riferimento a un elemento shapetype -->
<xsl:variable name="id-of-shapetype">
    <xsl:value-of select="substring-after(@type,'#')" />
</xsl:variable>

<!-- cerco un eventuale riferimento ad un immagine, contenuto o in questo elemento
     shape o in elementi shapetype riferiti
-->
<xsl:variable name="image_present">
    <xsl:choose>
        <xsl:when test="(//v:shapetype[@id = $id-of-shapetype]/v:imagedata) 
                        or (v:imagedata)" ><xsl:text>yes</xsl:text></xsl:when>
        <xsl:otherwise><xsl:text></xsl:text></xsl:otherwise>
    </xsl:choose>
</xsl:variable>

<!-- contiene yes o stringa vuota a seconda che debba o meno effettuare un riempimento:
      la scelta è dovuta alla presenza di un immagine o all'impostazione di un attributo
      per segnalare di non effettuare il riempiemento (fillok dell'elemento path)
-->
<xsl:variable name="no-fill">
   <xsl:choose>
        <xsl:when test="$image_present = 'yes'">
            <xsl:text>yes</xsl:text>
        </xsl:when>
        <xsl:when test="v:path[@fillok = 'true']">
            <xsl:text></xsl:text>
        </xsl:when>
        <xsl:when test="v:path[@fillok = 'false']">
            <xsl:text>yes</xsl:text>
        </xsl:when>
        <xsl:when test="(//v:shapetype[@id = $id-of-shapetype]/v:path[@fillok = 'true'])">
            <xsl:text></xsl:text>
        </xsl:when>  
        <xsl:when test="(//v:shapetype[@id = $id-of-shapetype]/v:path[@fillok = 'false'])">
            <xsl:text>yes</xsl:text>
        </xsl:when>         
        <xsl:otherwise>
            <xsl:text></xsl:text>
        </xsl:otherwise>
    </xsl:choose>
</xsl:variable>

<!-- segnala se devo disegnare i bordi, determinato dal valore dell'attributo strokeok, presente
     in elementi path, figli di shape o di shapetype riferiti
-->
<xsl:variable name="no-stroke">
   <xsl:choose>
        <xsl:when test="v:path[@strokeok = 'true']">
            <xsl:text></xsl:text>
        </xsl:when>
        <xsl:when test="v:path[@strokeok = 'false']">
            <xsl:text>yes</xsl:text>
        </xsl:when>
        <xsl:when test="(//v:shapetype[@id = $id-of-shapetype]/v:path[@strokeok = 'true'])">
            <xsl:text></xsl:text>
        </xsl:when>  
        <xsl:when test="(//v:shapetype[@id = $id-of-shapetype]/v:path[@strokeok = 'false'])">
            <xsl:text>yes</xsl:text>
        </xsl:when>         
        <xsl:otherwise>
            <xsl:text></xsl:text>
        </xsl:otherwise>
    </xsl:choose>
</xsl:variable>

        <!-- creo l'elemento svg -->
        <svg>
            <xsl:call-template name="core-attrs" />

            <!-- imposto coordsize (viewbox di svg):
                    prima lo cerco nell'elemento shape e se c'è lo imposto,
                    poi lo cerco in shapetype ed eventualmente lo imposto,
                    altrimenti cerco negli elementi ancestor 
             -->
            <xsl:choose>
                <xsl:when test="@coordsize">
                    <xsl:call-template name="attributo-viewbox" />
                </xsl:when>
                <xsl:when test="//v:shapetype[@id = $id-of-shapetype]">
                    <xsl:for-each select="//v:shapetype[@id = $id-of-shapetype]">
                        <xsl:call-template name="attributo-viewbox" />
                    </xsl:for-each>   
                </xsl:when>
                <xsl:otherwise>
                    <xsl:call-template name="attributo-viewbox" />
                </xsl:otherwise>
            </xsl:choose>

            <!-- imposto le proprietà di fill e stroke:
                    - prima le cerco in eventuali shapetype e imposto gli 
                        attributi;
                    - poi le cerco in shape, creando gli attributi che 
                        sovrascriveranno quelli impostati in precedenza e 
                        specifico (parametro default = no) che nel caso non
                        vengano trovate le proprietà non si devono impostare
                        i valori di default, altrimenti questi sovrascriverebbero
                        i valori impostati con shapetype (da notare che se una 
                        proprietà non è presente ne in shape che in shapetype, 
                        quando cerco i valori in st, non trovandoli vengono 
                        impostati con i valori di default e la successiva
                        ricerca in shape li lascerà inalterati.
            -->
            <xsl:choose>
                <xsl:when test="//v:shapetype[@id = $id-of-shapetype]">
                    <xsl:for-each select="//v:shapetype[@id = $id-of-shapetype]">
                        <xsl:call-template name="attributi-paint">
                            <xsl:with-param name="no-fill">
                                <xsl:value-of select="$image_present" />
                            </xsl:with-param>
                        </xsl:call-template>
                    </xsl:for-each>
                    <xsl:call-template name="attributi-paint">
                        <xsl:with-param name="default">
                            <xsl:text>no</xsl:text>
                        </xsl:with-param>
                        <xsl:with-param name="no-fill">
                            <xsl:value-of select="$no-fill" />
                        </xsl:with-param>   
                        <xsl:with-param name="no-stroke">
                            <xsl:value-of select="$no-stroke" />
                        </xsl:with-param>                      
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:call-template name="attributi-paint">
                        <xsl:with-param name="no-fill">
                            <xsl:value-of select="$no-fill" />
                        </xsl:with-param>
                        <xsl:with-param name="no-stroke">
                            <xsl:value-of select="$no-stroke" />
                        </xsl:with-param>    
                    </xsl:call-template>
                </xsl:otherwise>
            </xsl:choose>

            <!-- vml non presenta questa proprietà, tuttavia il comportamento
                 di vml equivale in svg alla proprietà impostata con evenodd,
                 mentre svg per default ha questa proprietà impostata a nonzero
            -->
            <xsl:attribute name="fill-rule">
                <xsl:text>evenodd</xsl:text>
            </xsl:attribute>

            <xsl:call-template name="attributo-title" />


            <xsl:variable name="r">
                <xsl:call-template name="attributo-rotation" />
            </xsl:variable>

            <!-- gestisco il contenuto di shape, controllando l'eventuale 
                 presenza di rotazioni (in caso affermativo creo un
                 gruppo con trasform, posizionandolo opportunamente), poi
                 cerco eventuali link associati alla figura, tramite il template
                 gestione-href, il quale in caso di link, creerà un elemento a e
                 chiamarà un template per gestire il contenuto di shape (vml-shape).
                 In caso non siano presenti link verrà chiamato ugualmente il 
                 template vml-shape (senza creare l'elemento a).
            -->
            <xsl:choose>
                <xsl:when test="$r != '0'">
                    <g>
                        <!-- devo calcolare questi valori per spostare l'asse 
                             di rotazione: dall'angolo in alto a sx (comportamento
                             di default di svg) al centro della regione.
                        -->
                        <xsl:variable name="cs-w">
                            <xsl:call-template name="valore-coordsize">
                                <xsl:with-param name="parametro"><xsl:text>w</xsl:text>
                                </xsl:with-param>
                            </xsl:call-template>
                        </xsl:variable>
                        <xsl:variable name="cs-h">
                            <xsl:call-template name="valore-coordsize">
                                <xsl:with-param name="parametro"><xsl:text>h</xsl:text>
                                </xsl:with-param>
                            </xsl:call-template>
                        </xsl:variable>

                        <xsl:attribute name="transform">
                            <xsl:text>rotate(</xsl:text>
                            <xsl:value-of select="$r" />
                            <xsl:text>, </xsl:text>
                            <xsl:value-of select="($cs-w div 2)" /> <!-- $x + .. -->
                            <xsl:text>, </xsl:text>
                            <xsl:value-of select="($cs-h div 2)" />
                            <xsl:text>)</xsl:text>
                        </xsl:attribute>

                        <xsl:call-template name="gestione-href">
                            <xsl:with-param name="nome-template">
                                <xsl:text>vml-shape</xsl:text>   
                            </xsl:with-param>
                        </xsl:call-template>        
                    </g>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:call-template name="gestione-href">
                        <xsl:with-param name="nome-template">
                            <xsl:text>vml-shape</xsl:text>   
                        </xsl:with-param>
                    </xsl:call-template>  
                </xsl:otherwise>
            </xsl:choose>     

        </svg>
</xsl:template>

<!-- ********************************************************************************* -->
<!-- ********************************************************************************* -->
<!-- ***************************** VML SHAPE ***************************************** -->
<!-- ********************************************************************************* -->
<!-- ********************************************************************************* -->
<xsl:template name="vml-shape">

<xsl:variable name="id-of-shapetype">
    <xsl:value-of select="substring-after(@type,'#')" />
</xsl:variable>

<!-- valore di coordsize(w) / w di tutti gli elementi ancestor: rappresenta la dimensione
      di un user unit.
-->
<xsl:variable name="aggiustamento">
    <xsl:for-each select="v:*">
        <xsl:if test="position() = last()">
            <xsl:call-template name="calcola-scala" />
        </xsl:if>
    </xsl:for-each>
</xsl:variable>

            <!-- XXXXXXXXXXXXXX GESTIONE IMAGEDATA XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX -->
            <xsl:for-each select="//v:shapetype[@id = $id-of-shapetype]/v:imagedata">
                <xsl:call-template name="elemento-imagedata" />
            </xsl:for-each>
            <xsl:for-each select="v:imagedata">
                <xsl:call-template name="elemento-imagedata" />
            </xsl:for-each>

            <!-- XXXXXXXXXXXXXX GESTIONE PATH - TEXTPATH XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX -->
            <xsl:choose>
                <!-- devo rappresentare textpath -->
                <xsl:when test="((v:path[@textpathok = 'true' or @textpathok = 't']) or
                                (//v:shapetype[@id = $id-of-shapetype]/v:path
                                [@textpathok = 'true' or @textpathok = 't'])) and 
                                ((v:textpath) or 
                                (//v:shapetype[@id = $id-of-shapetype]/v:textpath))">
                    <xsl:choose>
                        <xsl:when test="v:textpath[@string]">
                            <xsl:for-each select="v:textpath[@string]">
                                <xsl:if test="position() = last()">
                                    <xsl:call-template name="elemento-textpath" />
                                </xsl:if>
                            </xsl:for-each>
                        </xsl:when>
                        <xsl:when test="v:textpath">
                            <xsl:variable name="stringa-st">
                                <xsl:for-each select="
                                        //v:shapetype[@id = $id-of-shapetype]/
                                        v:textpath[@string]">
                                    <xsl:if test="position() = last()">
                                        <xsl:value-of select="@string" />             
                                    </xsl:if>
                                </xsl:for-each>

                            </xsl:variable>

                            <xsl:for-each select="v:textpath">
                                <xsl:if test="position() = last()">
                                    <xsl:call-template name="elemento-textpath" >
                                        <xsl:with-param name="stringa">
                                            <xsl:value-of select="$stringa-st" />
                                        </xsl:with-param>
                                    </xsl:call-template>
                                </xsl:if>
                            </xsl:for-each>

                        </xsl:when>
                        <xsl:when test="//v:shapetype[@id = $id-of-shapetype]/v:textpath">
                            <xsl:variable name="path-id">
                                <xsl:choose>
                                    <xsl:when test="v:path[@v != ''] or @path != ''">
                                        <xsl:value-of select="count(preceding::v:*) + 
                                                              count(ancestor::v:*)" />
                                    </xsl:when>
                                    <xsl:otherwise>
                                        <xsl:text></xsl:text>
                                    </xsl:otherwise>
                                </xsl:choose>
                            </xsl:variable>

                            <xsl:for-each select="
                                    //v:shapetype[@id = $id-of-shapetype]/v:textpath">
                                <xsl:if test="position() = last()">
                                    <xsl:choose>
                                        <xsl:when test="$path-id = ''">
                                            <xsl:call-template name="elemento-textpath">
                                                <xsl:with-param name="aggiustamento">
                                                    <xsl:value-of select="$aggiustamento" />
                                                </xsl:with-param>
                                            </xsl:call-template>
                                        </xsl:when>
                                        <xsl:otherwise>
                                            <xsl:call-template name="elemento-textpath" >
                                                <xsl:with-param name="path-id">
                                                    <xsl:value-of select="$path-id" />
                                                </xsl:with-param>
                                                <xsl:with-param name="aggiustamento">
                                                    <xsl:value-of select="$aggiustamento" />
                                                </xsl:with-param>
                                            </xsl:call-template>
                                        </xsl:otherwise>
                                    </xsl:choose>
                                </xsl:if>
                            </xsl:for-each>
                        </xsl:when>
                        <xsl:otherwise><!-- in teoria qui non dovrebbe andarci mai -->
                            <XXX></XXX>
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:when>

                <!-- devo rappresentare path -->

                <xsl:when test="v:path[@v != '']">
                    <xsl:for-each select="v:path[@v != '']">
                        <xsl:if test="position() = last()">
                            <xsl:call-template name="gestione-path" />
                        </xsl:if>
                    </xsl:for-each>
                </xsl:when>
                <xsl:when test="@path != ''">
                    <xsl:call-template name="elemento-path">
                        <xsl:with-param name="v">
                            <xsl:value-of select="@path" />
                        </xsl:with-param>
                    </xsl:call-template>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:for-each select="//v:shapetype[@id = $id-of-shapetype]">
                        <xsl:choose>
                            <xsl:when test="v:path[@v != '']">
                                <xsl:for-each select="v:path[@v != '']">
                                    <xsl:if test="position() = last()">
                                        <xsl:call-template name="gestione-path" />
                                    </xsl:if>
                                </xsl:for-each>
                            </xsl:when>
                            <xsl:when test="@path != ''">
                                <xsl:call-template name="elemento-path">
                                    <xsl:with-param name="v">
                                        <xsl:value-of select="@path" />
                                    </xsl:with-param>
                                </xsl:call-template>
                            </xsl:when>
                        </xsl:choose>
                    </xsl:for-each>
                </xsl:otherwise>
            </xsl:choose>

            <!-- XXXXXXXXXXXXXXXXXXX GESTIONE TEXTBOX XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX -->
            <xsl:if test="(v:textbox)">
               <xsl:call-template name="elemento-textbox"> 
                    <xsl:with-param name="id-of-shapetype">
                        <xsl:value-of select="$id-of-shapetype" />
                    </xsl:with-param>
                    <xsl:with-param name="aggiustamento">
                        <xsl:value-of select="$aggiustamento" />
                    </xsl:with-param>
               </xsl:call-template>
            </xsl:if>

            <xsl:apply-templates />
</xsl:template>

<!-- ********************************************************************************* -->
<!-- ********************************************************************************* -->
<!-- ************************ ELEMENTO SHAPETYPE ************************************* -->
<!-- ********************************************************************************* -->
<!-- ********************************************************************************* -->
<xsl:template name="elemento-shapetype">
<!-- shapetype viene gestito quando e' richiamato da shape -->
</xsl:template>

<!-- ********************************************************************************* -->
<!-- ********************************************************************************* -->
<!-- ************************ TEMPLATE GESTIONE-SHAPETYPE **************************** -->
<!-- ********************************************************************************* -->
<!-- ********************************************************************************* -->
<xsl:template name="gestione-shapetype">
<!-- shapetype viene gestito quando e' richiamato da shape -->
</xsl:template>

<!-- ********************************************************************************* -->
<!-- ********************************************************************************* -->
<!-- ************************ ELEMENTO A ***************************************** -->
<!-- ********************************************************************************* -->
<!-- ********************************************************************************* -->
<xsl:template match="html:a">
<xsl:choose>
    <xsl:when test="@href">
        <a>
            <xsl:attribute name="xlink:href">
                <xsl:value-of select="@href" />
            </xsl:attribute>
            <xsl:apply-templates />
        </a>
    </xsl:when>
    <xsl:otherwise>
        <xsl:apply-templates />
    </xsl:otherwise>
</xsl:choose>
</xsl:template>

关于php - 如何使用 php 将 VML 转换为 png,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7038740/

有关php - 如何使用 php 将 VML 转换为 png的更多相关文章

  1. ruby - 如何使用 Nokogiri 的 xpath 和 at_xpath 方法 - 2

    我正在学习如何使用Nokogiri,根据这段代码我遇到了一些问题:require'rubygems'require'mechanize'post_agent=WWW::Mechanize.newpost_page=post_agent.get('http://www.vbulletin.org/forum/showthread.php?t=230708')puts"\nabsolutepathwithtbodygivesnil"putspost_page.parser.xpath('/html/body/div/div/div/div/div/table/tbody/tr/td/div

  2. ruby - 如何从 ruby​​ 中的字符串运行任意对象方法? - 2

    总的来说,我对ruby​​还比较陌生,我正在为我正在创建的对象编写一些rspec测试用例。许多测试用例都非常基础,我只是想确保正确填充和返回值。我想知道是否有办法使用循环结构来执行此操作。不必为我要测试的每个方法都设置一个assertEquals。例如:describeitem,"TestingtheItem"doit"willhaveanullvaluetostart"doitem=Item.new#HereIcoulddotheitem.name.shouldbe_nil#thenIcoulddoitem.category.shouldbe_nilendend但我想要一些方法来使用

  3. ruby - 使用 RubyZip 生成 ZIP 文件时设置压缩级别 - 2

    我有一个Ruby程序,它使用rubyzip压缩XML文件的目录树。gem。我的问题是文件开始变得很重,我想提高压缩级别,因为压缩时间不是问题。我在rubyzipdocumentation中找不到一种为创建的ZIP文件指定压缩级别的方法。有人知道如何更改此设置吗?是否有另一个允许指定压缩级别的Ruby库? 最佳答案 这是我通过查看ruby​​zip内部创建的代码。level=Zlib::BEST_COMPRESSIONZip::ZipOutputStream.open(zip_file)do|zip|Dir.glob("**/*")d

  4. ruby - 为什么我可以在 Ruby 中使用 Object#send 访问私有(private)/ protected 方法? - 2

    类classAprivatedeffooputs:fooendpublicdefbarputs:barendprivatedefzimputs:zimendprotecteddefdibputs:dibendendA的实例a=A.new测试a.foorescueputs:faila.barrescueputs:faila.zimrescueputs:faila.dibrescueputs:faila.gazrescueputs:fail测试输出failbarfailfailfail.发送测试[:foo,:bar,:zim,:dib,:gaz].each{|m|a.send(m)resc

  5. ruby-on-rails - 使用 Ruby on Rails 进行自动化测试 - 最佳实践 - 2

    很好奇,就使用ruby​​onrails自动化单元测试而言,你们正在做什么?您是否创建了一个脚本来在cron中运行rake作业并将结果邮寄给您?git中的预提交Hook?只是手动调用?我完全理解测试,但想知道在错误发生之前捕获错误的最佳实践是什么。让我们理所当然地认为测试本身是完美无缺的,并且可以正常工作。下一步是什么以确保他们在正确的时间将可能有害的结果传达给您? 最佳答案 不确定您到底想听什么,但是有几个级别的自动代码库控制:在处理某项功能时,您可以使用类似autotest的内容获得关于哪些有效,哪些无效的即时反馈。要确保您的提

  6. ruby - 在 Ruby 中使用匿名模块 - 2

    假设我做了一个模块如下:m=Module.newdoclassCendend三个问题:除了对m的引用之外,还有什么方法可以访问C和m中的其他内容?我可以在创建匿名模块后为其命名吗(就像我输入“module...”一样)?如何在使用完匿名模块后将其删除,使其定义的常量不再存在? 最佳答案 三个答案:是的,使用ObjectSpace.此代码使c引用你的类(class)C不引用m:c=nilObjectSpace.each_object{|obj|c=objif(Class===objandobj.name=~/::C$/)}当然这取决于

  7. ruby-on-rails - 在 Rails 中将文件大小字符串转换为等效千字节 - 2

    我的目标是转换表单输入,例如“100兆字节”或“1GB”,并将其转换为我可以存储在数据库中的文件大小(以千字节为单位)。目前,我有这个:defquota_convert@regex=/([0-9]+)(.*)s/@sizes=%w{kilobytemegabytegigabyte}m=self.quota.match(@regex)if@sizes.include?m[2]eval("self.quota=#{m[1]}.#{m[2]}")endend这有效,但前提是输入是倍数(“gigabytes”,而不是“gigabyte”)并且由于使用了eval看起来疯狂不安全。所以,功能正常,

  8. ruby - 使用 ruby​​ 和 savon 的 SOAP 服务 - 2

    我正在尝试使用ruby​​和Savon来使用网络服务。测试服务为http://www.webservicex.net/WS/WSDetails.aspx?WSID=9&CATID=2require'rubygems'require'savon'client=Savon::Client.new"http://www.webservicex.net/stockquote.asmx?WSDL"client.get_quotedo|soap|soap.body={:symbol=>"AAPL"}end返回SOAP异常。检查soap信封,在我看来soap请求没有正确的命名空间。任何人都可以建议我

  9. python - 如何使用 Ruby 或 Python 创建一系列高音调和低音调的蜂鸣声? - 2

    关闭。这个问题是opinion-based.它目前不接受答案。想要改进这个问题?更新问题,以便editingthispost可以用事实和引用来回答它.关闭4年前。Improvethisquestion我想在固定时间创建一系列低音和高音调的哔哔声。例如:在150毫秒时发出高音调的蜂鸣声在151毫秒时发出低音调的蜂鸣声200毫秒时发出低音调的蜂鸣声250毫秒的高音调蜂鸣声有没有办法在Ruby或Python中做到这一点?我真的不在乎输出编码是什么(.wav、.mp3、.ogg等等),但我确实想创建一个输出文件。

  10. ruby-on-rails - 如何验证 update_all 是否实际在 Rails 中更新 - 2

    给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru

随机推荐