Imagy.php 5.42 KB
Newer Older
1
<?php namespace Modules\Media\Image;
Nicolas Widart's avatar
Nicolas Widart committed
2

3
use Illuminate\Contracts\Config\Repository;
Nicolas Widart's avatar
Nicolas Widart committed
4
use Illuminate\Support\Facades\App;
5
use Modules\Media\Entities\File;
Nicolas Widart's avatar
Nicolas Widart committed
6

7
class Imagy
Nicolas Widart's avatar
Nicolas Widart committed
8 9
{
    /**
10
     * @var \Intervention\Image\Image
Nicolas Widart's avatar
Nicolas Widart committed
11 12 13
     */
    private $image;
    /**
14
     * @var \Illuminate\Filesystem\Filesystem
Nicolas Widart's avatar
Nicolas Widart committed
15 16
     */
    private $finder;
17 18 19 20
    /**
     * @var ImageFactoryInterface
     */
    private $imageFactory;
21 22 23 24
    /**
     * @var ThumbnailsManager
     */
    private $manager;
Nicolas Widart's avatar
Nicolas Widart committed
25

26 27 28 29
    /**
     * All the different images types where thumbnails should be created
     * @var array
     */
30
    private $imageExtensions = ['jpg', 'png', 'jpeg', 'gif'];
31 32 33 34
    /**
     * @var Repository
     */
    private $config;
35 36 37 38

    /**
     * @param ImageFactoryInterface $imageFactory
     * @param ThumbnailsManager $manager
39
     * @param Repository $config
40
     */
41
    public function __construct(ImageFactoryInterface $imageFactory, ThumbnailsManager $manager, Repository $config)
Nicolas Widart's avatar
Nicolas Widart committed
42 43 44
    {
        $this->image = App::make('Intervention\Image\ImageManager');
        $this->finder = App::make('Illuminate\Filesystem\Filesystem');
45
        $this->imageFactory = $imageFactory;
46
        $this->manager = $manager;
47
        $this->config = $config;
Nicolas Widart's avatar
Nicolas Widart committed
48 49
    }

Nicolas Widart's avatar
Nicolas Widart committed
50 51 52 53 54 55 56
    /**
     * Get an image in the given thumbnail options
     * @param string $path
     * @param string $thumbnail
     * @param bool $forceCreate
     * @return string
     */
57
    public function get($path, $thumbnail, $forceCreate = false)
Nicolas Widart's avatar
Nicolas Widart committed
58
    {
59 60 61
        if (!$this->isImage($path)) {
            return;
        }
62

63
        $filename = $this->config->get('media::config.files-path') . $this->newFilename($path, $thumbnail);
64 65 66

        if ($this->returnCreatedFile($filename, $forceCreate)) {
            return $filename;
67
        }
68

69
        $this->makeNew($path, $filename, $thumbnail);
Nicolas Widart's avatar
Nicolas Widart committed
70

71
        return $filename;
Nicolas Widart's avatar
Nicolas Widart committed
72 73
    }

74 75 76 77 78 79 80 81
    /**
     * Return the thumbnail path
     * @param string $originalImage
     * @param string $thumbnail
     * @return string
     */
    public function getThumbnail($originalImage, $thumbnail)
    {
82 83 84
        if (!$this->isImage($originalImage)) {
            return $originalImage;
        }
85

86
        return $this->config->get('media::config.files-path') . $this->newFilename($originalImage, $thumbnail);
87 88
    }

Nicolas Widart's avatar
Nicolas Widart committed
89 90 91 92
    /**
     * Create all thumbnails for the given image path
     * @param string $path
     */
93 94
    public function createAll($path)
    {
95 96 97
        if (!$this->isImage($path)) {
            return;
        }
98

99 100
        foreach ($this->manager->all() as $thumbName => $filters) {
            $image = $this->image->make(public_path() . $path);
101
            $filename = $this->config->get('media::config.files-path') . $this->newFilename($path, $thumbName);
102 103 104 105 106 107 108 109
            foreach ($filters as $manipulation => $options) {
                $image = $this->imageFactory->make($manipulation)->handle($image, $options);
            }
            $image = $image->encode(pathinfo($path, PATHINFO_EXTENSION));
            $this->writeImage($filename, $image);
        }
    }

Nicolas Widart's avatar
Nicolas Widart committed
110 111 112 113 114 115 116 117 118 119 120 121
    /**
     * Prepend the thumbnail name to filename
     * @param $path
     * @param $thumbnail
     * @return mixed|string
     */
    private function newFilename($path, $thumbnail)
    {
        $filename = pathinfo($path, PATHINFO_FILENAME);

        return $filename . '_' . $thumbnail . '.' . pathinfo($path, PATHINFO_EXTENSION);
    }
122 123 124 125 126 127 128 129 130 131 132

    /**
     * Return the already created file if it exists and force create is false
     * @param string $filename
     * @param bool $forceCreate
     * @return bool
     */
    private function returnCreatedFile($filename, $forceCreate)
    {
        return $this->finder->isFile(public_path() . $filename) && !$forceCreate;
    }
133 134 135 136 137 138 139 140 141 142

    /**
     * Write the given image
     * @param string $filename
     * @param string $image
     */
    private function writeImage($filename, $image)
    {
        $this->finder->put(public_path() . $filename, $image);
    }
143 144 145 146 147

    /**
     * Make a new image
     * @param string $path
     * @param string $filename
148
     * @param string null $thumbnail
149
     */
150
    private function makeNew($path, $filename, $thumbnail)
151 152 153
    {
        $image = $this->image->make(public_path() . $path);

154
        foreach ($this->manager->find($thumbnail) as $manipulation => $options) {
155 156 157 158 159 160
            $image = $this->imageFactory->make($manipulation)->handle($image, $options);
        }

        $image = $image->encode(pathinfo($path, PATHINFO_EXTENSION));
        $this->writeImage($filename, $image);
    }
161

162 163 164 165 166
    /**
     * Check if the given path is en image
     * @param string $path
     * @return bool
     */
167
    public function isImage($path)
168 169 170 171
    {
        return in_array(pathinfo($path, PATHINFO_EXTENSION), $this->imageExtensions);
    }

172 173 174 175 176 177
    /**
     * Delete all files on disk for the given file in storage
     * This means the original and the thumbnails
     * @param $file
     * @return bool
     */
178
    public function deleteAllFor(File $file)
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194
    {
        if (!$this->isImage($file->path)) {
            return $this->finder->delete($file->path);
        }

        $paths[] = public_path() . $file->path;
        $fileName = pathinfo($file->path, PATHINFO_FILENAME);
        $extension = pathinfo($file->path, PATHINFO_EXTENSION);
        foreach ($this->manager->all() as $thumbnail => $filters) {
            $paths[] = public_path() . $this->config->get(
                    'media::config.files-path'
                ) . "{$fileName}_{$thumbnail}.{$extension}";
        }
        return $this->finder->delete($paths);
    }

Nicolas Widart's avatar
Nicolas Widart committed
195
}