src/Controller/ImageCache/ImageManager.php line 29

Open in your IDE?
  1. <?php
  2. namespace App\Controller\ImageCache;
  3. use Intervention\Image\Exception\NotReadableException;
  4. use Intervention\Image\ImageManager as BaseImageManager;
  5. use Symfony\Component\DependencyInjection\ContainerInterface;
  6. class ImageManager extends BaseImageManager
  7. {
  8. protected $container;
  9. public function __construct(ContainerInterface $container)
  10. {
  11. $this->container = $container;
  12. }
  13. public function make($data)
  14. {
  15. $driver = $this->createDriver();
  16. $domain = $data['siteUrl'];
  17. $imageUrl = $data['imageUrl'];
  18. if (
  19. filter_var($imageUrl, FILTER_VALIDATE_URL)
  20. && filter_var($domain, FILTER_VALIDATE_URL)
  21. ) {
  22. return $this->initFromUrl($driver, $imageUrl, $domain);
  23. }
  24. return $driver->init($data);
  25. }
  26. /**
  27. * This method hit the tracker image url and create a live instance
  28. *
  29. * @param mixed $driver
  30. * @param mixed $imageUrl
  31. * @param mixed $domain
  32. * @throws \Intervention\Image\Exception\NotReadableException
  33. * @return mixed
  34. */
  35. public function initFromUrl($driver, $imageUrl, $domain)
  36. {
  37. try {
  38. $options = [
  39. 'http' => [
  40. 'method' => 'GET',
  41. 'protocol_version' => 1.1,
  42. 'header' => "Accept-language: en\r\n" .
  43. "Domain: $domain\r\n" .
  44. "User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/97.0.4692.71 Safari/537.36\r\n",
  45. ],
  46. ];
  47. $context = stream_context_create($options);
  48. $data = @file_get_contents($imageUrl, false, $context);
  49. if ($data) {
  50. return $driver->decoder->initFromBinary($data);
  51. }
  52. } catch (\Exception $e) {
  53. throw new NotReadableException('Error: (' . $e . ').');
  54. }
  55. throw new NotReadableException('Unable to init from given URL (' . $imageUrl . ').');
  56. }
  57. private function createDriver()
  58. {
  59. $driverName = ucfirst($this->config['driver']);
  60. $driverClass = sprintf('Intervention\\Image\\%s\\Driver', $driverName);
  61. if (class_exists($driverClass)) {
  62. return new $driverClass;
  63. }
  64. throw new \Exception("Driver ({$driverName}) could not be instantiated.");
  65. }
  66. }