wordpress - Best Approach To Load SVG as Inline in PHP - cURL or file_get_contents
Get the solution ↓↓↓I'm writing this question after reading almost all similar questions on StackOverflow and other places, but still, couldn't get the answer I was looking for.
The Situation
I'm building a WordPress site where I have mostly used SVG illustrations when it comes to adding images. Now instead of using<ing ... />
tag as I was using SVG I preferred to inline them all on the page. So, to achieve that I've wrote a smallcUURL
(why cURL will be explained below) function in myfunctions.php
file as follows:
/**
* Uses cURL rather than to make a request to the specified URL.
*
* @param string $url The URL to which we're making the request.
* @return string $output The result of the request.
*/
function url_get_contents( $url ) {
if ( ! function_exists( 'curl_init' ) ) {
die( 'The cURL library is not installed.' );
}
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$output = curl_exec( $ch );
curl_close( $ch );
return $output;
}
This works fine and returns my SVG file content and then I can simply doecho url_get_contents('htttps://mysite.com/something/something/some.svg');
I have no issue with making the code work.
The Problem & Why Notfile_get_contents()
The main reason I initially didn't usefile_get_contents
is that I read in multiple StackOverflow questions that it is bad, slow and has security issues. No matter what questions I read all said the same thingcURL
is way faster and better.
But my experience is not the same. When I made the site live, I started to notice some wired thing; i.e. a very high wait time especially on the initial requests. Also each time I am refreshing a page, a lot ofphp-fpm
processes gets created on the server and consumes around 20-25% of CPU.
For example just to load the home page there were about 4-7php-fpm
instances getting created and eating resource. My host told me that this is not normal and there is something wrong. So, when I replaced mycURL
basedurl_get_contents()
, still multiplephp-fpm
processes were getting created but the page was loading a bit faster and the wait time was about 2-3 sec lower.
The Solution
I have no idea why this is happening so, if anyone can explain to me why this is happening and what is the best way to inline SVG files inside your pages, I would be really grateful. Should I usecURL
or simplyfile_get_contents()
? There is some page which loads as much as 8-10 SVG and then there are also pages which only loads 2-3 SVG as inline.
Looking forward to your valuable guidance.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: installation failed, reverting ./composer.json and ./composer.lock to their original content
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.