PHP Classes

File: function.imagecreatefromsvg.php

Recommend this page to a friend!
  Classes of Uku-Kaarel Jo~esaar   PHP SVG to PNG   function.imagecreatefromsvg.php   Download  
File: function.imagecreatefromsvg.php
Role: Auxiliary script
Content type: text/plain
Description: imagecreatefrom... function
Class: PHP SVG to PNG
Render a SVG graphic as a PNG image
Author: By
Last change: Fixed: alpha handling
Date: 15 years ago
Size: 1,897 bytes
 

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.0, 0.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_rc, false);
   
imagesavealpha($tmpf_png_rc, true);
   
    return
$tmpf_png_rc;
   

    }


?>