Issues (1191)

Security Analysis    not enabled

This project does not seem to handle request data directly as such no vulnerable execution paths were found.

  Cross-Site Scripting
Cross-Site Scripting enables an attacker to inject code into the response of a web-request that is viewed by other users. It can for example be used to bypass access controls, or even to take over other users' accounts.
  File Exposure
File Exposure allows an attacker to gain access to local files that he should not be able to access. These files can for example include database credentials, or other configuration files.
  File Manipulation
File Manipulation enables an attacker to write custom data to files. This potentially leads to injection of arbitrary code on the server.
  Object Injection
Object Injection enables an attacker to inject an object into PHP code, and can lead to arbitrary code execution, file exposure, or file manipulation attacks.
  Code Injection
Code Injection enables an attacker to execute arbitrary code on the server.
  Response Splitting
Response Splitting can be used to send arbitrary responses.
  File Inclusion
File Inclusion enables an attacker to inject custom files into PHP's file loading mechanism, either explicitly passed to include, or for example via PHP's auto-loading mechanism.
  Command Injection
Command Injection enables an attacker to inject a shell command that is execute with the privileges of the web-server. This can be used to expose sensitive data, or gain access of your server.
  SQL Injection
SQL Injection enables an attacker to execute arbitrary SQL code on your database server gaining access to user data, or manipulating user data.
  XPath Injection
XPath Injection enables an attacker to modify the parts of XML document that are read. If that XML document is for example used for authentication, this can lead to further vulnerabilities similar to SQL Injection.
  LDAP Injection
LDAP Injection enables an attacker to inject LDAP statements potentially granting permission to run unauthorized queries, or modify content inside the LDAP tree.
  Header Injection
  Other Vulnerability
This category comprises other attack vectors such as manipulating the PHP runtime, loading custom extensions, freezing the runtime, or similar.
  Regex Injection
Regex Injection enables an attacker to execute arbitrary code in your PHP process.
  XML Injection
XML Injection enables an attacker to read files on your local filesystem including configuration files, or can be abused to freeze your web-server process.
  Variable Injection
Variable Injection enables an attacker to overwrite program variables with custom data, and can lead to further vulnerabilities.
Unfortunately, the security analysis is currently not available for your project. If you are a non-commercial open-source project, please contact support to gain access.

src/Image/Image.php (16 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php namespace Anomaly\Streams\Platform\Image;
2
3
use Anomaly\FilesModule\File\Contract\FileInterface;
4
use Anomaly\FilesModule\File\FilePresenter;
5
use Anomaly\Streams\Platform\Addon\FieldType\FieldType;
6
use Anomaly\Streams\Platform\Application\Application;
7
use Anomaly\Streams\Platform\Routing\UrlGenerator;
8
use Collective\Html\HtmlBuilder;
9
use Illuminate\Contracts\Config\Repository;
10
use Illuminate\Filesystem\Filesystem;
11
use Illuminate\Http\Request;
12
use Intervention\Image\Constraint;
13
use Intervention\Image\ImageManager;
14
use League\Flysystem\File;
15
use League\Flysystem\MountManager;
16
use Mobile_Detect;
17
use Robbo\Presenter\Presenter;
18
19
/**
20
 * Class Image
21
 *
22
 * @link    http://pyrocms.com/
23
 * @author  PyroCMS, Inc. <[email protected]>
24
 * @author  Ryan Thompson <[email protected]>
25
 */
26
class Image
27
{
28
29
    /**
30
     * The publish flag.
31
     *
32
     * @var bool
33
     */
34
    protected $publish = false;
35
36
    /**
37
     * The publishable base directory.
38
     *
39
     * @var null
40
     */
41
    protected $directory = null;
42
43
    /**
44
     * The image object.
45
     *
46
     * @var null|string
47
     */
48
    protected $image = null;
49
50
    /**
51
     * The file extension.
52
     *
53
     * @var null|string
54
     */
55
    protected $extension = null;
56
57
    /**
58
     * The desired filename.
59
     *
60
     * @var null|string
61
     */
62
    protected $filename = null;
63
64
    /**
65
     * The original filename.
66
     *
67
     * @var null|string
68
     */
69
    protected $original = null;
70
71
    /**
72
     * The version flag.
73
     *
74
     * @var null|boolean
75
     */
76
    protected $version = null;
77
78
    /**
79
     * The default output method.
80
     *
81
     * @var string
82
     */
83
    protected $output = 'url';
84
85
    /**
86
     * The image attributes.
87
     *
88
     * @var array
89
     */
90
    protected $attributes = [];
91
92
    /**
93
     * Applied alterations.
94
     *
95
     * @var array
96
     */
97
    protected $alterations = [];
98
99
    /**
100
     * Image srcsets.
101
     *
102
     * @var array
103
     */
104
    protected $srcsets = [];
105
106
    /**
107
     * Image sources.
108
     *
109
     * @var array
110
     */
111
    protected $sources = [];
112
113
    /**
114
     * Allowed methods.
115
     *
116
     * @var array
117
     */
118
    protected $allowedMethods = [
119
        'blur',
120
        'brightness',
121
        'colorize',
122
        'resizeCanvas',
123
        'contrast',
124
        'crop',
125
        'encode',
126
        'fit',
127
        'flip',
128
        'gamma',
129
        'greyscale',
130
        'heighten',
131
        'insert',
132
        'interlace',
133
        'invert',
134
        'limitColors',
135
        'pixelate',
136
        'opacity',
137
        'resize',
138
        'rotate',
139
        'amount',
140
        'widen',
141
        'orientate',
142
    ];
143
144
    /**
145
     * The quality of the output.
146
     *
147
     * @var null|int
148
     */
149
    protected $quality = null;
150
151
    /**
152
     * The image width.
153
     *
154
     * @var null|int
155
     */
156
    protected $width = null;
157
158
    /**
159
     * The image height.
160
     *
161
     * @var null|int
162
     */
163
    protected $height = null;
164
165
    /**
166
     * The URL generator.
167
     *
168
     * @var UrlGenerator
169
     */
170
    protected $url;
171
172
    /**
173
     * The HTML builder.
174
     *
175
     * @var HtmlBuilder
176
     */
177
    protected $html;
178
179
    /**
180
     * Image path hints by namespace.
181
     *
182
     * @var ImagePaths
183
     */
184
    protected $paths;
185
186
    /**
187
     * The image macros.
188
     *
189
     * @var ImageMacros
190
     */
191
    protected $macros;
192
193
    /**
194
     * The file system.
195
     *
196
     * @var Filesystem
197
     */
198
    protected $files;
199
200
    /**
201
     * The user agent utility.
202
     *
203
     * @var Mobile_Detect
204
     */
205
    protected $agent;
206
207
    /**
208
     * The config repository.
209
     *
210
     * @var Repository
211
     */
212
    protected $config;
213
214
    /**
215
     * The request object.
216
     *
217
     * @var Request
218
     */
219
    protected $request;
220
221
    /**
222
     * The image manager.
223
     *
224
     * @var ImageManager
225
     */
226
    protected $manager;
227
228
    /**
229
     * The stream application.
230
     *
231
     * @var Application
232
     */
233
    protected $application;
234
235
    /**
236
     * Create a new Image instance.
237
     *
238
     * @param UrlGenerator  $url
239
     * @param HtmlBuilder   $html
240
     * @param Filesystem    $files
241
     * @param Mobile_Detect $agent
242
     * @param Repository    $config
243
     * @param ImageManager  $manager
244
     * @param Request       $request
245
     * @param Application   $application
246
     * @param ImagePaths    $paths
247
     * @param ImageMacros   $macros
248
     */
249 View Code Duplication
    public function __construct(
0 ignored issues
show
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
250
        UrlGenerator $url,
251
        HtmlBuilder $html,
252
        Filesystem $files,
253
        Mobile_Detect $agent,
254
        Repository $config,
255
        ImageManager $manager,
256
        Request $request,
0 ignored issues
show
You have injected the Request via parameter $request. This is generally not recommended as there might be multiple instances during a request cycle (f.e. when using sub-requests). Instead, it is recommended to inject the RequestStack and retrieve the current request each time you need it via getCurrentRequest().
Loading history...
257
        Application $application,
258
        ImagePaths $paths,
259
        ImageMacros $macros
260
    ) {
261
        $this->url         = $url;
262
        $this->html        = $html;
263
        $this->files       = $files;
264
        $this->agent       = $agent;
265
        $this->paths       = $paths;
266
        $this->config      = $config;
267
        $this->macros      = $macros;
268
        $this->manager     = $manager;
269
        $this->request     = $request;
270
        $this->application = $application;
271
    }
272
273
    /**
274
     * Make a new image instance.
275
     *
276
     * @param  mixed $image
277
     * @param  null  $output
278
     * @return $this
279
     */
280
    public function make($image, $output = null)
281
    {
282
        if ($image instanceof Image) {
283
            return $image;
284
        }
285
286
        if ($output) {
287
            $this->setOutput($output);
288
        }
289
290
        $clone = clone($this);
291
292
        $clone->setAlterations([]);
293
        $clone->setSources([]);
294
        $clone->setSrcsets([]);
295
        $clone->setImage(null);
296
297
        try {
298
            return $clone->setImage($image);
299
        } catch (\Exception $e) {
300
            return $this;
301
        }
302
    }
303
304
    /**
305
     * Return the path to an image.
306
     *
307
     * @return string
308
     */
309
    public function path()
310
    {
311
        $path = $this->getCachePath();
312
313
        return $this->request->getBasePath() . $path;
314
    }
315
316
    /**
317
     * Return the asset path to an image.
318
     *
319
     * @return string
320
     */
321
    public function asset()
322
    {
323
        $path = $this->getCachePath();
324
325
        return $this->url->asset($path);
326
    }
327
328
    /**
329
     * Run a macro on the image.
330
     *
331
     * @param $macro
332
     * @return Image
333
     * @throws \Exception
334
     */
335
    public function macro($macro)
336
    {
337
        return $this->macros->run($macro, $this);
338
    }
339
340
    /**
341
     * Return the URL to an image.
342
     *
343
     * @param  array $parameters
344
     * @param  null  $secure
345
     * @return string
346
     */
347
    public function url(array $parameters = [], $secure = null)
348
    {
349
        return $this->url->asset($this->getCachePath(), $parameters, $secure);
350
    }
351
352
    /**
353
     * Return the image tag to an image.
354
     *
355
     * @param  null  $alt
356
     * @param  array $attributes
357
     * @return string
358
     */
359
    public function image($alt = null, array $attributes = [])
360
    {
361
        $attributes = array_merge($this->getAttributes(), $attributes);
362
363
        $attributes['src'] = $this->asset();
364
365
        if ($srcset = $this->srcset()) {
366
            $attributes['srcset'] = $srcset;
367
        }
368
369
        if (!$alt && $this->config->get('streams::images.auto_alt', true)) {
370
371
            $attributes['alt'] = array_get(
372
                $this->getAttributes(),
373
                'alt',
374
                ucwords(
375
                    str_humanize(
376
                        trim(basename($this->getOriginal(), $this->getExtension()), '.'),
377
                        '^a-zA-Z0-9'
378
                    )
379
                )
380
            );
381
        }
382
383
        return '<img ' . $this->html->attributes($attributes) . '>';
384
    }
385
386
    /**
387
     * Return the image tag to an image.
388
     *
389
     * @param  null  $alt
390
     * @param  array $attributes
391
     * @return string
392
     */
393
    public function img($alt = null, array $attributes = [])
394
    {
395
        return $this->image($alt, $attributes);
396
    }
397
398
    /**
399
     * Return a picture tag.
400
     *
401
     * @return string
402
     */
403
    public function picture(array $attributes = [])
404
    {
405
        $sources = [];
406
407
        $attributes = array_merge($this->getAttributes(), $attributes);
408
409
        /* @var Image $image */
410
        foreach ($this->getSources() as $media => $image) {
411
            if ($media != 'fallback') {
412
                $sources[] = $image->source();
413
            } else {
414
                $sources[] = $image->image();
415
            }
416
        }
417
418
        $sources = implode("\n", $sources);
419
420
        $attributes = $this->html->attributes($attributes);
421
422
        return "<picture {$attributes}>\n{$sources}\n</picture>";
423
    }
424
425
    /**
426
     * Return a source tag.
427
     *
428
     * @return string
429
     */
430
    public function source()
431
    {
432
        $this->addAttribute('srcset', $this->srcset() ?: $this->asset() . ' 2x, ' . $this->asset() . ' 1x');
433
434
        $attributes = $this->html->attributes($this->getAttributes());
435
436
        if ($srcset = $this->srcset()) {
437
            $attributes['srcset'] = $srcset;
438
        }
439
440
        return "<source {$attributes}>";
441
    }
442
443
    /**
444
     * Return the image response.
445
     *
446
     * @param  null $format
447
     * @param  int  $quality
448
     * @return String
449
     */
450
    public function encode($format = null, $quality = null)
451
    {
452
        return $this->manager->make($this->getCachePath())->encode($format, $quality ?: $this->getQuality());
453
    }
454
455
    /**
456
     * Return the image contents.
457
     *
458
     * @return string
459
     */
460
    public function data()
461
    {
462
        return $this->dumpImage();
463
    }
464
465
    /**
466
     * Return the output.
467
     *
468
     * @return string
469
     */
470
    public function output()
471
    {
472
        return $this->{$this->output}();
473
    }
474
475
    /**
476
     * Set the filename.
477
     *
478
     * @param $filename
479
     * @return $this
480
     */
481
    public function rename($filename = null)
482
    {
483
        return $this->setFilename($filename);
484
    }
485
486
    /**
487
     * Set the version flag.
488
     *
489
     * @param bool $version
490
     * @return $this
491
     */
492
    public function version($version = true)
493
    {
494
        return $this->setVersion($version);
495
    }
496
497
    /**
498
     * Set the quality.
499
     *
500
     * @param $quality
501
     * @return $this
502
     */
503
    public function quality($quality)
504
    {
505
        return $this->setQuality($quality);
506
    }
507
508
    /**
509
     * Set the width attribute.
510
     *
511
     * @param  null $width
512
     * @return Image
513
     */
514
    public function width($width = null)
515
    {
516
        return $this->addAttribute('width', $width ?: $this->getWidth());
517
    }
518
519
    /**
520
     * Set the height attribute.
521
     *
522
     * @param  null $height
523
     * @return Image
524
     */
525
    public function height($height = null)
526
    {
527
        return $this->addAttribute('height', $height ?: $this->getHeight());
528
    }
529
530
    /**
531
     * Set the quality.
532
     *
533
     * @param $quality
534
     * @return $this
535
     */
536
    public function setQuality($quality)
537
    {
538
        $this->quality = (int)$quality;
539
540
        return $this;
541
    }
542
543
    /**
544
     * Get the cache path of the image.
545
     *
546
     * @return string
547
     */
548
    protected function getCachePath()
549
    {
550
        if (starts_with($this->getImage(), ['http://', 'https://', '//'])) {
551
            return $this->getImage();
552
        }
553
554
        if ($this->agent->isTablet()) {
555
            $this->macro('tablet_optimized');
556
        } elseif ($this->agent->isMobile()) {
557
            $this->macro('mobile_optimized');
558
        }
559
560
        $path = $this->paths->outputPath($this);
561
562
        try {
563
            if ($this->shouldPublish($path)) {
564
                $this->publish($path);
565
            }
566
        } catch (\Exception $e) {
567
            return $this->config->get('app.debug', false) ? $e->getMessage() : null;
568
        }
569
570
        if ($this->config->get('streams::images.version') || $this->getVersion() == true) {
571
            $path .= '?v=' . filemtime(public_path(trim($path, '/\\')));
572
        }
573
574
        return $path;
575
    }
576
577
    /**
578
     * Determine if the image needs to be published
579
     *
580
     * @param $path
581
     * @return bool
582
     */
583
    private function shouldPublish($path)
584
    {
585
        $path = ltrim($path, '/');
586
587
        if (!$this->files->exists($path)) {
588
            return true;
589
        }
590
591 View Code Duplication
        if (is_string($this->image) && !str_is('*://*', $this->image) && filemtime($path) < filemtime($this->image)) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
592
            return true;
593
        }
594
595 View Code Duplication
        if (is_string($this->image) && str_is('*://*', $this->image) && filemtime($path) < app(
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
596
                'League\Flysystem\MountManager'
597
            )->getTimestamp($this->image)
598
        ) {
599
            return true;
600
        }
601
602
        if ($this->image instanceof File && filemtime($path) < $this->image->getTimestamp()) {
603
            return true;
604
        }
605
606
        if ($this->image instanceof FileInterface && filemtime($path) < $this->image->lastModified()->format('U')) {
0 ignored issues
show
The class Anomaly\FilesModule\File\Contract\FileInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
607
            return true;
608
        }
609
610
        return false;
611
    }
612
613
    /**
614
     * Publish an image to the publish directory.
615
     *
616
     * @param $path
617
     */
618
    protected function publish($path)
619
    {
620
        $path = $this->directory . DIRECTORY_SEPARATOR . ltrim($path, '/');
621
622
        $this->files->makeDirectory((new \SplFileInfo($path))->getPath(), 0777, true, true);
623
624
        if (!$this->supportsType($this->getExtension())) {
625
626
            $this->files->put($path, $this->dumpImage());
627
628
            return;
629
        }
630
631
        if (!$image = $this->makeImage()) {
632
            return;
633
        }
634
635
        if (function_exists('exif_read_data') && $image->exif('Orientation') && $image->exif('Orientation') > 1) {
636
            $this->addAlteration('orientate');
637
        }
638
639
        if (in_array($this->getExtension(), ['jpeg', 'jpg']) && $this->config->get('streams::images.interlace')) {
640
            $this->addAlteration('interlace');
641
        }
642
643
        if (!$this->getAlterations() && !$this->getQuality() && $content = $this->dumpImage()) {
644
645
            $this->files->put($path, $content);
646
647
            return;
648
        }
649
650
        if (is_callable('exif_read_data') && in_array('orientate', $this->getAlterations())) {
651
            $this->setAlterations(array_unique(array_merge(['orientate'], $this->getAlterations())));
652
        }
653
654
        foreach ($this->getAlterations() as $method => $arguments) {
655
656
            if ($method == 'resize') {
657
                $this->guessResizeArguments($arguments);
658
            }
659
660
            if (in_array($method, $this->getAllowedMethods())) {
661
662
                if (is_array($arguments)) {
663
                    call_user_func_array([$image, $method], $arguments);
664
                } else {
665
                    call_user_func([$image, $method], $arguments);
666
                }
667
            }
668
        }
669
670
        $image->save($path, $this->getQuality());
671
    }
672
673
    /**
674
     * Set an attribute value.
675
     *
676
     * @param $attribute
677
     * @param $value
678
     * @return $this
679
     */
680
    public function attr($attribute, $value)
681
    {
682
        array_set($this->attributes, $attribute, $value);
683
684
        return $this;
685
    }
686
687
    /**
688
     * Return the image srcsets by set.
689
     *
690
     * @return array
691
     */
692
    public function srcset()
693
    {
694
        $sources = [];
695
696
        /* @var Image $image */
697
        foreach ($this->getSrcsets() as $descriptor => $image) {
698
            $sources[] = $image->asset() . ' ' . $descriptor;
699
        }
700
701
        return implode(', ', $sources);
702
    }
703
704
    /**
705
     * Set the srcsets/alterations.
706
     *
707
     * @param array $srcsets
708
     */
709
    public function srcsets(array $srcsets)
710
    {
711
        foreach ($srcsets as $descriptor => &$alterations) {
712
            $image = $this->make(array_pull($alterations, 'image', $this->getImage()))->setOutput('url');
713
714 View Code Duplication
            foreach ($alterations as $method => $arguments) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
715
                if (is_array($arguments)) {
716
                    call_user_func_array([$image, $method], $arguments);
717
                } else {
718
                    call_user_func([$image, $method], $arguments);
719
                }
720
            }
721
722
            $alterations = $image;
723
        }
724
725
        $this->setSrcsets($srcsets);
726
727
        return $this;
728
    }
729
730
    /**
731
     * Set the sources/alterations.
732
     *
733
     * @param  array $sources
734
     * @param  bool  $merge
735
     * @return $this
736
     */
737
    public function sources(array $sources, $merge = true)
738
    {
739
        foreach ($sources as $media => &$alterations) {
740
            if ($merge) {
741
                $alterations = array_merge($this->getAlterations(), $alterations);
742
            }
743
744
            $image = $this->make(array_pull($alterations, 'image', $this->getImage()))->setOutput('source');
745
746
            if ($media != 'fallback') {
747
                call_user_func([$image, 'media'], $media);
748
            }
749
750 View Code Duplication
            foreach ($alterations as $method => $arguments) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
751
                if (is_array($arguments)) {
752
                    call_user_func_array([$image, $method], $arguments);
753
                } else {
754
                    call_user_func([$image, $method], $arguments);
755
                }
756
            }
757
758
            $alterations = $image;
759
        }
760
761
        $this->setSources($sources);
762
763
        return $this;
764
    }
765
766
    /**
767
     * Alter the image based on the user agents.
768
     *
769
     * @param  array $agents
770
     * @param  bool  $exit
771
     * @return $this
772
     */
773
    public function agents(array $agents, $exit = false)
774
    {
775
        foreach ($agents as $agent => $alterations) {
776
            if (
777
                $this->agent->is($agent)
778
                || ($agent == 'phone' && $this->agent->isPhone())
779
                || ($agent == 'mobile' && $this->agent->isMobile())
780
                || ($agent == 'tablet' && $this->agent->isTablet())
781
                || ($agent == 'desktop' && $this->agent->isDesktop())
782
            ) {
783 View Code Duplication
                foreach ($alterations as $method => $arguments) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
784
                    if (is_array($arguments)) {
785
                        call_user_func_array([$this, $method], $arguments);
786
                    } else {
787
                        call_user_func([$this, $method], $arguments);
788
                    }
789
                }
790
791
                if ($exit) {
792
                    return $this;
793
                }
794
            }
795
        }
796
797
        return $this;
798
    }
799
800
    /**
801
     * Return if an extension is supported.
802
     *
803
     * @param $extension
804
     * @return bool
805
     */
806
    protected function supportsType($extension)
807
    {
808
        return !in_array($extension, ['svg', 'webp']);
809
    }
810
811
    /**
812
     * Set the image.
813
     *
814
     * @param  $image
815
     * @return $this
816
     */
817
    public function setImage($image)
818
    {
819
        if ($image instanceof Presenter) {
820
            $image = $image->getObject();
821
        }
822
823
        if ($image instanceof FieldType) {
824
            $image = $image->getValue();
825
        }
826
827
        // Replace path prefixes.
828
        if (is_string($image) && str_contains($image, '::')) {
829
            $image = $this->paths->realPath($image);
830
831
            $this->setOriginal(basename($image));
832
            $this->setExtension(pathinfo($image, PATHINFO_EXTENSION));
833
834
            $size = getimagesize($image);
835
836
            $this->setWidth(array_get($size, 0));
837
            $this->setHeight(array_get($size, 1));
838
        }
839
840
        if (is_string($image) && str_is('*://*', $image) && !starts_with($image, ['http', 'https'])) {
841
            $this->setOriginal(basename($image));
842
            $this->setExtension(pathinfo($image, PATHINFO_EXTENSION));
843
        }
844
845 View Code Duplication
        if ($image instanceof FileInterface) {
0 ignored issues
show
The class Anomaly\FilesModule\File\Contract\FileInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
846
847
            /* @var FileInterface $image */
848
            $this->setOriginal($image->getName());
849
            $this->setExtension($image->getExtension());
850
851
            $this->setWidth($image->getWidth());
852
            $this->setHeight($image->getHeight());
853
        }
854
855 View Code Duplication
        if ($image instanceof FilePresenter) {
0 ignored issues
show
The class Anomaly\FilesModule\File\FilePresenter does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
856
857
            /* @var FilePresenter|FileInterface $image */
858
            $image = $image->getObject();
859
860
            $this->setOriginal($image->getName());
861
            $this->setExtension($image->getExtension());
862
863
            $this->setWidth($image->getWidth());
864
            $this->setHeight($image->getHeight());
865
        }
866
867
        $this->image = $image;
868
869
        return $this;
870
    }
871
872
    /**
873
     * Make an image instance.
874
     *
875
     * @return \Intervention\Image\Image
876
     */
877
    protected function makeImage()
878
    {
879
        if ($this->image instanceof FileInterface) {
0 ignored issues
show
The class Anomaly\FilesModule\File\Contract\FileInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
880
            return $this->manager->make(app(MountManager::class)->read($this->image->location()));
881
        }
882
883
        if (is_string($this->image) && str_is('*://*', $this->image)) {
884
            return $this->manager->make(app(MountManager::class)->read($this->image));
885
        }
886
887
        if ($this->image instanceof File) {
888
            return $this->manager->make($this->image->read());
889
        }
890
891
        if (is_string($this->image) && file_exists($this->image)) {
892
            return $this->manager->make($this->image);
893
        }
894
895
        if ($this->image instanceof Image) {
896
            return $this->image;
897
        }
898
899
        return null;
900
    }
901
902
    /**
903
     * Dump an image instance's data.
904
     *
905
     * @return string
906
     */
907
    protected function dumpImage()
908
    {
909
        if ($this->image instanceof FileInterface) {
0 ignored issues
show
The class Anomaly\FilesModule\File\Contract\FileInterface does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
910
            return app('League\Flysystem\MountManager')->read($this->image->location());
911
        }
912
913 View Code Duplication
        if (is_string($this->image) && str_is('*://*', $this->image) && !starts_with($this->image, ['http', '//'])) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
914
            return app('League\Flysystem\MountManager')->read($this->image);
915
        }
916
917 View Code Duplication
        if (is_string($this->image) && (file_exists($this->image) || starts_with($this->image, ['http', '//']))) {
0 ignored issues
show
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
918
            return file_get_contents($this->image);
919
        }
920
921
        if (is_string($this->image) && file_exists($this->image)) {
922
            return file_get_contents($this->image);
923
        }
924
925
        if ($this->image instanceof File) {
926
            return $this->image->read();
927
        }
928
929
        if ($this->image instanceof Image) {
930
            return $this->image->encode();
931
        }
932
933
        if (is_string($this->image) && file_exists($this->image)) {
934
            return file_get_contents($this->image);
935
        }
936
937
        return null;
938
    }
939
940
    /**
941
     * Get the image instance.
942
     *
943
     * @return \Intervention\Image\Image
944
     */
945
    public function getImage()
946
    {
947
        return $this->image;
948
    }
949
950
    /**
951
     * Get the file name.
952
     *
953
     * @return null|string
954
     */
955
    public function getFilename()
956
    {
957
        return $this->filename;
958
    }
959
960
    /**
961
     * Set the file name.
962
     *
963
     * @param $filename
964
     * @return $this
965
     */
966
    public function setFilename($filename = null)
967
    {
968
        $this->filename = $filename;
969
970
        return $this;
971
    }
972
973
    /**
974
     * Get the original name.
975
     *
976
     * @return null|string
977
     */
978
    public function getOriginal()
979
    {
980
        return $this->original;
981
    }
982
983
    /**
984
     * Set the original name.
985
     *
986
     * @param $original
987
     * @return $this
988
     */
989
    public function setOriginal($original = null)
990
    {
991
        $this->original = $original;
992
993
        return $this;
994
    }
995
996
    /**
997
     * Get the file name.
998
     *
999
     * @return null|string
1000
     */
1001
    public function getVersion()
1002
    {
1003
        return $this->version;
1004
    }
1005
1006
    /**
1007
     * Set the file name.
1008
     *
1009
     * @param $version
1010
     * @return $this
1011
     */
1012
    public function setVersion($version = true)
1013
    {
1014
        $this->version = $version;
1015
1016
        return $this;
1017
    }
1018
1019
    /**
1020
     * Get the alterations.
1021
     *
1022
     * @return array
1023
     */
1024
    public function getAlterations()
1025
    {
1026
        return $this->alterations;
1027
    }
1028
1029
    /**
1030
     * Set the alterations.
1031
     *
1032
     * @param  array $alterations
1033
     * @return $this
1034
     */
1035
    public function setAlterations(array $alterations)
1036
    {
1037
        $this->alterations = $alterations;
1038
1039
        return $this;
1040
    }
1041
1042
    /**
1043
     * Add an alteration.
1044
     *
1045
     * @param  $method
1046
     * @param  $arguments
1047
     * @return $this
1048
     */
1049
    public function addAlteration($method, $arguments = [])
1050
    {
1051
        $this->alterations[$method] = $arguments;
1052
1053
        return $this;
1054
    }
1055
1056
    /**
1057
     * Get the attributes.
1058
     *
1059
     * @return array
1060
     */
1061
    public function getAttributes()
1062
    {
1063
        return $this->attributes;
1064
    }
1065
1066
    /**
1067
     * Set the attributes.
1068
     *
1069
     * @param  array $attributes
1070
     * @return $this
1071
     */
1072
    public function setAttributes(array $attributes)
1073
    {
1074
        $this->attributes = $attributes;
1075
1076
        return $this;
1077
    }
1078
1079
    /**
1080
     * Add an attribute.
1081
     *
1082
     * @param  $attribute
1083
     * @param  $value
1084
     * @return $this
1085
     */
1086
    protected function addAttribute($attribute, $value)
1087
    {
1088
        $this->attributes[$attribute] = $value;
1089
1090
        return $this;
1091
    }
1092
1093
    /**
1094
     * Get the srcsets.
1095
     *
1096
     * @return array
1097
     */
1098
    public function getSrcsets()
1099
    {
1100
        return $this->srcsets;
1101
    }
1102
1103
    /**
1104
     * Set the srcsets.
1105
     *
1106
     * @param  array $srcsets
1107
     * @return $this
1108
     */
1109
    public function setSrcsets(array $srcsets)
1110
    {
1111
        $this->srcsets = $srcsets;
1112
1113
        return $this;
1114
    }
1115
1116
    /**
1117
     * Get the sources.
1118
     *
1119
     * @return array
1120
     */
1121
    public function getSources()
1122
    {
1123
        return $this->sources;
1124
    }
1125
1126
    /**
1127
     * Set the sources.
1128
     *
1129
     * @param  array $sources
1130
     * @return $this
1131
     */
1132
    public function setSources(array $sources)
1133
    {
1134
        $this->sources = $sources;
1135
1136
        return $this;
1137
    }
1138
1139
    /**
1140
     * Get the quality.
1141
     *
1142
     * @param  null $default
1143
     * @return int
1144
     */
1145
    public function getQuality($default = null)
1146
    {
1147
        if (!$default) {
1148
            $default = $this->config->get('streams::images.quality', 80);
1149
        }
1150
1151
        return $this->quality ?: $default;
1152
    }
1153
1154
    /**
1155
     * Set the output mode.
1156
     *
1157
     * @param $output
1158
     * @return $this
1159
     */
1160
    public function setOutput($output)
1161
    {
1162
        $this->output = $output;
1163
1164
        return $this;
1165
    }
1166
1167
    /**
1168
     * Get the extension.
1169
     *
1170
     * @return null|string
1171
     */
1172
    public function getExtension()
1173
    {
1174
        return $this->extension;
1175
    }
1176
1177
    /**
1178
     * Set the extension.
1179
     *
1180
     * @param $extension
1181
     * @return $this
1182
     */
1183
    public function setExtension($extension)
1184
    {
1185
        $this->extension = $extension;
1186
1187
        return $this;
1188
    }
1189
1190
    /**
1191
     * Get the allowed methods.
1192
     *
1193
     * @return array
1194
     */
1195
    public function getAllowedMethods()
1196
    {
1197
        return $this->allowedMethods;
1198
    }
1199
1200
    /**
1201
     * Add a path by it's namespace hint.
1202
     *
1203
     * @param $namespace
1204
     * @param $path
1205
     * @return $this
1206
     */
1207
    public function addPath($namespace, $path)
1208
    {
1209
        $this->paths->addPath($namespace, $path);
1210
1211
        return $this;
1212
    }
1213
1214
1215
    /**
1216
     * Get the width.
1217
     *
1218
     * @return int|null
1219
     */
1220
    public function getWidth()
1221
    {
1222
        return $this->width;
1223
    }
1224
1225
    /**
1226
     * Set the width.
1227
     *
1228
     * @param $width
1229
     * @return $this
1230
     */
1231
    public function setWidth($width)
1232
    {
1233
        $this->width = $width;
1234
1235
        return $this;
1236
    }
1237
1238
    /**
1239
     * Get the height.
1240
     *
1241
     * @return int|null
1242
     */
1243
    public function getHeight()
1244
    {
1245
        return $this->height;
1246
    }
1247
1248
    /**
1249
     * Set the height.
1250
     *
1251
     * @param $height
1252
     * @return $this
1253
     */
1254
    public function setHeight($height)
1255
    {
1256
        $this->height = $height;
1257
1258
        return $this;
1259
    }
1260
1261
    /**
1262
     * Guess the resize callback value
1263
     * from a boolean.
1264
     *
1265
     * @param array $arguments
1266
     */
1267
    protected function guessResizeArguments(array &$arguments)
1268
    {
1269
        $arguments = array_pad($arguments, 3, null);
1270
1271
        if (end($arguments) instanceof \Closure) {
1272
            return;
1273
        }
1274
1275
        if (array_pop($arguments) !== false && (is_null($arguments[0]) || is_null($arguments[1]))) {
1276
            $arguments[] = function (Constraint $constraint) {
1277
                $constraint->aspectRatio();
1278
                $constraint->upsize();
1279
            };
1280
        }
1281
    }
1282
1283
    /**
1284
     * Set the public base directory.
1285
     *
1286
     * @param  $directory
1287
     * @return $this
1288
     */
1289
    public function setDirectory($directory)
1290
    {
1291
        $this->directory = $directory;
1292
1293
        return $this;
1294
    }
1295
1296
    /**
1297
     * Return the output.
1298
     *
1299
     * @return string
1300
     */
1301
    public function __toString()
1302
    {
1303
        return (string)$this->output();
1304
    }
1305
1306
    /**
1307
     * If the method does not exist then
1308
     * add an attribute and return.
1309
     *
1310
     * @param $name
1311
     * @param $arguments
1312
     * @return $this|mixed
1313
     */
1314
    public function __call($name, $arguments)
1315
    {
1316
        if (in_array($name, $this->getAllowedMethods())) {
1317
            return $this->addAlteration($name, $arguments);
1318
        }
1319
1320
        if ($this->macros->isMacro($macro = snake_case($name))) {
1321
            return $this->macro($macro);
1322
        }
1323
1324
        if (!method_exists($this, $name)) {
1325
            array_set($this->attributes, $name, array_shift($arguments));
1326
1327
            return $this;
1328
        }
1329
1330
        return call_user_func_array([$this, $name], $arguments);
1331
    }
1332
}
1333