PHP include svg assets in cake php - php

Does cake have a way to php include svg assets? I know how to use helpers to create an <img> tag pointing to the SVG for the img's src attribute, but I'd like to actually include the file rather than reference it within an <img> tag.

No, CakePHP doesn't ship with such functionality, you'll have to come up with something on your own, or use one of the many PHP based SVG inliners out there, it should be easy enough to wrap that in a custom helper.
If you just need to embed the file, then you could even stick to simply reading and outputting the file contents with the XML declaration and doctype removed, something like:
$svg = file_get_contents($path);
$svg = preg_replace('/^<\?xml.*?\?>\s*(<!DOCTYPE.*?>\s*)?/is', '', $svg);

In the end, this was a silly question. You can of course just use <?php include 'img/thefile.svg' ?> assuming your svg is in webroot/img folder. If the svg or file is not in a publicly accessible folder I would look to creating a custom helper as another post suggested.

Related

Use PHP to prep SVG file for use in img tag data uri

I'm using PHP. I would like to use file_get_contents to get an .svg file, and convert it for use as a data uri in an image tag. Something along these lines:
Controller:
$mylogo = file_get_contents(FCPATH.'app/views/emails/images/mylogo.svg');
View:
<img src="data:image/svg+xml;utf8,<?= $mylogo ?>">
I need to convert it into something (base64?) as right now it is just dumping it in tags and all and though the image does appear, it makes a mess of the img tag surrounding it.
<svg> elements can be echoed directly onto a web page like any other element; there's no need to include it as an img src attribute. PHP's include can be used for this (ie. include('/path/to/image.svg')), amongst a myriad of other methods.
Alternatively, if for some reason you need to include the svg as an actual img tag, there's no need for file_get_contents or similar functions; an SVG can be linked as a source path like any other image type (ie. <image src="/path/to/image.svg">).
Solution
Controller
$encodedSVG = \rawurlencode(\str_replace(["\r", "\n"], ' ', \file_get_contents($path)));
View
<img src="data:image/svg+xml;utf8,<?= $encodedSVG ?>">
⚠️ Do not convert to base64
It's less efficient in CPU time and also makes longer requests in size!
Know more about why

What is a good way to change/replace elements in a php+css document?

Forgive me for possibly misusing certain terminology. I would like to:
open a html/php file, with php
find elements with certain class
change its innerHtml basically
than save the file.
I have a feeling DOMElement in php could help, since I've used it for similar things in javascript, but I am (still) unsure of its function in PHP and php.net sais a DOM document "Represents an entire HTML or XML document" (so no php/javascript containing document).
So: which function(s), libraries should I study to best perform those operations?
Possibly: Aside from php.net do you perhaps have a good tutorial for ^that^ solution?
Edit: Possibly related: Manipulate HTML from php
Edit2: If I would build something with my knowledge a.t.m. it would probably be almost as violent as solutions I tried earlier: https://stackoverflow.com/a/8960363/574700
There will be php in the document.
There will be some inline css and javascript in there.
1.open a html/php file, with php
Include htmlsimpledom and open you php file.
$html = file_get_html('myfile.html');
2.find elements with certain class
foreach($html -> find('.class-name') as $element)
$element - > plaintext. '<br>';
3.change its innerHtml basically
$html -> find('.class-name') -> innertext = 'text-here';
4.then save the file.
file_put_contents($filename, $html);

PHP: Get all CSS files of an HTML web page

I'm trying to get all CSS files of an html file from URL.
I know that if I want to get the HTML code it is easy - just using PHP function - file_get_contents.
The question is - if I could search easily inside an a URL of HTML and get from there the files or content of all related CSS files?
Note - I want to build an engine for getting a lot of CSS files, this is why just reading the source is not enough..
Thanks,
You could try using http://simplehtmldom.sourceforge.net/ for HTML parsing.
require_once 'SimpleHtmlDom/simple_html_dom.php';
$url = 'www.website-to-scan.com';
$website = file_get_html($url);
// You might need to tweak the selector based on the website you are scanning
// Example: some websites don't set the rel attribute
// others might use less instead of css
//
// Some other options:
// link[href] - Any link with a href attribute (might get favicons and other resources but should catch all the css files)
// link[href="*.css*"] - Might miss files that aren't .css extension but return valid css (e.g.: .less, .php, etc)
// link[type="text/css"] - Might miss stylesheets without this attribute set
foreach ($website->find('link[rel="stylesheet"]') as $stylesheet)
{
$stylesheet_url = $stylesheet->href;
// Do something with the URL
}
You need to parse the HTML tags looking for CSS files. You can do it for example with preg_match - looking for matching regex.
Regex which would find such files might be like this:
\<link .+href="\..+css.+"\>

Render HTML pages from private folder in PHP

I have a web site that have HTML pages stored in a private folder. I want a PHP script that can read the HTML file then push it to the browser.
My tought was to get the html file with the file() function in PHP. Then echo() it to the browser. That works for the html content of the page. The images and the css does not follow however.
I heard of a "render" function in IIS or ASP that render the HTML content of a web page in a private folder then send the images in a binary format. Does PHP have something similar?
Currently I read the file as follow :
$htmlFile = file(PATHTOFILE);
echo(implode('',$htmlFile));
The reason we are trying to do that is to protect the url / information of the pages contained in this folder. The user will have to connect to the web service, then the PHP script will push the html pages
You can use the tag base to solve the problem of the relative path of the files, something like this:
$html = file_get_contents($url);
$html = str_replace('<head>', '<head><base href="FULL PATH OF DIR" />', $html);
echo $html;
CSS and images are not displayed because their paths in the HTML files is relative to HTML files, right? And if you have these CSS and images in the same private folder, how can you hope the user will fetch them?
Indirect, you should fetch CSS and images the same way you do with HTML. But this means you have to replace all paths in your displayed HTML, that is quite absurd. In fact, we are talking about some kind of proxy now... ?!?!?
Why you need it?
Anyway echo(file_get_contents($htmlFile)); is less stressful.
Another option if it is an <img /> tag and the image is also stored outside of the root you can just make the src= attribute as so:
src="get_image.php?file=thisfile.png" // add a $_GET if needed to distinguish files
then get_image.php:
$file = $_GET['file'];
// security checks if you wish
header(sprintf("Content-type: %s;",'image/png'));
readfile($file);
exit;

Is there a way to use css sprites inside dynamic contents?

I mean, using css sprites on non static content like: Inside while(mysql_fetch_array())...
You can use a CSS sprite anywhere you are writing HTML code... As far as "inside while..."... i have no idea what you mean by that. If your while loop prints HTML, then yes, you can use a sprite.
your code looks like PHP. That is processed on the server to generate static HTML content. It is not executed dynamically in the browser like JavaScript. You can have have the element classes and id attributes echoed out to whatever content your'e generating in PHP and then reference those from your CSS.

Categories