* The queue worker script * * To be run as the application component responsible for watermarking uploaded images. Polls queue for new jobs * and will keep polling queue until there are now jobs left, when it will wait for 20 seconds before continuing * to poll the queue. * * @package PhpSqsTutorial * @author George Webb <*****@*****.**> * @license http://opensource.org/licenses/MIT MIT License * @link http://george.webb.uno/posts/aws-simple-queue-service-php-sdk */ require_once __DIR__ . '/config.php'; require_once __DIR__ . '/vendor/autoload.php'; use Gaw508\PhpSqsTutorial\Queue; // Instantiate queue with aws credentials from config. $queue = new Queue(QUEUE_NAME, unserialize(AWS_CREDENTIALS)); // Continuously poll queue for new messages and process them. while (true) { $message = $queue->receive(); if ($message) { try { $message->process(); $queue->delete($message); } catch (Exception $e) { $queue->release($message); echo $e->getMessage(); } } else { // Wait 20 seconds if no jobs in queue to minimise requests to AWS API sleep(20); }
} elseif (!filesize($_FILES['images']['tmp_name'][$i])) { $warnings[] = array('class' => 'alert-danger', 'text' => 'Error uploading file.'); } elseif ($_FILES['images']['type'][$i] != 'image/png' and $_FILES['images']['type'][$i] != 'image/jpeg') { $warnings[] = array('class' => 'alert-danger', 'text' => 'Invalid file type.'); } elseif ($_FILES['images']['size'][$i] > 2000000) { $warnings[] = array('class' => 'alert-danger', 'text' => 'File too big.'); } else { // Create a new filename for the uploaded image and move it there $extension = $_FILES['images']['type'][$i] == 'image/png' ? '.png' : '.jpg'; $new_name = uniqid() . $extension; if (!move_uploaded_file($_FILES['images']['tmp_name'][$i], __DIR__ . '/images/queued/' . $new_name)) { $warnings[] = array('class' => 'alert-danger', 'text' => 'Error uploading file.'); } else { // Create a new message with processing instructions and push to SQS queue $message = new Message(array('input_file_path' => __DIR__ . '/images/queued/' . $new_name, 'output_file_path' => __DIR__ . '/images/watermarked/' . $new_name)); $queue = new Queue(QUEUE_NAME, unserialize(AWS_CREDENTIALS)); if ($queue->send($message)) { $successes++; } else { $warnings[] = array('class' => 'alert-danger', 'text' => 'Error adding file to queue.'); } } } } if ($successes > 0) { $warnings[] = array('class' => 'alert-success', 'text' => "{$successes} images uploaded successfully."); $warnings[] = array('class' => 'alert-info', 'text' => "Uploaded images added to queue..."); } } } ?>