-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSprite.php
More file actions
77 lines (59 loc) · 1.88 KB
/
Sprite.php
File metadata and controls
77 lines (59 loc) · 1.88 KB
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
<?php
namespace SCG\SpriteMapper;
class Sprite
{
private $original_height;
private $original_width;
public $fit = false;
private $height = 0;
private $width = 0;
private $scale = 1;
private $image_url;
private $image_resource;
public function __construct ( $sprite_url )
{
$this->image_url = $sprite_url;
$this->createImageResource();
}
public function __get ( $name )
{
return $this->{$name};
}
public function height ( $height = false )
{
$this->height = $height;
}
public function width ( $width = false )
{
$this->width = $width;
}
public function scaleHeight ( $height = false )
{
$this->scale = $height / $this->original_height;
$this->height = $height;
$this->width = $this->original_width * $this->scale;
}
public function scaleWidth ( $width = false )
{
$this->scale = $width / $this->original_width;
$this->width = $width;
$this->height = $this->original_height * $this->scale;
}
public function getResource ()
{
return $this->image_resource;
}
private function createImageResource ()
{
$image_contents = file_get_contents($this->image_url);
$res = imagecreatefromstring($image_contents);
$this->width = $this->original_width = imagesx($res);
$this->height = $this->original_height = imagesy($res);
$new = imagecreatetruecolor($this->original_width, $this->original_height);
imagecolortransparent($new, imagecolorallocatealpha($new, 0, 0, 0, 127));
imagealphablending($new, false);
imagesavealpha($new, true);
imagecopyresampled($new, $res, 0, 0, 0, 0, $this->original_width, $this->original_height, $this->original_width, $this->original_height);
$this->image_resource = $new;
}
}