-1

thank you for responding ;

I have image data in $snap , the content of $snap is like this :

data:image/jpeg;base64,/9j/4AAA... etc

I need that my php file return $snap as image file ; when the example-image.php oppend it shoud be like example.jpg

this is the script I want to respond to it :

jQuery.get(domainPath+'&getImage&site='+inputHost,function(data){
     $("#screenshotData").html('<img src="data:image/jpeg;base64,'+data+'"/>');
});

The script above will use my respond to make an img html tag , I can't change the script above ; my respond shoud make the script above applicable .

  • use `base64_decode($snap);` and then save that in a file like `file.jpg` – a55 Sep 16 '21 at 21:31
  • Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking. – Community Sep 24 '21 at 13:57

1 Answers1

0

Since your JavaScript code is expecting base64 content, you should be able to just echo it from your PHP script--after removing the part that you don't need.

<?php

$snap = 'data:image/jpeg;base64,/9j/4AAA...';

// remove the prefix 'data:image/jpeg;base64,' because JavaScript is prepending this
$data = str_replace('data:image/jpeg;base64,', '', $snap);

// output the data
echo $data;
Anony Mous
  • 89
  • 5
  • Why even bother replacing it in the data, why not just remove the `data:image/jpeg;base64,` from the JavaScript? If we are delivering images as base64 already, which will bloat the data to roughly about 4/3 of its original binary size, then this is probably not the place where a handful of bytes desperately need saving now in transport. – CBroe Sep 17 '21 at 07:13
  • @CBroe, because the OP stated "I can't change the script above". Which I think means that the JavaScript cannot be adjusted. – Anony Mous Sep 17 '21 at 15:47