How to play blob audio in html audio player using PHP? - php

I am saving blob audio in the mysql database as a blob. the format of the audio file is like this
blob:abc.com/4b67d486-61e9-4db7-afbe-b693a34ea758
Its saving in the database blob format like this
And when I fetch it using php its show in html like this
<audio src="blob:abc.com/4b67d486-61e9-4db7-afbe-b693a34ea758" controls="" id="audio-playback" class=""></audio>
But the problem is audio is not playing when I try to play it
I am missing something can anybody help how to play blob audio using php

Did you encode the blob?
This can work:
$audio_data = base64_encode($blob);
echo '<audio src="data:audio/mpeg;base64,'.$audio_data.'" controls="controls" id="audio-playback" class=""></audio>';
EDIT: Wav Format
If the audio file is in WAV format, you need to make sure that the audio tag in the HTML is also specified to be of the same type with type="audio/wav":
<audio src="blob:abc.com/4b67d486-61e9-4db7-afbe-b693a34ea758" type="audio/wav" controls="" id="audio-playback" class=""></audio>
Also, make sure that the server from which the audio is being served from is able to serve WAV files.

Related

I want to convert a video link to an m3u8 file

I want to convert a video link to an m3u8 file That means I can give a video for the input of the link and I can watch the same video with the extension m3u8 for the output.
Please help me with this.
You should use ffpmeg and ffprobe for video converting / procesing.
try this library PHP-FFmpeg-video-streaming

Change image size to Save on PHP server

I have a form in my Android app that send information to php server with an image pick button. I want to resize image before saving on server with php codes :
<?php
move_uploaded_file($_FILES['file']['tmp_name'],'uploads/'.$_FILES['file']
['name']);
$orgfile='uploads/'.$_FILES['file']['name'];
list($width,$height)=getimagesize($orgfile);
$newfile=imagecreatefromjpeg($orgfile);
$thumb='uploads/a/'.$_FILES['file']['name'];
$truecolor=imagecreatetruecolor(600,400);
imagecopyresampled($truecolor,$newfile,0,0,0,0,600,400,$width,$height);
imagejpeg($truecolor,$thumb,100);
unlink($orgfile);
?>
This code just resize jpeg images and another formats (png or gif and even jpg) saved a black image.
It is necessary to mention that name of image file changed to a random number like "32165465423" and I don't know the image format to use "imagecreatefrompng" or "imagecreatefromgif" in my php file.
I want a code like "imagecreatefromall" or another ...
Thanks guys(sorry for bad English)
You will have to detect the type of image, based on that you can run the function. See the one cool php library for reference
https://github.com/eventviva/php-image-resize/blob/master/lib/ImageResize.php#L77

PHP - Possible to take a base64 encoded pdf data string and compress it?

So I have an XML file that has a base64 encoded data string for a pdf file, which just has an image taken from an iPad.
This pdf file can be excessively large, as much as 14MB with dimensions of 57"x38".
These images are taken from an iPad through a DocuSign session, thus I have no way at the moment of controlling their size or format before they get to my php listener script.
However, my script cannot work with such large files as my CRM's API file size max is 10MB, and I need a way of reducing the file size before I can upload it through my CRM's API.
Now if it was just a jpg, it would be ok as there are plenty of ways to reduce file size in PHP, but it is a PDF. I have found plenty of PHP extensions for making PDFs, but I haven't found any for reading a PDF and extracting an image from it.
So is there a way to extract the image from the PDF through PHP, or perhaps compress the pdf file?
UPDATE
I didn't think about the possibility of converting a pdf into a jpg, which apparently is easier to do with imagick. Having my server admin install it and I will see if I can make it work with my script.
UPDATE 2
So I was able to get imagick working and locally I am able to convert pdf files into jpg, and reduce file size dramatically.
However, I am running into an issue using it with my application. I get the following error from my CRM's API:
Failed to parse XML-RPC request: Invalid byte 1 of 1-byte UTF-8 sequence.
So the process is the following:
XML file has a base64 encoded data stream of the pdf file.
I decode this data
I then convert with imagick and reduce file size
I base64 encode and prep for upload
CODE
$imageBlob = base64_decode((string)$pdf->PDFBytes);
$imagick.$x = new Imagick();
$imagick.$x->readImageBlob($imageBlob);
$imagick.$x->setImageFormat('jpeg');
$imagick.$x->setImageCompressionQuality(60);
$imagick.$x->adaptiveResizeImage(1024,768,true);
$imageBlob = $imagick.$x->getImageBlob();
$PDFdata[] = base64_encode($imageBlob);
I can test the date by using the proper header and I can see the new jpeg fine, so I assume the data is properly formatted.
What I am missing?
Ok, so I figured it out.
Imagick was the way to go, and my use of it was good. I just goofed up on the file name because I wasn't using a proper dynamic variable name. Code should have looked like this:
CODE
$imageBlob = base64_decode((string)$pdf->PDFBytes);
${'imagick'.$x} = new Imagick();
${'imagick'.$x}->readImageBlob($imageBlob);
${'imagick'.$x}->setImageFormat('jpeg');
${'imagick'.$x}->setImageCompressionQuality(60);
${'imagick'.$x}->adaptiveResizeImage(1024,768,true);
$imageBlob = ${'imagick'.$x}->getImageBlob();
$PDFdata[] = base64_encode($imageBlob);
$PDFfile[] = $FormCustomField . $x . '.jpg';
So the error I was getting was because of an invalid file name, because the $x variable in the previous code was getting junk values. Now everything works fine.

How to add HTML 5 audio player?

I'm getting source file from a folder. When I select a ready-made mp3 file like song as src in audio tag, It works. But when I select a mp3 file(call recording file) created from base64 encoded string, I doesn't work. File is perfectly created as I checked in folder and played. Problem is that when I move curser on player, it becomes transparent just like an image. If anyone know answer then please explain with an example. Thank You. Here is my code.
<?php
$data = $_REQUEST['data'];
$filename = $_REQUEST['filename'] . ".3gpp";
$imei = $_REQUEST['imei'];
$dir = __DIR__ . "/recordings/";
$file = file_put_contents($dir . $filename, base64_decode($data));
$rloc = "recordings/" . $filename;
$str = "INSERT INTO recording(data, filename, imei) VALUES('$rloc', '$filename', '$imei')";
$qry = mysql_query($str);
?>
<html>
<audio controls autoplay="">
<source src="<?php echo $row['data']; ?>">
</audio>
</html>
You need to prefix the data:audio/mp3;base64, to the Data URI. Use this instead:
<audio controls autoplay="">
<source src="data:audio/mp3;base64,<?php echo $row['data']; ?>">
</audio>
I found this short example:
<audio controls autoplay>
<source src="horse.ogg" type="audio/ogg">
<source src="horse.mp3" type="audio/mpeg">
Your browser does not support the audio element.
</audio>
May it's helpful.
<audio controls="controls" autobuffer="autobuffer" autoplay="autoplay">
<source src="data:audio/wav;base64,UklGRhwMAABXQVZFZm10IBAAAAABAAEAgD4AAIA+AAABAAgAZGF0Ya4LAACAgICAgICAgICAgICAgICAgICAgICAgICAf3hxeH+AfXZ1eHx6dnR5fYGFgoOKi42aloubq6GOjI2Op7ythXJ0eYF5aV1AOFFib32HmZSHhpCalIiYi4SRkZaLfnhxaWptb21qaWBea2BRYmZTVmFgWFNXVVVhaGdbYGhZbXh1gXZ1goeIlot1k6yxtKaOkaWhq7KonKCZoaCjoKWuqqmurK6ztrO7tbTAvru/vb68vbW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Gh4aHhoWGhYaFhoWFhIWEhYSFhIWEhISDhIOEg4OCg4OCg4KDgYKCgYKCgYCBgIGAgYCBgICAgICAgICAf4B/f4B/gH+Af35/fn9+f35/fn1+fn19fn1+fX59fn19fX19fH18fXx9fH18fXx9fH18fXx8fHt8e3x7fHt8e3x7fHt8e3x7fHt8e3x7fHt8e3x7fHt8e3x8e3x7fHt8e3x7fHx8fXx9fH18fX5+fX59fn9+f35+f35/gH+Af4B/gICAgICAgICAgICAgYCBgIGAgIGAgYGBgoGCgYKBgoGCgYKBgoGCgoKDgoOCg4KDgoOCg4KDgoOCg4KDgoOCg4KDgoOCg4KDgoOCg4KDgoOCg4KDgoOCg4KDgoOCg4KDgoOCg4KCgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGCgYKBgoGBgYCBgIGAgYCBgIGAgYCBgIGAgYCBgIGAgYCBgIGAgYCAgICBgIGAgYCBgIGAgYCBgIGAgYCBgExJU1RCAAAASU5GT0lDUkQMAAAAMjAwOC0wOS0yMQAASUVORwMAAAAgAAABSVNGVBYAAABTb255IFNvdW5kIEZvcmdlIDguMAAA" />
</audio>
A Data URI takes the format:
data:[][;charset=][;base64],
The MIME-type specifies what type of data the URI contains.
The charset in which it is encoded.
last the encoded data
It looks like you are using wrong variable to set the source. You don't have $row['data'] defined in your php code. Maybe it should be $rloc instead:
<audio controls autoplay="">
<source src="<?php echo $rloc; ?>">
</audio>
Edit:
Did you convert uploaded file (3gpp) to other formats (mp3, ogg), or did you just rename it to a different extension? Looking at binary data at links you posted in comments to other answer this is your situation: all links you posted are exactly the same file, with different name. Binary, they are identical, just file name is different. Format details for the file which is on all your links are:
Format : MPEG-4
Format profile : 3GPP Media Release 4
Codec ID : 3gp4
You can not just change file extension and expect it will play in browser. You need to convert file to other format. You can use ffmpeg or any other conversion tool to convert from 3gpp to mp3 and ogg.
It is possible to use a Data URI for audio, as others have suggested. However, it's not a good idea. Not only are you adding 33% overhead for this encoding, and the CPU overhead on both ends, but there is a 1 MB limit which won't take long to hit with audio.
If you must serve your audio data this way, you can simply reference your PHP script which outputs nothing but raw binary audio data.
<audio src="/audio.php?id=12345"></audio>
Be sure to set the appropriate Content-Type header.
Finally, note that you have serious security issues in your code. As it stands right now, anyone can write pretty much whatever they want to whatever path they want on your hard drive, because you're letting them specify the filename on disk which can contain ../. Also, your SQL is subject to SQL injection attacks. Use prepared/parameterized queries to avoid this issue entirely.

How to save a html5 Canvas.toDataURl string as a png on a php backend

After converting my canvas to a an image source using
canvas.toDataURL("image/png");
and passing it to a php file, how do I save it as a .png image on the server?
It's actually very simple, if you have allow-url-fopen enabled. PHP supports the data: URL scheme then, and automatically decodes base64 and urlencoding.
preg_match('#^data:[\w/]+(;[\w=]+)*,[\w+/=%]+$#', $data=$_POST["dataU"])
and
copy($data, "output.png");
But you could also just extract the part after the , and manually base64_decode() it.

Categories