save SVG output by PHP in SVG file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cleary1981
    New Member
    • Jun 2008
    • 178

    save SVG output by PHP in SVG file

    Hi, I have created a script in PHP thats generates an SVG image. Want I want to do is have PHP save this as example.svg. Can this be done? Heres my code

    Code:
    <?php
    require "config.php";
    $proj_id = $_GET['proj'];
    
    //set the content type 
    
    header("Content-type: image/svg-xml"); 
    
    //mark this as XML 
    
    echo(<?xml version="1.0" encoding="iso-8859-1"?>' . "\n"); 
    
    //Point to SVG DTD 
    
    echo('<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20000303 Stylable//EN" '); 
    
    echo('"http://www.w3.org/TR/2000/03/WD-SVG-20000303/DTD/' . 
    
    'svg-20000303-stylable.dtd">' . "\n"); 
    
    
    //start SVG document, set size in "user units" 
    
    echo("<svg xml:space=\"preserve\" " . 
    
    "width=\"36cm\" height=\"24cm\" " . 
    
    "viewBox=\"0 0 0 0\">\n"); //x, y, ??, zoomout
    
    
    $today = date('d/m/Y');
    $projectQ1 = mysql_query("SELECT * FROM project WHERE proj_id='$proj_id'") or die(mysql_error());
    $rowQ1 = mysql_fetch_assoc($projectQ1);
    $company = $rowQ1['company_name'];
    $project = $rowQ1['project_name'];
    $form = $rowQ1['form'];
    $access = $rowQ1['access'];
    $entry = $rowQ1['cable_entry'];
    $ip = $rowQ1['ip_rating'];
    $fault = $rowQ1['fault_rating'];
    	
    echo '<g id="main">';
    
    
    //draw each object
    
    	$results = mysql_query("SELECT object_name, module_id, xpos, ypos, height, width FROM object, module WHERE object.module_name = module.module_name AND proj_id='$proj_id'") or die();
    	while($values = mysql_fetch_array($results)){
    		$object = $values['object_name'];
    		$module_id = $values['module_id'];
    		$mod = "SELECT module_name FROM module WHERE module_id = $module_id";
    		$res = mysql_query($mod) or trigger_error(mysql_error());
    		$row = mysql_fetch_object($res);
    		$module=$row->module_name;
    		$x = $values['xpos'];
    		$y = $values['ypos'];
    		$height = $values['height']/10;
    		$width = $values['width']/10;
    		$xx = $x + 5;
    		$yy = $y + 10;
    		echo("<rect x=\"$x\" y=\"$y\" " . 
    
    		"width=\"$width\" height=\"$height\" " . 
    
    		"style=\"fill:white; stroke:black; " . "stroke-width:2px\" />\n");
    		
    		echo("<text x=\"$xx\" y=\"$yy\" " . 
    
    		"style=\"file:black; font-size:10\">" . 
    
    		"$object" . 
    
    		"</text>\n");
    	
        }
    
    // add busbar
    // calculate length of panel Left(smallest X, smallest Y) Right(largest X, smallest Y)+ length
    $q1 = "SELECT module_name, object_name, xpos, ypos FROM object WHERE proj_id='$proj_id' ORDER BY xpos DESC ,ypos ASC LIMIT 1"; // Right
    $result = mysql_query($q1) or trigger_error(mysql_error());
    $row = mysql_fetch_array($result);
    $module = $row['module_name'];
    $R_object = $row['object_name'];
    $R_x = $row['xpos'];
    $R_y = $row['ypos'];
    
    $q3 = "SELECT width FROM module WHERE module_name = '$module'";
    $result3 = mysql_query($q3) or trigger_error(mysql_error());
    $row3 = mysql_fetch_array($result3);
    $Lwidth = $row3['width'];
    
    
    $q2 = "SELECT object_name, xpos, ypos FROM object WHERE proj_id='$proj_id' ORDER BY xpos ASC ,ypos ASC LIMIT 1"; // Left
    $result2 = mysql_query($q2) or trigger_error(mysql_error());
    $row2 = mysql_fetch_array($result2);
    $L_object = $row2['object_name'];
    $L_x = $row2['xpos'];
    $L_y = $row2['ypos'];
    $B_x = $L_x;
    $B_y = $L_y - 20;
    $iB_x = $B_x + 5;
    $iB_y = $B_y + 10;
    $length = ($R_x - $L_x)+ ($Lwidth / 10); 
    echo("<rect x=\"$B_x\" y=\"$B_y\" " . 
    
    		"width=\"$length\" height=\"20\" " . 
    
    		"style=\"fill:white; stroke:black; " . "stroke-width:2px\" />\n");
    		
    		echo("<text x=\"$iB_x\" y=\"$iB_y\" " . 
    
    		"style=\"file:black; font-size:10\">" . 
    
    		"Busbar" . 
    
    		"</text>\n");
    
    		
    // add busbase
    // calculate length of panel Left(smallest X, smallest Y) Right(largest X, smallest Y)+ length
    $q5 = "SELECT module_name, object_name, xpos, ypos FROM object WHERE proj_id='$proj_id' ORDER BY xpos ASC ,ypos DESC LIMIT 1"; // Right
    $result5 = mysql_query($q5) or trigger_error(mysql_error());
    $row5 = mysql_fetch_array($result5);
    $Bot_module = $row5['module_name'];
    $Bot_object = $row5['object_name'];
    $Bot_x = $row5['xpos'];
    $Bot_y = $row5['ypos'];
    
    //get bottom of lowest module in panel
    $w1 = "SELECT module.module_name, object_name, xpos, ypos FROM object, module WHERE object.module_name = module.module_name AND proj_id='$proj_id' ORDER BY height DESC LIMIT 1";
    $resultW = mysql_query($w1) or trigger_error(mysql_error());
    $rowW = mysql_fetch_array($resultW);
    $WWy = $rowW['ypos'];
    $WWx = $rowW['xpos'];
    $WWmodule = $rowW['module_name'];
    $WWobject = $rowW['object_name'];
     
    $q6 = "SELECT height FROM module WHERE module_name = '$WWmodule'";
    $result6 = mysql_query($q6) or trigger_error(mysql_error());
    $row6 = mysql_fetch_array($result6);
    $Lheight = $row6['height'];
    
    $Base_x = $Bot_x;
    $Base_y = $WWy + ($Lheight / 10);
    $BaseText_x = $Base_x + 5;
    $BaseText_y = $Base_y + 10;
     
    echo("<rect x=\"$Base_x\" y=\"$Base_y\" " . 
    
    		"width=\"$length\" height=\"10\" " . 
    
    		"style=\"fill:black; stroke:black; " . "stroke-width:2px\" />\n");
    		
    		echo("<text x=\"$BaseText_x\" y=\"$BaseText_y\" " . 
    
    		"style=\"file:black; font-size:10\">" . 
    
    		"Base" . 
    
    		"</text>\n");
    
    // y axis line	
    $line_height_x1 = $L_x - 20;
    $line_height_y1 = $L_y - 20;
    $line_height_x2 = $Base_x - 20;
    $line_height_y2 = $Base_y + 10;
    $line_height_y3 = $line_height_y2 + 10;
    $line_height_x3 = $line_height_x2 - 10;
    $actual_height = (($Base_y - $L_y)+30) * 10; 
    echo("<line x1=\"$line_height_x1\" y1=\"$line_height_y1\" " . 
    
    		"x2=\"$line_height_x2\" y2=\"$line_height_y2\" " . 
    
    		"style=\"stroke:red;" . "stroke-width:2px\" />\n");
    
    echo("<text x=\"$line_height_x3\" y=\"$line_height_y3\" " . 
    
    		"style=\"file:black; font-size:10\">" . 
    
    		"$actual_height mm" . 
    
    		"</text>\n");
    
    // x axis line
    $line_width_x1 = $B_x;
    $line_width_y1 = $B_y - 20;
    $line_width_x2 = $R_x + ($Lwidth / 10);
    $line_width_y2 = $R_y - 40;
    $line_length_x = $line_width_x2 + 10;
    $actual_length = ($line_width_x2 - $line_width_x1)* 10; 
    echo("<line x1=\"$line_width_x1\" y1=\"$line_width_y1\" " . 
    
    		"x2=\"$line_width_x2\" y2=\"$line_width_y2\" " . 
    
    		"style=\"stroke:red;" . "stroke-width:2px\" />\n");
    
    echo("<text x=\"$line_length_x\" y=\"$line_width_y2\" " . 
    
    		"style=\"file:black; font-size:10\">" . 
    
    		"$actual_length mm" . 
    
    		"</text>\n");
    
    echo '</g>';
    
    
    //panel side view
    echo '<g id="side">';
    //Get the deepest depth in the whole panel  SELECT * FROM table ORDER BY price desc"); 
    $kk1 = mysql_query("SELECT depth FROM object, module WHERE proj_id='$proj_id' AND object.module_name = module.module_name ORDER BY depth DESC") or die(mysql_error());
    $kk2 = mysql_fetch_assoc($kk1);
    $kk3 = $kk2['depth'];
    $sidewidth = $kk3/10;
    
    
    
    
    $side_height = ($actual_height / 10)-10;
    $side_y = $R_y - 20;
    $side_x = $R_x + ($Lwidth/10) + 160;
    $sidebase_x = $side_x;
    $sidebase_y = $side_y + $side_height;
    
    echo("<rect x=\"$side_x\" y=\"$side_y\" " . 
    
    		"width=\"$sidewidth\" height=\"$side_height\" " . 
    
    		"style=\"fill:white; stroke:black; " . "stroke-width:2px\" />\n");
    
    echo("<rect x=\"$sidebase_x\" y=\"$sidebase_y\" " . 
    
    		"width=\"$sidewidth\" height=\"10\" " . 
    
    		"style=\"fill:black; stroke:black; " . "stroke-width:2px\" />\n");
    		
    $side_xLine = $side_x - 20;
    $side_xText = $side_xLine - 10;
    //side_yLine
    echo("<line x1=\"$side_xLine\" y1=\"$line_height_y1\" " . 
    
    		"x2=\"$side_xLine\" y2=\"$line_height_y2\" " . 
    
    		"style=\"stroke:red;" . "stroke-width:2px\" />\n");
    
    echo("<text x=\"$side_xText\" y=\"$line_height_y3\" " . 
    
    		"style=\"file:black; font-size:10\">" . 
    
    		"$actual_height mm" . 
    
    		"</text>\n");
    //side xLine
    $side_xLine_y = $side_y - 20;
    $side_xLine_x_end = $side_x + $sidewidth;
    $side_xLine_x_end_text = $side_xLine_x_end + 10;
    
    echo("<line x1=\"$side_x\" y1=\"$side_xLine_y\" " . 
    
    		"x2=\"$side_xLine_x_end\" y2=\"$side_xLine_y\" " . 
    
    		"style=\"stroke:red;" . "stroke-width:2px\" />\n");
    echo("<text x=\"$side_xLine_x_end_text\" y=\"$side_xLine_y\" " . 
    
    		"style=\"file:black; font-size:10\">" . 
    
    		"$kk3 mm" . 
    
    		"</text>\n");
    //page titles
    $titleX = $line_width_x1 + ($length/2);
    $titleY = $side_xLine_y - 50;
    		echo("<text x=\"$titleX\" y=\"$titleY\" " . 
    
    		"style=\"file:black; font-size:14\">" . 
    
    		"FRONT VIEW" . 
    
    		"</text>\n");
    		
    		echo("<text x=\"$side_x\" y=\"$titleY\" " . 
    
    		"style=\"file:black; font-size:14\">" . 
    
    		"SIDE VIEW" . 
    
    		"</text>\n");
    
    echo '</g>';//panel side view end
    
    
    //TES details box
    $boxStartX = $side_xLine_x_end_text - 250;
    $boxStartY = $titleY + 500;
    $adX = $boxStartX + 5;
    $ad1Y = $boxStartY + 65;
    $ad2Y = $ad1Y + 9;
    $ad3Y = $ad2Y + 9;
    $ad4Y = $ad3Y + 9;
    $ad5Y = $ad4Y + 9;
    $ad6Y = $ad5Y + 9;
    $picX = $boxStartX + 2;
    $picY = $boxStartY + 4;
    
    
    echo("<rect x=\"$boxStartX\" y=\"$boxStartY\" " . 
    
    		"width=\"250px\" height=\"200px\" " . 
    
    		"style=\"fill:white; stroke:black; " . "stroke-width:1px\" />\n");
    
    echo("<rect x=\"$boxStartX\" y=\"$boxStartY\" " . 
    
    		"width=\"250px\" height=\"120px\" " . 
    
    		"style=\"fill:white; stroke:black; " . "stroke-width:1px\" />\n");
    echo("<rect x=\"$boxStartX\" y=\"$boxStartY\" " . 
    
    		"width=\"105px\" height=\"120px\" " . 
    
    		"style=\"fill:white; stroke:black; " . "stroke-width:1px\" />\n");
    echo("<image x='$picX' y='$picY' width='100px' height='55px' xlink:href='tes_logo_small.gif'/>");
    echo("<text x=\"$adX\" y=\"$ad1Y\" " . "style=\"file:black; font-size:8\">" . "18 Derryloaran Ind Estate" . "</text>\n");
    echo("<text x=\"$adX\" y=\"$ad2Y\" " . "style=\"file:black; font-size:8\">" . "Sandholes Rd, Cookstown" . "</text>\n");
    echo("<text x=\"$adX\" y=\"$ad3Y\" " . "style=\"file:black; font-size:8\">" . "Co. Tyrone" . "</text>\n");
    echo("<text x=\"$adX\" y=\"$ad4Y\" " . "style=\"file:black; font-size:8\">" . "Tel:	(028) 8676 1141" . "</text>\n");
    echo("<text x=\"$adX\" y=\"$ad5Y\" " . "style=\"file:black; font-size:8\">" . "Fax:	(028) 8676 9732" . "</text>\n");
    echo("<text x=\"$adX\" y=\"$ad6Y\" " . "style=\"file:black; font-size:8\">" . "Email: info@tes-ni.com" . "</text>\n");
    
    $boxTextX = $boxStartX + 110;
    $boxText1Y = $boxStartY + 15;
    $boxText2Y = $boxText1Y + 15;
    $boxText3Y = $boxText2Y + 15;
    $boxText4Y = $boxText3Y + 15;
    
    echo("<text x=\"$boxTextX\" y=\"$boxText1Y\" " . 
    
    		"style=\"file:black; font-size:8\">" . 
    
    		"CLIENT: $company" . 
    
    		"</text>\n");
    echo("<text x=\"$boxTextX\" y=\"$boxText2Y\" " . 
    
    		"style=\"file:black; font-size:8\">" . 
    
    		"PROJECT:" . 
    
    		"</text>\n");
    		
    echo("<text x=\"$boxTextX\" y=\"$boxText3Y\" " . 
    
    		"style=\"file:black; font-size:8\">" . 
    
    		"$project" . 
    
    		"</text>\n");
    
    echo("<text x=\"$boxTextX\" y=\"$boxText4Y\" " . 
    
    		"style=\"file:black; font-size:8\">" . 
    
    		"DATE: $today" . 
    
    		"</text>\n");
    		
    //panel details
    $det1Y = $boxStartY + 130;
    $det2Y = $det1Y + 15;
    $det3Y = $det2Y + 15;
    $det4Y = $det3Y + 15;
    $det5Y = $det4Y + 15;
    
    echo("<text x=\"$adX\" y=\"$det1Y\" " . 
    
    		"style=\"file:black; font-size:8\">" . 
    
    		"FORM: $form" . 
    
    		"</text>\n");
    		
    echo("<text x=\"$adX\" y=\"$det2Y\" " . 
    
    		"style=\"file:black; font-size:8\">" . 
    
    		"FAULT RATING: $fault" . 
    
    		"</text>\n");
    		
    echo("<text x=\"$adX\" y=\"$det3Y\" " . 
    
    		"style=\"file:black; font-size:8\">" . 
    
    		"IP RATING: $ip" . 
    
    		"</text>\n");
    		
    echo("<text x=\"$adX\" y=\"$det4Y\" " . 
    
    		"style=\"file:black; font-size:8\">" . 
    
    		"ACCESS: $access" . 
    
    		"</text>\n");
    		
    echo("<text x=\"$adX\" y=\"$det5Y\" " . 
    
    		"style=\"file:black; font-size:8\">" . 
    
    		"CABLE ENTRY: $entry" . 
    
    		"</text>\n");
    	
    
    //table with descriptions
    $rCount = 0;
    $keyX1 = $line_height_x1;
    $keyY = $boxStartY;
    $table = mysql_query("SELECT module_name, object_name FROM object WHERE proj_id = '$proj_id' ORDER BY object_name") or die();
    while($tt = mysql_fetch_array($table)) {
    	$objtt = $tt['object_name'];
    	$modtt = $tt['module_name'];
    	$rCount = $rCount+1;
    	echo("<text x=\"$keyX1\" y=\"$keyY\" " . 
    
    		"style=\"file:black; font-size:10\">" . 
    
    		"$objtt = $modtt" . 
    
    		"</text>\n");
    		
    	
    	if ($rCount == 20) {
    		$keyY = $boxStartY;
    		$keyX1 = $keyX1 + 240;
    		$rCount = 0;
    	} else {
    		$keyY = $keyY + 10;
    	}
    }
    
    
    //end SVG document 
    echo('</svg>' . "\n");	
    
    ?>
    Last edited by Dormilich; Oct 18 '09, 05:54 PM. Reason: fixed [code] tags
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    You could always just use the Output Control functions.

    Like, for example:
    [code=php]
    <?php
    ob_start();

    echo "<html><head><t itle>Test</title></head>";
    echo "<body><h1> It works!</h1></body></html>";

    $content = ob_get_clean();

    if(file_put_con tents("test.htm l", $content)) {
    echo "Success";
    }
    else {
    echo "Failed to save file";
    }
    ?>
    [/code]

    Comment

    • amzadkhan
      New Member
      • Sep 2018
      • 1

      #3
      Hi friends,

      I have updated below code and its working 100%

      ob_start();
      echo '<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
      <svg xmlns="http://www.w3.org/2000/svg"
      xmlns:xlink="ht tp://www.w3.org/1999/xlink"
      version="1.1"
      baseProfile="fu ll"
      width="100" height="100">
      <circle cx="50" cy="50" r="40" stroke="green" stroke-width="4" fill="yellow" />
      </svg>';

      $content = ob_get_clean();

      if(file_put_con tents("Amzad.sv g", $content)) { // Filename for storing purpose
      echo "Success";
      }
      else {
      echo "Failed to save file";
      }


      If you have any query, please let me know.

      Comment

      Working...