-2

I want to integrate a logo (.png) in a another image(.svg) file. I would like to know is it possible using php?

I am using this , but this is not working .

<image x="0" y="0" width="10" height="10" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="https://bellard.org/bpg/2.png" onclick="this.setAttributeNS('http://www.w3.org/1999/xlink', 'href', 'http://pngimg.com/uploads/polar_bear/polar_bear_PNG23514.png')">
  </image>

I have converted a .osm file into .svg and now I have to integrate a logo inside that .svg file when I enter an image tag manually inside that SVG file that is showing on chrome but not reflected on image viewer. So is there any way or documentation for this?

Thank you for your help.

  • 1
    If it's possible to embed a png logo inside an svg file in general (I don't know if it is, off the top of my head), then it will be possible to do so using PHP (or most other programming languages), yes. Whether there is an existing function to do so, or whether you will need to write your own, is a topic for you to research. What have you found out so far? What have you tried? Where are you stuck? We're not here to do searching on your behalf, but we'll help you with a more specific question or problem about your code, once you have started to write some. – ADyson Jan 16 '20 at 10:07
  • 1
    See https://stackoverflow.com/questions/6249664/does-svg-support-embedding-of-bitmap-images for the embedding PNG into SVG part. As for doing it “dynamically” using PHP - since SVG is XML, you might want to use DOMDocument for that kind of manipulation. – 04FS Jan 16 '20 at 10:15
  • actually, I have converted a .osm file into .svg and now I have to embed a logo inside that .svg file when I enter an image tag manually inside that SVG file that is showing on chrome but not reflected on image viewer. So is there any way or documentation for this? – sagar sonar Jan 16 '20 at 10:22
  • 1
    _How_ exactly did you embed the image then? If you use an HTTP URL, your image viewer might simply not support that perhaps? Try the inline version then. – 04FS Jan 16 '20 at 10:38
  • To start to understand why you have a problem with your approach, we would need to see some actual code and data. We can't fix abstract descriptions of a problem - they are useful, but can only give us a general impression, not the specific details which are needed to reproduce and fix the issue. – ADyson Jan 16 '20 at 11:00
  • Thnakyou all of you. I got the solution. There is added a base 64 image in href and it also showing in image viewer. – sagar sonar Jan 16 '20 at 11:52

1 Answers1

0

public function imageEmmbed(Request $request){

$file = $request->file('file');
$svg_data = file_get_contents($file);
$svg = new \SimpleXMLElement($svg_data, LIBXML_NOEMPTYTAG);
$svg->registerXPathNamespace('svg', 'http://www.w3.org/2000/svg');

$g_res= $svg->addChild("text", "COPYRIGHT @ 2020");
$g_res->addAttribute("x", "150");
$g_res->addAttribute("y", "220");
$g_res->addAttribute("font-family", "Verdana");
$g_res->addAttribute("font-size", "10");
$g_res->addAttribute("fill", "red");

$g_res = $svg->addChild("image","1");
$g_res->addAttribute("x", "0");
$g_res->addAttribute("y", "0");
$g_res->addAttribute("width", "50");
$g_res->addAttribute("height", "50");
$g_res->addAttribute("xlink", 'http://www.w3.org/1999/xlink');
$g_res['xlink:href'] = "enter your base64 image here.";
$g_res->addAttribute("onclick", "this.setAttributeNS('http://www.w3.org/1999/xlink')");

$dom_res = dom_import_simplexml($g_res);
$dom_svg = dom_import_simplexml($svg);
$dom_svg->formatOutput = true;
$dom_svg->appendChild($dom_res);

echo $svg->saveXML(public_path('image/test.svg'));

}