-
Notifications
You must be signed in to change notification settings - Fork 0
/
resize-image.php
168 lines (112 loc) · 3.44 KB
/
resize-image.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
<?php
// New Dimensions
$newWidth = 500;
$newHeight = 350;
// Min Dimensions - Keep the minimum of the new dimensions
$minDimensions = true;
// Exact Dimensions - Keep the exact new dimensions, cropping is required
$exactDimensions = true;
// Image Quality
$quality = 100;
// Image Path
$imagePath = __DIR__ . "/banner.jpg";
$newImagePath = __DIR__ . "/banner-new.jpg";
// Obtain Image
$image = null;
$extension = strtolower( strrchr( $imagePath, '.' ) );
switch( $extension ) {
// JPEG
case '.jpg':
case '.jpeg': {
$image = @imagecreatefromjpeg( $imagePath );
break;
}
// GIF
case '.gif': {
$image = @imagecreatefromgif( $imagePath );
break;
}
// PNG
case '.png': {
$image = @imagecreatefrompng( $imagePath );
break;
}
}
if( empty( $image ) ) {
die( "Provide a valid image." );
}
// Get the Image dimensions
$width = imagesx( $image );
$height = imagesy( $image );
// Image Aspect Ratio
$aspectRatio = $width / $height;
// Get the optimal Image dimensions
$optimalWidth = $aspectRatio >= 1 ? $newWidth : ($aspectRatio * $newHeight);
$optimalHeight = $aspectRatio <= 1 ? $newHeight : ($newWidth / $aspectRatio);
// Keep the minimum of the new dimensions
if( $minDimensions ) {
$heightRatio = $height / $newHeight;
$widthRatio = $width / $newWidth;
if( $heightRatio < $widthRatio ) {
$optimalRatio = $heightRatio;
}
else {
$optimalRatio = $widthRatio;
}
$optimalWidth = $width / $optimalRatio;
$optimalHeight = $height / $optimalRatio;
}
// Stats
echo "Width: $width Height: $height \n";
echo "Optimal Width: $optimalWidth Optimal Height: $optimalHeight \n";
// Optimal Dimensions using New Dimensions
$tempImage = imagecreatetruecolor( $optimalWidth, $optimalHeight );
if( $extension == '.png' ) {
imagealphablending( $tempImage, false );
imagesavealpha( $tempImage, true );
$transparent = imagecolorallocatealpha( $tempImage, 255, 255, 255, 127 );
imagefilledrectangle( $tempImage, 0, 0, $optimalWidth, $optimalHeight, $transparent );
}
imagecopyresampled( $tempImage, $image, 0, 0, 0, 0, $optimalWidth, $optimalHeight, $width, $height );
// Crop to get exact dimensions
if( $exactDimensions ) {
// Crop Position
$cropStartX = ( $optimalWidth / 2) - ( $newWidth / 2 );
$cropStartY = ( $optimalHeight / 2) - ( $newHeight / 2 );
// Crop the temporary image
$crop = $tempImage;
// Crop from center
$tempImage = imagecreatetruecolor( $newWidth, $newHeight );
if( $extension == '.png' ) {
imagealphablending( $tempImage, false );
imagesavealpha( $tempImage, true );
$transparent = imagecolorallocatealpha( $tempImage, 255, 255, 255, 127 );
imagefilledrectangle( $tempImage, 0, 0, $newWidth, $newWidth, $transparent );
}
imagecopyresampled( $tempImage, $crop, 0, 0, $cropStartX, $cropStartY, $newWidth, $newHeight, $newWidth, $newHeight );
}
// Save New Image
switch( $extension ) {
case '.jpg':
case '.jpeg': {
if( imagetypes() & IMG_JPG ) {
imagejpeg( $tempImage, $newImagePath, $quality );
}
break;
}
case '.gif': {
if( imagetypes() & IMG_GIF ) {
imagegif( $tempImage, $newImagePath );
}
break;
}
case '.png': {
$scale = round( ( $quality / 100 ) * 9 );
$invert = 9 - $scale;
if( imagetypes() & IMG_PNG ) {
imagepng( $tempImage, $newImagePath, $invert );
}
break;
}
}
imagedestroy( $tempImage );