Login   Register  
PHP Classes
elePHPant
Icontem

File: function.imagecreatefromsvg.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Uku-Kaarel Jo~esaar  >  svg2png  >  function.imagecreatefromsvg.php  
File: function.imagecreatefromsvg.php
Role: Auxiliary script
Content type: text/plain
Description: imagecreatefrom... function
Class: svg2png
Render a SVG graphic as a PNG image
 

Contents

Class file image Download
<?php

/**
 * imagecreatefromsvg - Create new image from file or URL
 * 
 * Function requires Inkscape installed for run.
 *
 * Usage:
 * ------
      '/usr/share/icons/Humility/scalable/mimetypes/gnome-mime-application-msword.svg'
    $img_res =imagecreatefromsvg( $filename );
    
    header( 'Content-type: image/jpeg' );
    imagejpeg( $img_res);
 *
 *
 * @param string $filename
 * @return resource or FALSE
 * @author ukj@ukj.pri.ee ; http://ukj.pri.ee
 * @version 0.9b
 */
function imagecreatefromsvg$filename ) {

    if( 
function_exists('sys_get_temp_dir')) {
        
$tmpdir sys_get_temp_dir();
        }
    elseif( 
is_dir'/tmp')) {
        
$tmpdir '/tmp/';
        }
    elseif( 
is_dir'C:\\windows\\tmp')) {
        
$tmpdir 'C:\\windows\\tmp\\';
        }

    
$svg_str file_get_contents$filename );
    if( 
$tmpv_svgf === FALSE ) return FALSE;
    else {
        
$aexecout_S  '';
        
$aexecout_Sa = array('svg2'0.0 0.0 0.00.0);
        
$aexecout_W $aexecout_H 0.0;
        
        
$tmpf_svg tempnam$tmpdir 'icfsvg_' );
        
$resf_svg = @fopen $tmpf_svg'wb' );
        
fwrite$resf_svg$svg_str );
        
fclose($resf_svg);

        
$tmpf_png tempnam$tmpdir 'icfpng_' );
        
        
/* Prints a comma delimited listing of all objects in the SVG 
           document with IDs defined, along with their x, y, width, 
           and height values. */  
        
exec'inkscape -z -S "' $tmpf_svg '"' $aexecout_S );
        
$aexecout_Sa explode',' $aexecout_S[0] );
        
        if(
$aexecout_Sa[3]+$aexecout_Sa[4] > 3000) return FALSE;
        
        
exec'inkscape -z --export-area-canvas --export-png="' $tmpf_png '" "' $tmpf_svg '"' );
    }

    
// echo $tmpf_svg .  ', ' . $tmpf_png;

    
unlink$tmpf_svg );
    
    
    
    
$tmpf_png_rc imagecreatefrompng $tmpf_png );
    
    
// Turn off alpha blending and set alpha flag
    
imagealphablending($tmpf_png_rcfalse);
    
imagesavealpha($tmpf_png_rctrue);
    
    return 
$tmpf_png_rc;
    

    }


?>