-
Notifications
You must be signed in to change notification settings - Fork 0
/
functions.php
98 lines (88 loc) · 2.87 KB
/
functions.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
<?php
// Code based on a stripped down version of thus TutsPlus code:
// http://code.tutsplus.com/tutorials/image-resizing-made-easy-with-php--net-10362
Class resize {
private $image;
private $width;
private $height;
private $imageResized;
function __construct($fileName) {
$this->image = $this->openImage($fileName);
$this->width = imagesx($this->image);
$this->height = imagesy($this->image);
}
private function openImage($file) {
$extension = strtolower(strrchr($file, "."));
switch($extension) {
case ".jpg":
case ".jpeg":
$img = @imagecreatefromjpeg($file);
break;
case ".gif":
$img = @imagecreatefromgif($file);
break;
case ".png":
$img = @imagecreatefrompng($file);
break;
default:
$img = false;
break;
}
return $img;
}
public function resizeImage($newWidth, $newHeight) {
$optionArray = $this->getOptimalCrop($newWidth, $newHeight);
$optimalWidth = $optionArray["optimalWidth"];
$optimalHeight = $optionArray["optimalHeight"];
$this->imageResized = imagecreatetruecolor($optimalWidth, $optimalHeight);
imagecopyresampled($this->imageResized, $this->image, 0, 0, 0, 0, $optimalWidth, $optimalHeight, $this->width, $this->height);
$this->crop($optimalWidth, $optimalHeight, $newWidth, $newHeight);
}
private function getOptimalCrop($newWidth, $newHeight) {
$widthRatio = $this->width / $newWidth;
$heightRatio = $this->height / $newHeight;
if($heightRatio < $widthRatio) {
$optimalRatio = $heightRatio;
}
else {
$optimalRatio = $widthRatio;
}
$optimalWidth = $this->width / $optimalRatio;
$optimalHeight = $this->height / $optimalRatio;
return array("optimalWidth" => $optimalWidth, "optimalHeight" => $optimalHeight);
}
private function crop($optimalWidth, $optimalHeight, $newWidth, $newHeight) {
$cropStartX = ($optimalWidth / 2) - ($newWidth / 2);
$cropStartY = ($optimalHeight / 2) - ($newHeight / 2);
$crop = $this->imageResized;
$this->imageResized = imagecreatetruecolor($newWidth, $newHeight);
imagecopyresampled($this->imageResized, $crop, 0, 0, $cropStartX, $cropStartY, $newWidth, $newHeight, $newWidth, $newHeight);
}
public function grayscaleImage() {
imagefilter($this->imageResized, IMG_FILTER_GRAYSCALE);
}
public function saveImage($savePath, $imageQuality = 100) {
$extension = strtolower(strrchr($savePath, "."));
switch($extension) {
case ".jpg":
case ".jpeg":
if(imagetypes() & IMG_JPG) {
imagejpeg($this->imageResized, $savePath, $imageQuality);
}
break;
case ".gif":
if(imagetypes() & IMG_GIF) {
imagegif($this->imageResized, $savePath);
}
break;
case ".png":
$scaleQuality = round(($imageQuality / 100) * 9);
$invertScaleQuality = 9 - $scaleQuality;
if(imagetypes() & IMG_PNG) {
imagepng($this->imageResized, $savePath, $invertScaleQuality);
}
break;
}
imagedestroy($this->imageResized);
}
}