-
Notifications
You must be signed in to change notification settings - Fork 23
/
Eve.php
223 lines (186 loc) · 6.07 KB
/
Eve.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
<?php
/*
* This file is part of SeAT
*
* Copyright (C) 2015 to present Leon Jacobs
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
namespace Seat\Services\Image;
use Seat\Services\Exceptions\EveImageException;
/**
* Class Eve.
*
* @package Seat\Services\Image
*/
class Eve
{
/**
* @var string
*/
protected $type;
/**
* @var string
*/
protected $variation;
/**
* @var int
*/
protected $id;
/**
* @var int
*/
protected $size;
/**
* @var array
*/
protected $attributes;
/**
* @var bool
*/
protected $lazy;
/**
* @var array
*/
protected $known_types = [
'characters', 'corporations', 'alliances', 'factions', 'types', 'render', 'auto', ];
/**
* @var string
*/
protected $img_server = '//images.evetech.net';
/**
* Eve constructor.
*
* @param string $type
* @param string $variation
* @param int $id
* @param int $size
* @param array $attr
* @param bool $lazy
*
* @throws \Seat\Services\Exceptions\EveImageException
*/
public function __construct(string $type, string $variation, ?int $id, int $size, array $attr = [], bool $lazy = true)
{
// Validate the arguments
if (! in_array($type, $this->known_types))
throw new EveImageException($type . ' is not a valid image type.');
if (! (is_int($id) || $id === null))
throw new EveImageException('id must be an integer or null.');
if (! is_int($size))
throw new EveImageException('size must be an integer');
if (! in_array($size, [32, 64, 128, 256, 512, 1024]))
throw new EveImageException('unsupported size');
// Check if we should detect the type based on id
if ($type == 'auto') {
$type = $this->detect_type($id);
switch ($type) {
case 'characters':
$this->variation = 'portrait';
break;
case 'corporations':
case 'alliances':
case 'factions':
$this->variation = 'logo';
break;
case 'unknown':
$this->variation = 'unknown';
break;
default:
$this->variation = 'icon';
}
}
// ccp trick - http://eveonline-third-party-documentation.readthedocs.io/en/latest/imageserver/intro.html#faction-images
if ($type == 'factions')
$type = 'alliances';
$this->type = $type;
$this->variation = $variation;
$this->id = $id;
$this->attributes = $attr;
$this->lazy = $lazy;
$this->size = $size;
}
/**
* Attempt to detect the image type based on the
* range in which an integer falls.
*
* @param $id
* @return string
*/
public function detect_type($id)
{
if ($id === null)
return 'unknown';
if ($id > 90000000 && $id < 98000000)
return 'characters';
elseif (($id > 98000000 && $id < 99000000) || ($id > 1000000 && $id < 2000000))
return 'corporations';
elseif (($id > 99000000 && $id < 100000000) || ($id > 0 && $id < 1000000))
return 'alliances';
return 'characters';
}
/**
* @return string
*/
public function html()
{
// make new IMG tag
$html = '<img ';
//if the id is unknown, we don't even need to generate the image server url
if ($this->id === null){
//there might be a better asset for this, or maybe we should create one
$html .= 'src="' . asset('web/img/bg.png') . '" ';
} else {
if ($this->lazy) {
// images are lazy loaded. prepare the the data-src attributes with the
// location for the image.
$html .= 'src="' . asset('web/img/bg.png') . '" ';
$html .= 'data-src="' . $this->url($this->size) . '" ';
// Item Type images can be max 64?
// http://imageserver.eveonline.com/Type/670_128.png goes 404
// In case requested size is greater than 32, lock size to 64
$html .= 'data-src-retina="' . $this->url($this->size == 1024 ? 1024 : $this->size * 2) . '" ';
// put class on images to lazy load them
if (! isset($this->attributes['class']))
$this->attributes['class'] = '';
$this->attributes['class'] .= ' img-lazy-load';
} else {
// no lazy loaded image
$html .= 'src="' . $this->url($this->size) . '" ';
}
}
// unset already built attributes
unset(
$this->attributes['src'],
$this->attributes['data-src='],
$this->attributes['data-src-retina']
);
// render other attributes
foreach ($this->attributes as $name => $value)
$html .= "{$name}=\"{$value}\" ";
// close IMG tag
$html .= ' />';
// return completed img tag
return $html;
}
/**
* @param $size
* @return string
*/
public function url($size)
{
return sprintf('%s/%s/%d/%s?size=%d', $this->img_server, $this->type, $this->id, $this->variation, $size);
}
}