FileFactory.php 1.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. <?php
  2. namespace Illuminate\Http\Testing;
  3. class FileFactory
  4. {
  5. /**
  6. * Create a new fake file.
  7. *
  8. * @param string $name
  9. * @param int $kilobytes
  10. * @return \Illuminate\Http\Testing\File
  11. */
  12. public function create($name, $kilobytes = 0)
  13. {
  14. return tap(new File($name, tmpfile()), function ($file) use ($kilobytes) {
  15. $file->sizeToReport = $kilobytes * 1024;
  16. });
  17. }
  18. /**
  19. * Create a new fake image.
  20. *
  21. * @param string $name
  22. * @param int $width
  23. * @param int $height
  24. * @return \Illuminate\Http\Testing\File
  25. */
  26. public function image($name, $width = 10, $height = 10)
  27. {
  28. return new File($name, $this->generateImage($width, $height));
  29. }
  30. /**
  31. * Generate a dummy image of the given width and height.
  32. *
  33. * @param int $width
  34. * @param int $height
  35. * @return resource
  36. */
  37. protected function generateImage($width, $height)
  38. {
  39. return tap(tmpfile(), function ($temp) use ($width, $height) {
  40. ob_start();
  41. imagepng(imagecreatetruecolor($width, $height));
  42. fwrite($temp, ob_get_clean());
  43. });
  44. }
  45. }