Using the FileHelper to place inline SVG from the FileManager...

Permalink
So I've got an Image Page Attribute, like thumbnail is, for an SVG image.
But rather than place it as an <img />, I want to inline the SVG code right into the page's HTML. From there, the sky's the limit for what you can do with it, but for now, I just want the .svg file's contents placed into the HTML.

I've got it working in a PageList block like so:
$fh = Loader::helper('file');
foreach ($pages as $page):
            $amp = $page->getAttribute('custom_ampersand');
      $ampPath = $amp->getVersion()->getRelativePath();
      if(!empty($ampPath) || $ampPath !== "") {   
              $ampTitle = $amp->getVersion()->getTitle();
         $ampDescription = $amp->getDescription();
       #v1-Works: 
         $ampSVG = file_get_contents(BASE_URL.$ampPath);//This allows us to embed the SVG content right into the HTML, and thus use CSS styling!
      #v2: Doesn't Work? c5 Method:
         //$amp = fopen($ampPath,'r');//r means read only
             //$ampSVG = $fh->getContents($amp, $timeout = 5);
      }

This works great! The SVG code is placed in the HTML and it's great. Not sure why the c5 method didn't, but file_get_contents does work.

However, in other situations, it's not working for some reason?
Specifically, in the case of a blog post or a custom page type.
So in the case of a page type, I have this code:
$fh = Loader::helper('file');
      $amp = $c->getAttribute('custom_ampersand');
       if (is_object($amp)){
          $ampExt = $amp->getExtension();
          $amp_src = $amp->getVersion()->getRelativePath();
          $ampTitle = $amp->getVersion()->getTitle();
          }
          if($c->getAttribute('custom_ampersand')) {
             if ($ampExt === svg){
                //Open SVG file to get and get contents
                   echo '<!--Got an SVG over here! -->';
                     $ampFile = fopen($amp_src,'r');
                               $ampSVGcode = $fh->getContents($amp_src, $timeout = 15); 
                     #echo '<img src="'.$amp_src.'" alt="'.$ampTitle.'" />';
                 } else {

Neither $ampFile or $ampSVGcode works.
When I var_dump $ampSVGcode, it returns bool(false)?
I'm quite lost and not sure what I'm doing wrong.
Interestingly, the commented out: echo '<img src="'.$amp_src… works great, but I really want the straight, inline SVG code instead.
Interesting example. Treating an image/graphic as a file to get it's contents and display as an image.
I'm hoping someone can steer me in the right direction to get this working where needed, not just page list.
Thanks in advance!

PatrickHenry
View Replies: View Best Answer
JohntheFish replied on at Permalink Best Answer Reply
JohntheFish
I think the reason why one works and the other does not is that you are building a URL for the svg file, not an absolute filesystem path. In some cases, the URL coincides with a relative file system path, in others, it doers not.

Try using
$ampPath = $amp->getVersion()->getPath();
PatrickHenry replied on at Permalink Reply
PatrickHenry
Thanks John,
Switched $amp_src to $ampPath as you recommended, and it gives this error:
fopen(): Filename cannot be empty in /path/to/c5/page_types/my_page_type_overide.php on line 62


When I var_dump($ampPath), everything looks as it should I think?
string(75) "/path/to/c5/files/6814/2385/1631/255-KaitlynMcNabney-2015.svg"


Seems like I'm missing something obvious but can't put my finger on it.
Any ideas?
Thanks for your time and insights, as always!
PatrickHenry replied on at Permalink Reply
PatrickHenry
Bingo.
Got it. I'd turned on the PHP fopen(), which was redundant b/c I had the file helper method doing that right after.
Just had to echo out the $ampSVGcode.
Works like a charm.
You're my hero, again!
Thanks for your help!
MrKDilkington replied on at Permalink Reply
MrKDilkington
@PatrickHenry

This is an interesting post.

I am assuming that you are using 5.6, correct?

In 5.7, I found two ways that worked:
$file->getFileContents()

and
file_get_contents($file->getURL())

@JohntheFish

I have never been quite sure about what adding getVersion() does. Omitting it works, but I wonder if I am missing something important.
PatrickHenry replied on at Permalink Reply
PatrickHenry
Hey MrKDilkington!
Thanks for seeing the potential here!
I was somewhat shocked someone else hadn't thought of it/posted about it.
I am using 5.6.3.3, and this works like a charm.
There's so much potential with inline SVG, so I'm really excited to build upon this.
Main thing was getting it inline tho, and this is the ticket.
I've begun working 1 site in c5.7. Glad to know this is already built in.
Cheers!
JohntheFish replied on at Permalink Reply
JohntheFish
If you don't get version, the currently approved version is the default. I think the methods available in 5.7 without getting the version may have increased vs 5.6.

For reading a file, $file->getFileContents() is required for anything going through the PRB. Otherwise its pretty much a matter of what is suited to your server and personal preferences.
drago2308 replied on at Permalink Reply
For those who need it,

The following no longer works in 5.8 (currently RC2)

$SVGcode = file_get_contents($svgFile->getURL());


You will need to use the other stated above in MrKDinkington's post
eg.
$SVGcode = $svgFile->getFileContents();


This works without a hitch.