-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresize_img.php
106 lines (96 loc) · 3.2 KB
/
resize_img.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
<?php
/*
Module developed for the Open Source Content Management System WebsiteBaker (http://websitebaker.org)
Copyright (C) 2007 - 2017, Christoph Marti
LICENCE TERMS:
This module is free software. You can redistribute it and/or modify it
under the terms of the GNU General Public License - version 2 or later,
as published by the Free Software Foundation: http://www.gnu.org/licenses/gpl.html.
DISCLAIMER:
This module is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
*/
// Resize PNG image
function resizePNG($source, $destination, $new_max_w, $new_max_h) {
// Check if GD is installed
if (extension_loaded('gd') AND function_exists('imagecreatefrompng')) {
// First figure out the size of the image
list($orig_w, $orig_h) = getimagesize($source);
if ($orig_w > $new_max_w) {
$new_w = $new_max_w;
$new_h = intval($orig_h * ($new_w / $orig_w));
if ($new_h > $new_max_h) {
$new_h = $new_max_h;
$new_w = intval($orig_w * ($new_h / $orig_h));
}
} else if ($orig_h > $new_max_h) {
$new_h = $new_max_h;
$new_w = intval($orig_w * ($new_h / $orig_h));
} else {
// Image to small to be downsized
echo "<div align='center'><p style='color: red;'>Image to small to be downsized!</p></div>";
return false;
}
// Now make the image
$source = imagecreatefrompng($source);
$dst_img = imagecreatetruecolor($new_w, $new_h);
// Allow png transparency (full alpha channel information)
imagealphablending($dst_img, false);
imagesavealpha($dst_img, true);
// Resizing
imagecopyresampled($dst_img, $source, 0,0,0,0, $new_w, $new_h, $orig_w, $orig_h);
imagepng($dst_img, $destination);
// Clear memory
imagedestroy($dst_img);
imagedestroy($source);
// Return true
return true;
} else {
return false;
}
}
// Resize JPEG image
function resizeJPEG($source, $new_max_w, $new_max_h, $quality = 75) {
if ($img = imagecreatefromjpeg($source)) {
$orig_w = imagesx($img);
$orig_h = imagesy($img);
$resize = FALSE;
$handle;
if ($orig_w > $new_max_w) {
$new_w = $new_max_w;
$new_h = intval($orig_h * ($new_w / $orig_w));
if ($new_h > $new_max_h) {
$new_h = $new_max_h;
$new_w = intval($orig_w * ($new_h / $orig_h));
}
$resize = TRUE;
} else if ($orig_h > $new_max_h) {
$new_h = $new_max_h;
$new_w = intval($orig_w * ($new_h / $orig_h));
$resize = TRUE;
} else {
// Image cant be downsized
echo "<div align='center'><p style='color: red;'>Image to small to be downsized!</p></div>";
return false;
}
if ($resize) {
// Resize using appropriate function
if (function_exists('imagecopyresampled')) {
$imageId = imagecreatetruecolor($new_w, $new_h);
imagecopyresampled($imageId, $img, 0,0,0,0, $new_w, $new_h, $orig_w, $orig_h);
} else {
$imageId = imagecreate($new_w , $new_h);
imagecopyresized($imageId, $img, 0,0,0,0, $new_w, $new_h, $orig_w, $orig_h);
}
$handle = $imageId;
// Free original image
imagedestroy($img);
} else {
$handle = $img;
}
imagejpeg($handle, $source, $quality);
imagedestroy($handle);
}
}