How to monitor progress of conversion, PDF to JPG with Imagick? - php

I am using PHP exec() for -convert Image Magick command, and i want to convert more than one page.
Given that it may take a few minutes, I need some kind of progress bar in order to monitor conversion.
Any ideas how I could done this?

well you can not really track the progress of a single conversion. but you could for exmaple do something like the following when you want to convert multiple documents:
the number of pages = 100%
current page number / number of pages * 100 = progress in percent
so after each processed page you can update the progress.
you can write the info in a file or in a database (linked to the user session for a multi-user software) and poll for the status with an ajax reqeust to a php script which reads this file/db.

A solution for this problem is message queues. I forked a code example of how to use Pheanstalkd (a PHP framework for beanstalkd) here
This example shows how to have a sender that puts jobs in queue, worker that pulls jobs from queue, and a watcher that watches jobs (this part essentially does what you are asking for).
You can download Pheanstalk from here

Related

Progress bar for an application

I have implemented a SAAS scenario with my Windows server: a user could upload a file in a website, then Fetch.exe is an application coded in C# hosted in the server, Fetch.exe takes the uploaded file as an input, executes and generates an output to download for the user.
So in my php, I use exec to wrap Fetch.exe:
exec("Fetch.exe " . $inputFile . " > " . $outputFile)
Uploading and executing (ie, Fetch.exe) may take more than several seconds, and I want to show the user that it is processing and everything is going fine.
I have found some threads that discuss how to show a progress bar for the uploading. Whereas, does anyone know what I could do to show the approximate progress of Fetch.exe? Do I have to split it into smaller applications and use several exec?
You could supply Fetch.exe with an randomly generated ID from php, for example from the uniqueid function. Fetch.exe will create a file called <uniqueid>.txt with a progress percentage. From the browser, you could call another script with that unique ID to get the contents of that .txt file. In order, it would be something like this:
User uploads the file to PHP
PHP:
handles the uploaded file
creates a uniqueID
starts Fetch.exe with the file and the uniqueID
returns a page with the uniqueID embedded
The following happens in parallel:
Fetch.exe creates a textfile called /progress/uniqueid.txt with the uniqueid as name. It logs the progress into it.
The browser does an AJAX call to http://example.com/progress/uniqueid.txt and shows the progress to the user
And finally, when the progress reaches 100% the browser downloads the file. The only thing you might want to add is the pruning of the progress files after a while. Say you delete all files older than 10 minutes every hour.
Your PHP program needs a way to know the state of the subprocess (the Fetch.exe application), so, Fetch.exe needs to send info about the processing state, the most natural way to do this is through the standard output (the standard output is the information that provides a program when you run it from cmd).
Knowing this, you can run and keep reading a subprocess output from php using popen().
And secod, you can use the PHP ob_flush() and flush() with the onmessage javascript event to establish the comunication from your client page with your running php script, here you can find a good tutorial on how do this.

Architecture for streaming an endless video queue with mysql datasource

right now i'm building a webinterface. It should be something like a media control system. In this interface a user can select some vid's from a coverflow'ish style and put the videos in a queue. The queue is visualized like a timeline. This interface is webbased, made with TYPO3 FLOW. So i got in my database data to play/stream.
I'm trying to figure out how to stream my selected data. I'd prefer a webbased solution but i'm open for everything.
Could anyone please give me a hint what kind of technique would fit best? This is what i need:
the playlist/queue is stored in a database (not the videos itself but the meta-data and file-urls)
people can add items to the queue over a webinterface (already implemented)
if the queue / playlist is empty i'd like to play some random videos
if someone adds a video to the playlist / queue this video needs to be played at a given time (mostly now()+60sec if the queue is empty, endtime last clip +x when not)
if i open the stream while a video is started in the queue the video should start at that time should be at
So i need to stream dynamicly videos with in an endless loop. I'd love to have a webbased solution. Like something with node.js and vidStreamer.js but i'm not familiar with it yet. Would it be possible to play content dynamically, based on mysql input?
I would do that mostly client side with Javascript. Export the metadata and preload the videos hidden from view, then start as needed. With HTML5 video you have easily access to the video and player properties so you can get the timing right.

Need to write to a text file as video caption with 7second delay

I have video running as a live stream and an editor can add captions at any time. Also when microphones are clicked in the broadcast room they move the camera and then update the caption.
The flash video pulls a text file every 3 seconds for the new caption.
The video takes an average of 7secs delay to get to the web users though and so I need to write some php to hold the update somewhere and then write it 7 seconds later to the text file.
I need to update away from a Java demon that needs to keep a socket open at the moment.
I have thought of trying out a queue or a cron job. Cron does not do second updates and queue seems to mean running a java demon again, and have not found a way or pausing yet.
The caption needs to stay in a text file and must not do a db call, but the api can be changed in any simple way to delay the updates.
So Jason object generated through the api (only 1-5k) held for 7seconds and then written to text file.
You should look into long polling and real time updates, then do some sort of loop that pushes content when specified.
You can setup a very simple webservice with something like Pubnub or Pusher. And then do some sort of loop in the server side of your choice that pushes contents with the right intervals to make sure data is there.
Please be aware that the design you mention has race conditions all over, so take that in consideration when building it.

Need to execute a code in background

I have done a code to receive images from iphone to PHP Server and I need to resize these image and move to 4 folders.
Only then the json respose is giving to iphone. But it takes much time.
Requirement:
i want to move a file to the folder "folder1" then want to give the json response.
the resizing process should do from this "folder1" after giving json response.
How to run this resizing process in background.
Here is my code:
http://pastebin.com/qAcT1yi9
You could always send your php script to run in the background with a Linux command.
Example:
// using backticks to execute the Linux command but there are
// other alternatives
$cmd = `php runScriptInBackground.php &`;
echo $cmd;
First send/upload the images and send a response back, without doing the resize operation.
Then, if the upload was successful, let the browser issue another request and do the resizing. When this succeeds, send the message ‘resizing successful’ back.
A common solution to this problem is implementing a loading/processing message on hitting a specific event. Then - still being displayed - the action will continue to load on the background and the result page will finally be displayed when done.
Although the user must wait, I prefer this above display a result message when the actual result is not known. Unfortunately I'm not sure how this is done on iphone development.
if your building in objective c then you may just resize make a copy and resize it there and send the resized image to your php you could then display a spinner and json result back to the user and also if the is an error the user will still have the resized image to try again with... Also another thought I had was was to use push notification. I don't know what that code would look like but it's something to consider
you need some async javascript or an iframe in your page posting the image to your server and providing feedback to the user.
This means that the 'main' page would not change, but some visual information can be provided to the user.
You can display an animated gif loader or use JS setInterval to give the user the feeling that things are moving forward why waiting for the server to respond.
If the processing is split in more 1 parts, after each step the server could respond with an HTML page and a redirect: this would even work in an IFRAME without JS.
Each 'page' would perform one more step. But if the user closes the browser before all is done you would end with an unfinished task.
A DB, real background processing, and client side JS polling are a more robust alternative.
A full answer would be quite long and require way more details on your settings (apache CGI PHP? or mod_php? are you using an MVC model or framework, or are you writing a page-oriented website?).
If i had to write a full answer I would forget PHP and use Python and celery http://celeryproject.org/ ;-)
PS.
I just found out that a few related questions already existed:
PHP Background Processes
Asynchronous shell exec in PHP
You can do it realy in two times, first send de files and save on first server, after when the user request that you generate the necesary parts.
You will pass the costs from the file sender to the first request from that.

How do I track file downloads

I have a website that plays mp3s in a flash player. If a user clicks 'play' the flash player automatically downloads an mp3 and starts playing it.
Is there an easy way to track how many times a particular song clip (or any binary file) has been downloaded?
Is the play link a link to the actual
mp3 file or to some javascript code
that pops up a player?
If the latter, you can easily add your
own logging code in there to track the
number of hits to it.
If the former, you'll need something
that can track the web server log
itself and make that distinction. My
hosting plan comes with Webalizer,
which does this nicely.
It's a javascript code so that answers that.
However, it would be nice to know how to track downloads using the other method (without switching hosts).
The funny thing is I wrote a php media gallery for all my musics 2 days ago. I had a similar problem. I'm using http://musicplayer.sourceforge.net/ for the player. And the playlist is built via php. All music requests go to a script called xfer.php?file=WHATEVER
$filename = base64_url_decode($_REQUEST['file']);
header("Cache-Control: public");
header('Content-disposition: attachment; filename='.basename($filename));
header("Content-Transfer-Encoding: binary");
header('Content-Length: '. filesize($filename));
// Put either file counting code here, either a db or static files
//
readfile($filename); //and spit the user the file
function base64_url_decode($input) {
return base64_decode(strtr($input, '-_,', '+/='));
}
And when you call files use something like:
function base64_url_encode($input) {
return strtr(base64_encode($input), '+/=', '-_,');
}
http://us.php.net/manual/en/function.base64-encode.php
If you are using some JavaScript or a flash player (JW player for example) that requires the actual link of an mp3 file or whatever, you can append the text "&type=.mp3" so the final link becomes something like:
"www.example.com/xfer.php?file=34842ffjfjxfh&type=.mp3". That way it looks like it ends with an mp3 extension without affecting the file link.
Use your httpd log files. Install http://awstats.sourceforge.net/
Use bash:
grep mp3 /var/log/httpd/access_log | wc
If your song / binary file was served by apache, you can easily grep the access_log to find out the number of downloads. A simple post-logrotate script can grep the logs and maintain your count statistics in a db.
This has the performance advantage by not being in your live request code path. Doing non-critical things like stats offline is a good idea to scale your website to large number of users.
You could even set up an Apache .htaccess directive that converts *.mp3 requests into the querystring dubayou is working with. It might be an elegant way to keep the direct request and still be able to slipstream log function into the response.
Is the play link a link to the actual mp3 file or to some javascript code that pops up a player?
If the latter, you can easily add your own logging code in there to track the number of hits to it.
If the former, you'll need something that can track the web server log itself and make that distinction. My hosting plan comes with webalizer, which does this nicely.
Is there a database for your music library? If there is any server code that runs when downloading the mp3 then you can add extra code there to increment the play count. You could also have javascript make a second request to increment the play count, but this could lead to people/robots falsely incrementing counts.
I used to work for an internet-radio site and we used separate tables to track the time every song was played. Our streams were powered by a perl script running icecast, so we triggered a database request every time a new track started playing. Then to compute the play count we would run a query to count how many times a song's id was in the play log.
The problem I had with things like AWStats / reading through web server logs is that large downloads can often be split in data chunks within the logs. This makes reconciling the exact number of downloads quite hard.
I'd suggest the Google Analytics Event Tracking, as this will register once per click on a download link.

Categories