1

Normally when you have a .PHP file and the client request it, the PHP code is run on the server and the HTML and JavaScript are sent to the client.

Question

Is it possible to have the server request a webpage (local) and run both the PHP code and the HTML with JavaScript on the server? I have created a single .html file that after 3 seconds of processing locally creates the image data for a thumbnail of the given video.

Why

I need to generate a thumbnail for a video. I used shared hosting and my hosting provider doesn't support for ffmpeg. You can, however, generate thumbnails using a canvas and JavaScript. I have already put a lot of pressure on the client. If this is possible, upload and download times would be significantly shorter than using the client.

Attempts

I've tried using file_get_contents(), but it doesn't run the code (Makes sense). Is there a way I could have it open and run for x seconds and then grab the contents?

I've tried using curl to get the file using this function here. I believe it is similar to my previous attempt in that it gets the file contents, but never executes them.

My final attempt was to use new DOMDocument(). I couldn't even get to loading the page though. First, I can't parse it with a video tag. It gives this error:

Warning: DOMDocument::load(): Specification mandates value for attribute controls in 
file:\path\to\html\document.html, line: 53 in C:\path\to\php\document.php on line 50

If I were to remove the video tag (which is required), I get errors while parsing my JavaScript. So that attempt also did not work.


Is there a way that I could have PHP process the code (for something on the server) for x seconds before getting the contents? It would allow for time to generate the thumbnail data. If there is another way to do this without using ffmpeg on the server, that would be great.

Jack
  • 1,893
  • 1
  • 11
  • 28
  • Have a look at https://stackoverflow.com/questions/21179522/create-thumbnail-from-video-without-ffmpeg-in-php. Hope this will help. – Deepak Apr 04 '20 at 16:24
  • @Deepak, thanks! That is currently what I am doing. I am just looking for a way to run that script on the server, instead of having to run it on the client to cut down on file transfer time and pressure for the client. – Jack Apr 04 '20 at 16:26
  • What I'm suggesting is not a good idea of course but have you consider using something like a hidden iframe? For example consider google login. A page opens and you do your login and it saves somewhere and you get your response from it using APIs and now you can do something like that using a hidden iframe. – Mohsen Nemati Apr 04 '20 at 18:08
  • 1
    @MohsenNemati, Ok I haven't thought of that. Where were you suggesting this be put? When the admin user submits the video? Or when the video is displayed the common user? The admin user in my case can't have hardly any processing done client-side which is really limiting me. – Jack Apr 04 '20 at 18:58
  • I'll explain it as an answer. – Mohsen Nemati Apr 05 '20 at 09:43

2 Answers2

1

So as I mentioned in comments, what I'm gonna explain is just an option (not the best one and just answering for your need of running html code!)

Where to do this?

Personally I rather to do this when the video is being uploaded by admin's browser and the best thing is that you can do this as a part of the posting procedure.
So in the page that you want this process to be done, put an invisible iframe like this.

<iframe id="myIframe" style="display: none;"></iframe>

How to begin the process?

I don't know the way you use to upload the videos (and it really is not that important!) but let's assume you want to use formdata. After the video is uploaded you need to know something unique to address the video (let's say an id). So after the video is uploaded, we can recive a code like id:20, initiateThumbnail:true as the result json data. Then we can simply use that hidden iframe to be the browser you've been asking for like this:

$("#myIframe").attr("src","dothething.php?video=20");

Now do what ever you wanted to do in it and change it's content after it's done. Now you need to wait for the result!

$('#myIframe').load(()=>{
    let result = $("#myIframe").contents();
    // checking result!
});

As you have already thought about, you can handle any errors by processing the result.

Notes

  1. The event listener we used for iframe (iframe.load) fires when you initiate making the thumbnail as well. So be careful with the process of checking result (content of that iframe!)
  2. If you don't use ajax or formdata, simply the action of your form is what I used as iframe.
  3. One question? What happens if network connection goes down during this process? Simple answer! You can check in so many ways that the thumbnail exists or not. If not you can create it once that user requests for it in his browser and upload it back to server and save it for ever (as you did it in admin's panel!)
Mohsen Nemati
  • 379
  • 1
  • 11
  • Thanks for your answer, how would this be different than just including `dothething.php` in the page? Would it start a new thread because of the `iframe`? Sorry for the late, reply, I didn't see SO notify me. – Jack Apr 05 '20 at 22:40
  • @Jack dothething.php is responsible for creating thumbnail. The concept of what I explained is important and based on how your codes work you can design the structure. For example if you need some information from server for editing each video, you should either use iframe to load data each time or use ajax to get that data (I personally choose ajax.) If not, you are right using iframe is not necessary. – Mohsen Nemati Apr 06 '20 at 05:40
0

I think there isn’t another way to generate thumbnail on php server than with ffmpeg. The only thing you can do, I suppose, is to force canvas generation on page load if you aren’t already doing it.

Anyway you are trying to do something wrong. Php doesn’t evaluate the html code, it’s just a preprocessor and not an interpreter like the browser. You can wait all the time of the world, but you’ll never get the content of the image that only a browser will generate.