Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generating SVG files using PHP

Tags:

html

php

svg

So, I'm attempting to create some tag images for my users, showing the total points that they have earned. I am attempting to write the SVG in a php file which will add in dynamic content, such as the user's points. I'd like to use a HTML <img> tag to fetch the SVG from a file image_gen.php.

How would I display the SVG through the <img> tag?

Thanks!

like image 929
Ryan Avatar asked Jan 10 '17 19:01

Ryan


People also ask

How to create SVG in PHP?

Echo the SVG with PHP php”. This adjustment instructs the server to process the file as a PHP file. Second, add the Content-type: image/svg+xml HTTP header to the generated document. This second change tells the browser to display the document as an SVG.

What is SVG in PHP?

SVG stands for Scalable Vector Graphics. SVG defines vector-based graphics in XML format.

How to echo SVG in PHP?

php $svg_file = file_get_contents('http://example.com/logo.svg'); $find_string = '<svg'; $position = strpos($svg_file, $find_string); $svg_file_new = substr($svg_file, $position); echo "<div style='width:100%; height:100%;' >" .

Are SVG vector files?

What is an SVG file? Scalable Vector Graphics (SVG) is a web-friendly vector file format. As opposed to pixel-based raster files like JPEGs, vector files store images via mathematical formulas based on points and lines on a grid.


1 Answers

Using the following PHP code, I set the content type of the file to svg+xml:

<?php
    header('Content-Type: image/svg+xml');
?>
like image 154
Ryan Avatar answered Oct 25 '22 08:10

Ryan