PageRenderTime 45ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/Gd/Filter/Distort.php

https://github.com/meze/GenemuFormBundle
PHP | 69 lines | 44 code | 11 blank | 14 comment | 6 complexity | cff9ebba11e20bc5ac5e396e364ef100 MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Genemu package.
  4. *
  5. * (c) Olivier Chauvel <olivier@generation-multiple.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Genemu\Bundle\FormBundle\Gd\Filter;
  11. use Genemu\Bundle\FormBundle\Gd\Gd;
  12. /**
  13. * @author Olivier Chauvel <olivier@generation-multiple.com>
  14. */
  15. class Distort extends Gd implements Filter
  16. {
  17. /**
  18. * {@inheritdoc}
  19. */
  20. public function apply()
  21. {
  22. $X = mt_rand(0, $this->width);
  23. $Y = mt_rand(0, $this->height);
  24. $Phase = mt_rand(0, 10);
  25. $Scale = 1.3 + mt_rand(0, 10000) / 30000;
  26. $Amp = 1 + mt_rand(0, 1000) / 1000;
  27. for ($x = 0; $x < $this->width; ++$x) {
  28. for ($y = 0; $y < $this->height; ++$y) {
  29. $Vx = $x - $X;
  30. $Vy = $y - $Y;
  31. $Vn = sqrt($Vx * $Vx + $Vy * $Vy);
  32. if ($Vn != 0) {
  33. $Vn2 = $Vn + 4 * sin($Vn / 8);
  34. $nX = $X + ($Vx * $Vn2 / $Vn);
  35. $nY = $Y + ($Vy * $Vn2 / $Vn);
  36. } else {
  37. $nX = $X;
  38. $nY = $Y;
  39. }
  40. $nY = $nY + $Scale * sin($Phase + $nX * 0.2);
  41. $p = $this->bilinearInterpolate(
  42. $nX - floor($nX),
  43. $nY - floor($nY),
  44. $this->getColor(floor($nX), floor($nY)),
  45. $this->getColor(ceil($nX), floor($nY)),
  46. $this->getColor(floor($nX), ceil($nY)),
  47. $this->getColor(ceil($nX), ceil($nY))
  48. );
  49. if ($p === 0) {
  50. $p = 0xFFFFFF;
  51. }
  52. $color = $this->getColor($x, $y);
  53. imagesetpixel($this->resource, $x, $y, $p);
  54. }
  55. }
  56. return $this->resource;
  57. }
  58. }