-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompressImage.php
More file actions
52 lines (31 loc) · 1.18 KB
/
Copy pathCompressImage.php
File metadata and controls
52 lines (31 loc) · 1.18 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
<?php
class CompressImage {
private static $quality = 75;
private static $outputPath = null;
private static $outputPrefix = "image";
private static $outputImageName = null;
public static function setQuality(int $quality) {
self::$quality = $quality;
}
public static function setOutputPath(string $path) {
self::$outputImageName = self::$outputPrefix . "-" . uniqid() . ".jpg";
self::$outputPath = trim($path, "/") . "/" . self::$outputImageName;
}
static function setOutputPrefix(string $prefix) {
self::$outputPrefix = $prefix;
}
public static function compress($source) {
$info = getimagesize($source);
if ($info['mime'] == 'image/jpeg')
$image = imagecreatefromjpeg($source);
elseif ($info['mime'] == 'image/gif')
$image = imagecreatefromgif($source);
elseif ($info['mime'] == 'image/png')
$image = imagecreatefrompng($source);
imagejpeg($image, self::$outputPath, self::$quality);
return array(
"path" => self::$outputPath,
"name" => self::$outputImageName
);
}
}