Getting data from an SVG file using PHP - php

I'm not sure if this is a duplicate question or not but I'll ask it anyway.
How do I take image data from an SVG file and pass it into PHP, then output the info with HTML? I know it can be done in Javascript, but that's not how this particular project needs to be done.

you can use file_get_contents to read svg file data in php.
$templateString= array(
'front' => file_get_contents($svgUrl, FILE_USE_INCLUDE_PATH)
);
echo $templateString;
Now complete svg data is in $templateString variable.

This is what I did and got my SVG XML content from URL.
$svg_file = file_get_contents($url);
echo $svg_file;

Related

Programmatically (PHP) save image with no extension

I am trying to save to disk an image that is served to me via a JSON result. The returned JSON result property that I am interested in is this:
https://i.scdn.co/image/6cd03f58ddf30a1393f06d6469973ba16ac908df
Which is the correct image. The problem is that, while the above URL does display the image, it does not allow me to download it, yet I can download it by right-clicking on it.
What I need to be able to do is, using my PHP code, save it to disk.
I have no issues saving results from other sites that give results that link to a direct image extension (.jpg, .gif or .png). But I have not been able to figure out how to programmatically download the image from the above URL.
Is it possible?
This is the code that I use, which works correctly on results that give a URL that has a correct image extension. The URL returned is loaded into the $largeimg variable.
$input = $largeimg;
$output = 'image.jpg';
file_put_contents($output, file_get_contents($input));
How do I achieve this?
file_get_contents() is able to accept raw URI arguments. Your code works perfectly for me, if modified in the way:
$input = 'https://i.scdn.co/image/6cd03f58ddf30a1393f06d6469973ba16ac908df';
So, file_get_contents() can download the image directly. I think, the problem is your $largeimg variable.

Output XML file, without saving

I want to be able to create a XML File, add nodes and such to it, then output it to the screen without saving.
$bookxml = new DOMDocument('1.0', 'utf-8');
is how i have the XML file created, however i just can't anyway to display the XML file on the screen without saving it.
However i am having a problem with outputting even in save, this is the line i have
echo $bookxml->save("testing.xml");
All this does is return the file size of the newly created XML, and not the contents.
Any help would be awesome, i'm completely stumped on this.
What you're looking for is saveXML, additionally you can use htmlspecialchars to encode the xml so you can see it in your browser display.
echo htmlspecialchars($bookxml->saveXML());
You want the saveXML method, not the save method:
http://us3.php.net/manual/en/domdocument.savexml.php

convert php file to pdf using mpdf

I can convert html page to pdf and send it via email with out any problem, but I am facing trouble with converting php file to pdf.
is it possible to convert php file to pdf using mpdf or do I need to use some other php class for this?
Thanks!
The option seems to be like, first convert the output of the php file to html file, save it and pass that file to mpdf.
Thought my solution may seem other way round but it worked well for me.
Or otherwise this Link
<?php
$file = '/home/user/Desktop/myfile.html';
$result = file_get_contents("url/of/ur/page");
echo $result; //view source now
file_put_contents($file, $result);
?>
now u can pass this file to mpdf. Realie sorie bt i havnt used mpdf till date. May be this solution works for you.
Also, other option is curl.

Outputting an image from the same PHP file which also receives & sends JSON

Ok, I'm hoping I can explain my situation rather than pasting lines and lines of code.
Currently, JSON sends positional info to my PHP file which in turn uses this data to generate an image, saves it and returns the filename via JSON back to browser. Javascript then refreshes the image on screen.
This all works fine at the moment, but I am wanting to optimise the process and look at the possibility of outputting the image file straight after it's created then save afterwards.
My ideal solution would be something like:
header('Content-Type: image/gif');
echo $this->canvas;
// Save user file
$this->canvas->writeImage( $this->userFile = 'user_img.gif' );
$this->canvas->destroy();
// encode everything and send to browser
echo json_encode(array('misc data back to the browser'));
(I still need to send data back to browser via JSON)
And in my HTML I would have the image laid out like this:
<img src='json-processing-script.php' />
But as usual nothing is ever that simple, so I'd like to hear if anyone can make any pointers.
In your example, the json would be added to the gif, messing up your image. If you want to return these two completely different things from your php script, you would have to encode the image, add it to the json and extract it in the javascript to get the source of your image.
See for example this question.

How to get variable from php file to actionscript?

I will write the text that getting from php file in adobe flash . how can I?
let your php-script do the output in form of a xml, which should be easy to load and processed with actionscript.

Categories