-
Notifications
You must be signed in to change notification settings - Fork 2
/
resize.php
130 lines (89 loc) · 2.66 KB
/
resize.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
<?php
$type = @$_GET['type'];
if ($type == '') die('SPECIFY type');
$file = @$_GET['file'];
if ($file == '') die('SPECIFY file');
$is_retina = @$_GET['retina'] == 'true';
if (isset($_GET['quality']) === false || strlen($_GET['quality']) === 0)
{
$_GET['quality'] = 75;
}
define('ROOT', realpath(dirname(__FILE__).'/../../../').'/');
function crop_image($path = '', $type = '', $retina = false)
{
$filename = substr($path, strpos($path, '/wp-content/'));
if (substr($filename, 0, 1) === '/') $filename = substr($filename, 1);
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$size = getimagesize(ROOT.$filename);
$image_width = round($size[0]);
$image_height = round($size[1]);
$ratio = $size[0] / $size[1];
switch ($type)
{
case 'wide':
$width = $image_width;
$height = $image_height;
if ($width > 1097)
{
$width = 1097;
$height = $width * $ratio;
}
break;
case 'desktop':
$width = round($image_width / 4 * 3);
$height = round($image_height / 4 * 3);
if ($width > 1097 / 4 * 3)
{
$width = round(1097 / 4 * 3);
$height = $width * $ratio;
}
break;
case 'tablet':
$width = round($image_width / 4 * 3);
$height = round($image_height / 4 * 3);
if ($width > 1097 / 4 * 3)
{
$width = round(1097 / 4 * 3);
$height = $width * $ratio;
}
break;
case 'mobile':
$width = round($image_width / 4 * 1.5);
$height = round($image_height / 4 * 1.5);
if ($width > 1097 / 4 * 1.5)
{
$width = round(1097 / 4 * 1.5);
$height = $width * $ratio;
}
break;
}
if ($retina == 'true')
{
$width = $width * 2;
$height = $height * 2;
}
$new_filename = str_replace('uploads/', 'uploads/cache/', str_replace('.'.$ext, '_w'.$width.'_h'.$height.'.'.$ext, $path));
$new_filename_path = ROOT.str_replace('uploads/', 'uploads/cache/', str_replace('.'.$ext, '_w'.$width.'_h'.$height.'.'.$ext, $filename));
$recreate = false;
if (file_exists($new_filename_path) === false || @filemtime($new_filename_path) - time() < -3600)
{
$recreate = true;
}
if ($recreate)
{
$dir = dirname($new_filename_path);
@mkdir($dir, 0777, true);
$editor = wp_get_image_editor( ROOT.$path );
$editor->resize( $width, $height, false );
$editor->set_quality($_GET['quality']);
$editor->save($new_filename_path);
}
return $new_filename;
}
if ( !isset($wp_did_header) ) {
$wp_did_header = true;
require_once( ROOT . 'wp-load.php' );
}
$source = crop_image($file, $type, $is_retina);
header('Location: '.get_bloginfo('url').'/'.$source);
exit();