irishdan /
ResponsiveImageBundle
This project does not seem to handle request data directly as such no vulnerable execution paths were found.
include, or for example
via PHP's auto-loading mechanism.
These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * This file is part of the IrishDan\ResponsiveImageBundle package. |
||
| 4 | * |
||
| 5 | * (c) Daniel Byrne <[email protected]> |
||
| 6 | * |
||
| 7 | * For the full copyright and license information, please view the LICENSE file that was distributed with this source |
||
| 8 | * code. |
||
| 9 | */ |
||
| 10 | |||
| 11 | namespace IrishDan\ResponsiveImageBundle\ImageProcessing; |
||
| 12 | |||
| 13 | use IrishDan\ResponsiveImageBundle\Event\StyledImagesEvent; |
||
| 14 | use IrishDan\ResponsiveImageBundle\Event\StyledImagesEvents; |
||
| 15 | use IrishDan\ResponsiveImageBundle\FileSystem\PrimaryFileSystemWrapper; |
||
| 16 | use IrishDan\ResponsiveImageBundle\ResponsiveImageInterface; |
||
| 17 | use IrishDan\ResponsiveImageBundle\StyleManager; |
||
| 18 | use League\Flysystem\FilesystemInterface; |
||
| 19 | use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * Class ImageManager |
||
| 23 | * |
||
| 24 | * @package IrishDan\ResponsiveImageBundle\Image |
||
| 25 | */ |
||
| 26 | class ImageManager |
||
| 27 | { |
||
| 28 | /** |
||
| 29 | * @var StyleManager |
||
| 30 | */ |
||
| 31 | protected $styleManager; |
||
| 32 | protected $ImageStyler; |
||
| 33 | protected $fileSystem; |
||
| 34 | protected $temporaryFileSystem; |
||
| 35 | protected $eventDispatcher; |
||
| 36 | protected $generatedImages = []; |
||
| 37 | |||
| 38 | public function __construct( |
||
| 39 | StyleManager $styleManager, |
||
| 40 | ImageStyler $imageStyler, |
||
| 41 | PrimaryFileSystemWrapper $fileSystem, |
||
| 42 | FilesystemInterface $temporaryFileSystem = null, |
||
| 43 | EventDispatcherInterface $eventDispatcher = null |
||
| 44 | ) { |
||
| 45 | $this->styleManager = $styleManager; |
||
| 46 | $this->ImageStyler = $imageStyler; |
||
| 47 | $this->fileSystem = $fileSystem->getFileSystem(); |
||
| 48 | $this->temporaryFileSystem = $temporaryFileSystem; |
||
| 49 | $this->eventDispatcher = $eventDispatcher; |
||
| 50 | } |
||
| 51 | |||
| 52 | public function createAllStyledImages(ResponsiveImageInterface $image) |
||
| 53 | { |
||
| 54 | $styles = $this->styleManager->getAllStylesNames(); |
||
| 55 | |||
| 56 | return $this->createStyledImages($image, $styles); |
||
| 57 | } |
||
| 58 | |||
| 59 | public function createStyledImages(ResponsiveImageInterface $image, array $styles = []) |
||
| 60 | { |
||
| 61 | // Copy the image from its current filesystem onto the filesystem used by intervention. |
||
| 62 | $contents = $this->fileSystem->read($image->getPath()); |
||
| 63 | $this->temporaryFileSystem->put($image->getPath(), $contents); |
||
| 64 | |||
| 65 | // Generate all of the required files |
||
| 66 | foreach ($styles as $style) { |
||
| 67 | // @TODO: Use the relative part instead of the full path!!! |
||
| 68 | $this->createStyledImage($image, $style); |
||
| 69 | } |
||
| 70 | |||
| 71 | // Dispatch an event. |
||
| 72 | if (!empty($this->eventDispatcher)) { |
||
| 73 | $imagesGeneratedEvent = new StyledImagesEvent($image, $this->generatedImages); |
||
| 74 | $this->eventDispatcher->dispatch(StyledImagesEvents::STYLED_IMAGES_GENERATED, $imagesGeneratedEvent); |
||
| 75 | } |
||
| 76 | |||
| 77 | return $this->generatedImages; |
||
| 78 | } |
||
| 79 | |||
| 80 | protected function createStyledImage(ResponsiveImageInterface $image, $style) |
||
| 81 | { |
||
| 82 | $styleData = $this->styleManager->getStyleData($style); |
||
| 83 | |||
| 84 | $directory = $this->temporaryFileSystem->getAdapter()->getPathPrefix(); |
||
| 85 | $source = $directory . $image->getPath(); |
||
| 86 | |||
| 87 | if (!empty($styleData)) { |
||
| 88 | $cropFocusData = $image->getCropCoordinates(); |
||
| 89 | $relativeStylePath = $this->styleManager->getStylePath($image, $style); |
||
| 90 | |||
| 91 | $destination = $directory . $relativeStylePath; |
||
| 92 | |||
| 93 | // Intervention needs directories to exist prior to creating images. |
||
| 94 | $this->createStyleDirectory($relativeStylePath); |
||
| 95 | |||
| 96 | try { |
||
| 97 | $this->ImageStyler->createImage($source, $destination, $styleData, $cropFocusData); |
||
| 98 | $this->generatedImages[$style] = $relativeStylePath; |
||
| 99 | } catch (\Exception $e) { |
||
| 100 | // @TODO: Throw exception |
||
| 101 | } |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | public function deleteAllImages(ResponsiveImageInterface $image) |
||
| 106 | { |
||
| 107 | $this->deleteImage($image); |
||
| 108 | $this->deleteStyledImages($image); |
||
| 109 | } |
||
| 110 | |||
| 111 | public function deleteStyledImages(ResponsiveImageInterface $image, array $styles = []) |
||
| 112 | { |
||
| 113 | if (empty($styles)) { |
||
| 114 | $styles = $this->styleManager->getAllStylesNames(); |
||
| 115 | } |
||
| 116 | |||
| 117 | foreach ($styles as $style) { |
||
| 118 | $this->deleteImage($image, $style); |
||
| 119 | } |
||
| 120 | } |
||
| 121 | |||
| 122 | public function deleteImage(ResponsiveImageInterface $image, $style = '') |
||
| 123 | { |
||
| 124 | View Code Duplication | if (!empty($style)) { |
|
| 125 | $path = $this->styleManager->getStylePath($image, $style); |
||
| 126 | } |
||
| 127 | else { |
||
| 128 | $path = $image->getPath(); |
||
| 129 | } |
||
| 130 | $this->fileSystem->delete($path); |
||
| 131 | } |
||
| 132 | |||
| 133 | protected function createStyleDirectory($destination) |
||
| 134 | { |
||
| 135 | $filename = basename($destination); |
||
| 136 | $directory = explode($filename, $destination)[0]; |
||
| 137 | |||
| 138 | if (!$this->temporaryFileSystem->has($directory)) { |
||
| 139 | $this->temporaryFileSystem->createDir($directory); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | public function imageExists($path) |
||
| 144 | { |
||
| 145 | return $this->fileSystem->has($path); |
||
| 146 | } |
||
| 147 | |||
| 148 | public function createCustomStyledImage(ResponsiveImageInterface $image, $customStyleString, $forceGenerate = false) |
||
| 149 | { |
||
| 150 | // @TODO: To avoid creating images, that already exist, check if it exists, need a way to disable this checking |
||
| 151 | |||
| 152 | // check is it exists, using the string |
||
| 153 | $stylePath = $this->styleManager->getStylePath($image, $customStyleString); |
||
| 154 | |||
| 155 | $exists = $this->imageExists($stylePath); |
||
| 156 | if (!$exists || $forceGenerate) { |
||
| 157 | // Create the style array and add to the existing styles |
||
| 158 | $style = $this->styleManager->styleDataFromCustomStyleString($customStyleString); |
||
| 159 | $this->styleManager->addStyle($customStyleString, $style); |
||
| 160 | |||
| 161 | // generate the image |
||
| 162 | $this->createStyledImages($image, [$customStyleString]); |
||
| 163 | } |
||
| 164 | } |
||
| 165 | |||
| 166 | public function deleteCustomStyledImages(ResponsiveImageInterface $image) |
||
|
0 ignored issues
–
show
|
|||
| 167 | { |
||
| 168 | // @TODO: Implement |
||
| 169 | } |
||
| 170 | } |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.