|
| 1 | +Introduction |
| 2 | +============ |
| 3 | +The php-task library provides a simple and easy to extend interface |
| 4 | +to handle synchronous and asynchronous tasks in PHP. |
| 5 | + |
| 6 | +What it does |
| 7 | +------------ |
| 8 | +It allows to implement worker classes in PHP for tasks. These worker |
| 9 | +can be implemented in your favorite environment. Over a simple |
| 10 | +interface the developer can define and schedule long running tasks |
| 11 | +without any overhead. |
| 12 | + |
| 13 | +One typical usecase is generating thumbnails or rendering videos. |
| 14 | +These tasks are to long to run them immediately. These task can be |
| 15 | +done after generating the response to the user. |
| 16 | + |
| 17 | +How it works |
| 18 | +------------ |
| 19 | +The php-task library provides two ways to schedule tasks: |
| 20 | + |
| 21 | +* Gearman_: A generic application framework for farming out work |
| 22 | + to multiple machines or processes. |
| 23 | +* `PHP Implementation`_: A implementation of the php-task library |
| 24 | + in raw PHP. |
| 25 | + |
| 26 | +Quick Example |
| 27 | +------------- |
| 28 | +This example will assume you want to generate thumbnail images. |
| 29 | + |
| 30 | +.. note:: |
| 31 | + |
| 32 | + The example uses the raw PHP implementation for php-task. |
| 33 | + |
| 34 | +.. code-block:: php |
| 35 | +
|
| 36 | + <?php |
| 37 | +
|
| 38 | + use Task\Scheduler\TaskInterface; |
| 39 | +
|
| 40 | + class ImageResizeWorker implements Task\TaskRunner\WorkerInterface |
| 41 | + { |
| 42 | + /** |
| 43 | + * {@inheritdoc} |
| 44 | + */ |
| 45 | + public function run(Task\Scheduler\TaskInterface $task) |
| 46 | + { |
| 47 | + list($sourceImagePath, $destinationImagePath, $desiredWidth) = $task->getWorkload(); |
| 48 | +
|
| 49 | + /* read the source image */ |
| 50 | + $sourceImage = imagecreatefromjpeg($sourceImagePath); |
| 51 | + $width = imagesx($sourceImage); |
| 52 | + $height = imagesy($sourceImage); |
| 53 | +
|
| 54 | + /* find the "desired height" of this thumbnail, relative to the desired width */ |
| 55 | + $desiredHeight = floor($height * ($desiredWidth / $width)); |
| 56 | +
|
| 57 | + /* create a new, "virtual" image */ |
| 58 | + $virtualImage = imagecreatetruecolor($desiredWidth, $desiredHeight); |
| 59 | +
|
| 60 | + /* copy source image at a resized size */ |
| 61 | + imagecopyresampled($virtualImage, $sourceImage, 0, 0, 0, 0, $desiredWidth, $desiredHeight, $width, $height); |
| 62 | +
|
| 63 | + /* create the physical thumbnail image to its destination */ |
| 64 | + imagejpeg($virtualImage, $destinationImagePath); |
| 65 | + } |
| 66 | +
|
| 67 | + /** |
| 68 | + * {@inheritdoc} |
| 69 | + */ |
| 70 | + public function getNamespace() |
| 71 | + { |
| 72 | + return 'app'; |
| 73 | + } |
| 74 | +
|
| 75 | + /** |
| 76 | + * {@inheritdoc} |
| 77 | + */ |
| 78 | + public function getName() |
| 79 | + { |
| 80 | + return 'image_resize'; |
| 81 | + } |
| 82 | + } |
| 83 | +
|
| 84 | + // bootstrap |
| 85 | +
|
| 86 | + $namingFactory = new Task\Naming\NamingFactory(); |
| 87 | + $taskStorage = new Task\PHP\ArrayStorage\TaskStorage(); |
| 88 | + $taskRunner = new Task\PHP\TaskRunner($taskStorage, $namingFactory); |
| 89 | + $scheduler = new Task\PHP\Scheduler($taskStorage, $taskRunner); |
| 90 | +
|
| 91 | + // add worker instances |
| 92 | + $taskRunner->addWorker(new ImageResizeWorker()); |
| 93 | +
|
| 94 | + // schedule task |
| 95 | + $scheduler->schedule(new Task\Scheduler\Task('app.image_resize', ['example-1.jpg', 'thumbnails/example-1.jpg', 100])); |
| 96 | + $scheduler->schedule(new Task\Scheduler\Task('app.image_resize', ['example-2.jpg', 'thumbnails/example-2.jpg', 100])); |
| 97 | +
|
| 98 | + // run task |
| 99 | + $taskRunner->run(); |
| 100 | +
|
| 101 | +The example will generate two thumbnail image one for the jpg ``example-1.jpg`` |
| 102 | +and one for ``example-2.jpg`` both in the folder thumbnails. |
| 103 | + |
| 104 | +Integration |
| 105 | +----------- |
| 106 | +The library provides a integration into Symfony_ framework (see :doc:`symfony`). |
| 107 | + |
| 108 | +.. _Gearman: http://gearman.org |
| 109 | +.. _PHP Implementation: https://github.com/php-task/php |
| 110 | +.. _Symfony: http://symfony.com/ |
0 commit comments