ImageAsset::getImageUrl()   A
last analyzed

Complexity

Conditions 5
Paths 4

Size

Total Lines 20
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 5
eloc 12
c 0
b 0
f 0
nc 4
nop 3
dl 0
loc 20
rs 9.5555
1
<?php
2
namespace Phpsa\Datastore\Ams;
3
use Phpsa\Datastore\Asset;
4
5
use Illuminate\Support\Facades\Storage;
6
use Illuminate\Support\Facades\View;
7
use Intervention\Image\Facades\Image;
8
9
class ImageAsset extends Asset {
10
11
    public $type = ImageAsset::class;
12
	public $namespace = 'property';
13
14
	public static function getImageUrl($filename, $width = null, $height = null){
15
16
		$imagePath = public_path().'/vendor/phpsa-datastore/';
17
18
		if ($width || $height) {
19
			$path = 'thumbs/' . $width .  'x' . $height  . '.' . $filename;
20
21
			if(!is_file($imagePath . $path)){
22
				try{
23
					Image::make(public_path().'/vendor/phpsa-datastore/img/' . $filename)->resize($width,$height, function ($constraint) {
24
						$constraint->aspectRatio();
25
					})->save($imagePath . $path);
26
				}catch(Exception $e){
0 ignored issues
show
Bug introduced by
The type Phpsa\Datastore\Ams\Exception was not found. Did you mean Exception? If so, make sure to prefix the type with \.
Loading history...
27
					//Silent fallback as the image does not exist!
28
				}
29
			}
30
		}else{
31
			$path = 'img/' . $filename;
32
		}
33
		return '/vendor/phpsa-datastore/' . $path;
34
35
36
37
	}
38
39
    public static function html($data) {
40
41
		return '<img src="' . self::getImageUrl($data['value']) . '" />';
42
    }
43
44
45
46
}
47