使用PHP准备img标签数据uri中使用的SVG文件 [英] Use PHP to prep SVG file for use in img tag data uri

查看:40
本文介绍了使用PHP准备img标签数据uri中使用的SVG文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PHP.我想使用file_get_contents来获取一个.svg文件,并将其转换为图像标签中的数据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:

控制器:

$mylogo   = file_get_contents(FCPATH.'app/views/emails/images/mylogo.svg');

查看:

<img src="data:image/svg+xml;utf8,<?= $mylogo ?>">

我需要将其转换为某种东西(base64?),因为现在它只是将其转储到标签中,并且所有图像都出现了,尽管它确实使周围的img标签变得一团糟.

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> 元素可以像其他元素一样直接回显到网页上;无需将其包含为 img src 属性.PHP的 include 可以用于此目的(即 include('/path/to/image.svg')),还有许多其他方法.

<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.

或者,如果出于某些原因,您需要 svg 包含为实际的 img 标签,则无需 file_get_contents或类似功能;SVG可以像任何其他图像类型一样链接为源路径(即< image src ="/path/to/image.svg"> ).

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">).

这篇关于使用PHP准备img标签数据uri中使用的SVG文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆