Streaming a video using PHP and manipulating it using FFMPEG - php

I use the class in this tutorial to open a mp4 file in PHP and stream it to HTML5 video player and everything works fine. Now I would like to do some manipulations to the stream on the fly. e.g: adding a watermark to the stream. I am not sure if it can be done using ffmpeg command or PHP-FFMpeg.
The class in the mentioned tutorial opens the file (or the range of the file that is needed by the player), into the $data variable:
$data = fread($this->stream, $bytesToRead);
echo $data;
flush();
I guess I need to pass the $data to ffmpeg somehow, add the watermark and then echo() the manipulated $data instead of the original one. But I am not sure if it's possible or not, and if it is, how it can be done? I don't know how to open a variable in ffmpeg instead of a file and how to get the output as a variable, and not a file.
Any help would be appreciated.

Related

Programmatically (PHP) save image with no extension

I am trying to save to disk an image that is served to me via a JSON result. The returned JSON result property that I am interested in is this:
https://i.scdn.co/image/6cd03f58ddf30a1393f06d6469973ba16ac908df
Which is the correct image. The problem is that, while the above URL does display the image, it does not allow me to download it, yet I can download it by right-clicking on it.
What I need to be able to do is, using my PHP code, save it to disk.
I have no issues saving results from other sites that give results that link to a direct image extension (.jpg, .gif or .png). But I have not been able to figure out how to programmatically download the image from the above URL.
Is it possible?
This is the code that I use, which works correctly on results that give a URL that has a correct image extension. The URL returned is loaded into the $largeimg variable.
$input = $largeimg;
$output = 'image.jpg';
file_put_contents($output, file_get_contents($input));
How do I achieve this?
file_get_contents() is able to accept raw URI arguments. Your code works perfectly for me, if modified in the way:
$input = 'https://i.scdn.co/image/6cd03f58ddf30a1393f06d6469973ba16ac908df';
So, file_get_contents() can download the image directly. I think, the problem is your $largeimg variable.

Passing a file from command line to PHP stdin

I am using an image manipulation library in PHP called Intervention. It's a great library but doesn't quite have all the features I need, specifically lossless compression. There are command line tools that do this, but I want to do the processing on the fly and pass the data to the browser, not write the data to the file system.
I want to pull the file data the is supposed to go to a file to be fed straight to PHP, but I am not familiar enough with the command line to know how to "pipe" the data to PHP or how to get PHP to read the data. Maybe something like this:
use Intervention\Image\ImageManagerStatic as Image;
$data = exec('optipng image.png >tophp');
$image_data = fopen('php://stdin', 'r');
$img = Image::make($image_data);
//...do more stuff...
echo $img->response();
I know that obviously won't work, but is there a way to accomplish what I am trying to do?
For any command line tools that have the ability to return the image data to stdout, you can capture that using shell_exec. Then you can pass that data to Intervention. Otherwise, you could save the data to a tmp file, have PHP read the data and then delete the file.

Using PHP to upload a file that's generated by a URL

So, I have a little PHP code that uses Kaywa to generate and display a QR code:
echo "<img src='http://qrcode.kaywa.com/img.php?s=10&d=$qr_url' alt='QR code' />";
Easy peasy. But for the sake of having a backup, I'd like to save this image to my server, maybe in "myserver/qrbackups". I know how to make PHP upload a file from a form, but can I do from a retrieved image URL?
See file_get_contents.
$data = file_get_contents("http://qrcode.kaywa.com/img.php?s=10&d=$qr_url");
$saved = file_put_contents('/path/to/myserver/qrbackups/the-code.png', $data);
Keep in mind /path/to/myserver/qrbackups/the-code.png should be a unique file name for each individual QR code.
You can use cURL to programmatically access URLs.
http://us3.php.net/manual/en/curl.examples-basic.php
You can try to use curl or file_get_contents to pull the file from the server.
For example:
http://www.phpriot.com/articles/download-with-curl-and-php
And after getting the file just display the one you got from your server or remote.
copy('http://domain.com/path', '/tmp/file.jpeg');
should work for you. And, as others have pointed you, cURL will work too.
If you have the GD extension on your server, you can use a library like PHP QR Code to eliminate the dependency on Kaywa. Usage requires only one line of code wherever you want to generate a QR code.

Image creation using php

Am trying to create a video clip using .jpg images and ffmpeg, am creating .jpg images as below:
$str=$_REQUEST['data_array'];//gets the base64 encoded image data;
$count =$_REQUEST['count'];//gets number of images i've to create;
$edited_str=substr($str,23,strlen($str)-1);//
$edited_str=base64_decode($edited_str);
$f=fopen("Images/temp".$count.".jpg","w");//am creating temp.jpg file
fwrite($f,$edited_str);//placing my decoded data into file
fclose($f);
are the images am creating above different from normal .jpg images?
This line:
$edited_str=substr($str,23,strlen($str)-1);
makes it different. If this is the full base64 sting of the file, then this cuts it up and corrupts it. Maybe you are adding some stuff on the front.
If you are just removing stuff from the front that was added, then it should be the same as the original file that was encoded with base64.
If you want to get the information this way from another page, I suggest using $_POST as opposed to $_REQUEST for a number of reasons.
EDIT: I wouldn't say video manipulation in php is impossible. I think there is even a toolkit... here's one:
http://phpvideotoolkit.sourceforge.net/
which states:
It can perform video format conversion, extract video frames into separate image files, and assemble a video stream from a set of separate video images.
Haven't tested it, but plan to try it out one day.
EDIT2: On the php site, there were some issues that you could try, but to help more, there needs to be more information. Check with a file directly to make sure it's being sent and decrypted properly.
I haven't got to test any of these yet.
One piece of advice was for large files use:
$decodedstring=base64_decode(chunk_split($encodedstring));
Another was if you use javascript canvas.toDataURL() then you need to convert spaces back to pluses:
$encodedData = str_replace(' ','+',$encodedData);
$decocedData = base64_decode($encodedData);
http://php.net/manual/en/function.base64-decode.php

How can I save this php created image?

What are some possible ways to save an image or make use of it that is generated from a PHP script. Using save as it does not help though.
This is not an image created by me that's why I want to avoid get_contents.
here is the picture
and here is the url
https://render01.fontshop.com/fonts/font_rend.php?idt=f&id=38005&rbe=fsifr&rt=how+do+I+save+this?&rs=38&w=500&bg=ffffff&fg=000000&tp=0.0
Just write the content of the URL to a file
<?php
file_put_contents("img.png", file_get_contents("http://render01.fontshop.com/fonts/font_rend.php?idt=f&id=38005&rbe=fsifr&rt=how+do+I+save+this?&rs=38&w=500&bg=ffffff&fg=000000&tp=0.0"));
Using file_put_contents() function. If you don't have data in variable and want to readout use file_get_contents()
Since you are not generating the image in your own code, the simplest would be a combo of file_get_contents and file_put_contents:
$url = '...'; // your url here
$data = file_get_conents($url);
file_put_conents('image.png', $data);
In this specific case the render is a PNG image, but if there's a possibility of it being a JPEG or something else then you need to somehow detect that as well. I 'm not giving any suggestions for this because there's not enough info to go by.
You can define a filename in imgpng() or the other functions to tell PHP to store the picture instead of sending it to the calling browser.
I understand you want to save it on the client, with a browser, not on the server.
"Save as" worked fine for me (Firefox 7). In Chrome you'll have to specify the extension of the filename manually. Did not test other browsers, but it should work similarly
You can do this from the terminal using the curl command.
curl -o out.png 'http://render01.fontshop.com/fonts/font_rend.php?idt=f&id=38005&rbe=fsifr&rt=how+do+I+save+this?&rs=38&w=500&bg=ffffff&fg=000000&tp=0.0'
This will save the file as out.png
use imagepng function.
It will return file to browser or save it specified location.
Need to set parameter for function to save image on specified location.

Categories