image_thumbnail_client_task.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <?php
  2. /*
  3. * Gearman PHP Extension
  4. *
  5. * Copyright (C) 2008 James M. Luedke (jluedke@jamesluedke.com)
  6. * Eric Day (eday@oddments.org)
  7. * All rights reserved.
  8. *
  9. * Use and distribution licensed under the PHP license. See
  10. * the LICENSE file in this directory for full text.
  11. */
  12. /* create our object */
  13. $gmc= new GearmanClient();
  14. /* add the default server */
  15. $gmc->addServer();
  16. /* set a few callbacks */
  17. $gmc->setCreatedCallback("thumb_created");
  18. $gmc->setCompleteCallback("thumb_complete");
  19. $gmc->setFailCallback("thumb_fail");
  20. for ($x= 0; $x<20; $x++)
  21. {
  22. $data[$x]['src']= $_SERVER['argv'][1];
  23. $data[$x]['dest']= "$x.jpg";
  24. $data[$x]['x']= ((80+1)*($x+1));
  25. $data[$x]['y']= NULL;
  26. }
  27. /* fire off each job */
  28. foreach ($data as $img)
  29. {
  30. /* NOTE: if you want to asynchronously queue jobs use
  31. ** $task= $gmc->add_task_background("shrink_image", serialize($img));
  32. ** however keep in mind that your complete callback will not get called */
  33. if (! $gmc->addTask("shrink_image", serialize($img)))
  34. {
  35. echo "ERROR RET: " . $gmc->error() . "\n";
  36. exit;
  37. }
  38. }
  39. if (! $gmc->runTasks())
  40. {
  41. echo "ERROR RET:" . $gmc->error() . "\n";
  42. exit;
  43. }
  44. echo "DONE\n";
  45. exit;
  46. function thumb_created($task)
  47. {
  48. echo "CREATED -> job: " . $task->jobHandle() . "\n";
  49. }
  50. function thumb_complete($task)
  51. {
  52. echo "COMPLETE -> job: " . $task->jobHandle() .
  53. " new_file: " . $task->data() . "\n";
  54. }
  55. function thumb_fail($task)
  56. {
  57. echo "FAIL job: " . $task->jobHandle() . "\n";
  58. }
  59. ?>