-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimageEditor.php
More file actions
105 lines (96 loc) · 3.01 KB
/
Copy pathimageEditor.php
File metadata and controls
105 lines (96 loc) · 3.01 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
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
<?php
class imageEditor
{
var $url;
/**
* imageEditor constructor.
* @param $url
*/
public function __construct($url)
{
$this->url = $url;
}
/**
* @param int $x
* @param int $y
* @param $width
* @param $height
* @return string
* Image crop function
*/
function imageCrop($x = 0, $y = 0, $width, $height)
{
$action = 'crop' . '(' . $x . ',' . $y . ')' . $width . 'x' . $height;
if ($this->checkCache($action)) {
return 'cache/' . $this->url . $action;
} else {
$image_type = getimagesize("https://codex-images.s3.amazonaws.com/" . $this->url)[2];
if ($image_type == IMAGETYPE_JPEG) {
$im = imagecreatefromjpeg("https://codex-images.s3.amazonaws.com/" . $this->url);
} else {
$im = imagecreatefrompng("https://codex-images.s3.amazonaws.com/" . $this->url);
}
$im2 = imagecrop($im, ['x' => $x, 'y' => $y, 'width' => $width, 'height' => $height]);
$this->doCache($im2, 'crop' . '(' . $x . ',' . $y . ')' . $width . 'x' . $height);
return 'cache/' . $this->url . 'crop' . '(' . $x . ',' . $y . ')' . $width . 'x' . $height;
}
}
/**
* @param $width
* @param $height
* @return string
* Image resize function
*/
function imageResize($width, $height)
{
$action = 'resize' . $width . 'x' . $height;
if ($this->checkCache($action)) {
return 'cache/' . $this->url . $action;
} else {
$image_type = getimagesize("https://codex-images.s3.amazonaws.com/" . $this->url)[2];
if ($image_type == IMAGETYPE_JPEG) {
$im = imagecreatefromjpeg("https://codex-images.s3.amazonaws.com/" . $this->url);
} else {
$im = imagecreatefrompng("https://codex-images.s3.amazonaws.com/" . $this->url);
}
$im2 = imagecreatetruecolor($width, $height);
imagecopyresized($im2, $im, 0, 0, 0, 0, $width, $height, imagesx($im), imagesy($im));
$this->doCache($im2, $action);
return 'cache/' . $this->url . $action;
}
}
/**
* @return string
* Returns full image
*/
function imageFull()
{
if ($this->checkCache('full')) {
} else {
file_put_contents('cache/' . $this->url . 'full', file_get_contents("https://codex-images.s3.amazonaws.com/" . $this->url));
}
return 'cache/' . $this->url . 'full';
}
/**
* @param $im
* @param $action
* Function caching image
*/
function doCache($im, $action)
{
imagepng($im, 'cache/' . $this->url . $action);
}
/**
* @param $action
* @return bool
* function checking if image already cached
*/
function checkCache($action)
{
if (file_exists('cache/' . $this->url . $action)) {
return true;
} else {
return false;
}
}
}