-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimgresize.php
More file actions
75 lines (57 loc) · 1.93 KB
/
Copy pathimgresize.php
File metadata and controls
75 lines (57 loc) · 1.93 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
<?php
function img_resize($mode, $src, $dest, $width, $height, $quality=100)
{
$dest = getcwd(). $dest;
$src = getcwd() . "\\" . $src;
//make dir if it isnt exist
if (!file_exists('.\\images\\cache\\' . $_GET['preset-name'])) {
mkdir('.\\images\\cache\\' . $_GET['preset-name']);
}
if (!file_exists($src)) return false;
$size = getimagesize($src);
if ($size === false) return false;
$format = strtolower(substr($size['mime'], strpos($size['mime'], '/')+1));
$icfunc = "imagecreatefrom" . $format;
if (!function_exists($icfunc)) return false;
switch ($mode) {
case 'IN':
$x_ratio = $width / $size[0];
$y_ratio = $height / $size[1];
$ratio = min($x_ratio, $y_ratio);
$new_width = floor($size[0] * $ratio);
$new_height = floor($size[1] * $ratio);
$left_offset = 0;
$top_offset = 0;
break;
case 'OUT':
$x_ratio = $size[0] / $width;
$y_ratio = $size[1] / $height;
$ratio = min($x_ratio, $y_ratio);
$new_width = floor($width * $ratio);
$new_height = floor($height * $ratio);
$left_offset = ($size[0] - $new_width) / 2;
$top_offset = ($size[1] - $new_height) / 2;
$resize = true;
break;
case 'EXACT':
$left_offset = 0;
$top_offset = 0;
$new_width = $width;
$new_height = $height;
}
$isrc = $icfunc($src);
$idest = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($idest, $isrc, 0, 0, $left, $top,
$new_width, $new_height, $size[0]-$left_offset*2, $size[1]-$top_offset*2);
if ($resize) {
$resized = imagecreatetruecolor($width, $height);
imagecopyresampled($resized , $idest, 0, 0, 0, 0, $width, $height, $new_width, $new_height);
imagejpeg($resized, $dest, $quality);
imagedestroy($resized);
}
else if(!(imagejpeg($idest, $dest, $quality))) return false;
imagedestroy($isrc);
imagedestroy($idest);
return true;
}
?>