-
Notifications
You must be signed in to change notification settings - Fork 25
/
MF_thumb.php
192 lines (153 loc) · 5.63 KB
/
MF_thumb.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
<?php
/*
Magic Fields Thumb class
Paramters
---------
w: width
h: height
zc: zoom crop (0 or 1)
q: quality (default is 75 and max is 100)
*/
class mfthumb{
function mfthumb(){
require_once(ABSPATH."/wp-admin/includes/image.php");
require_once(ABSPATH."/wp-includes/media.php");
}
/**
* This function is almost equal to the image_resize (native function of wordpress)
*/
function image_resize( $file, $max_w, $max_h, $crop = false, $far = false, $iar = false, $dest_path = null, $jpeg_quality = 90 ) {
$image = wp_get_image_editor( $file );
if ( is_wp_error( $image ) )
return $image;
$size = @getimagesize( $file );
if ( !$size )
return new WP_Error('invalid_image', __('Could not read image size'), $file);
list($orig_w, $orig_h, $orig_type) = $size;
$dims = mf_image_resize_dimensions($orig_w, $orig_h, $max_w, $max_h, $crop, $far, $iar);
if ( !$dims ){
$dims = array(0,0,0,0,$orig_w,$orig_h,$orig_w,$orig_h);
}
list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims;
$newimage = imagecreatetruecolor( $dst_w, $dst_h );
imagealphablending($newimage, false);
imagesavealpha($newimage, true);
$transparent = imagecolorallocatealpha($newimage, 255, 255, 255, 127);
imagefilledrectangle($newimage, 0, 0, $dst_w, $dst_h, $transparent);
@ini_set( 'memory_limit', apply_filters( 'image_memory_limit', WP_MAX_MEMORY_LIMIT ) );
$imageTmp = imagecreatefromstring( file_get_contents( $file ) );
imagecopyresampled( $newimage, $imageTmp, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
// convert from full colors to index colors, like original PNG.
if ( IMAGETYPE_PNG == $orig_type && !imageistruecolor( $image ) )
imagetruecolortopalette( $newimage, false, imagecolorstotal( $image ) );
// we don't need the original in memory anymore
imagedestroy( $imageTmp );
$info = pathinfo($dest_path);
$dir = $info['dirname'];
$ext = $info['extension'];
$name = basename($dest_path, ".{$ext}");
$destfilename = "{$dir}/{$name}.{$ext}";
if ( IMAGETYPE_GIF == $orig_type ) {
if ( !imagegif( $newimage, $destfilename ) )
return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
} elseif ( IMAGETYPE_PNG == $orig_type ) {
if ( !imagepng( $newimage, $destfilename ) )
return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
} else {
// all other formats are converted to jpg
//Todo: add option for use progresive JPG
//imageinterlace($newimage, true); //Progressive JPG
if ( !imagejpeg( $newimage, $destfilename, apply_filters( 'jpeg_quality', $jpeg_quality, 'image_resize' ) ) )
return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
}
imagedestroy( $newimage );
// Set correct file permissions
$stat = stat( dirname( $destfilename ));
$perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits
@ chmod( $destfilename, $perms );
return $destfilename;
}
}
/**
* Based in the image_resize_dimensions of wordpress
*/
function mf_image_resize_dimensions($orig_w, $orig_h, $dest_w, $dest_h, $crop = false, $far = false, $iar = false) {
if ($orig_w <= 0 || $orig_h <= 0)
return false;
// at least one of dest_w or dest_h must be specific
if ($dest_w <= 0 && $dest_h <= 0)
return false;
if ( $crop ) {
// crop the largest possible portion of the original image that we can size to $dest_w x $dest_h
$aspect_ratio = $orig_w / $orig_h;
$new_w = min($dest_w, $orig_w);
$new_h = min($dest_h, $orig_h);
if ( !$new_w ) {
$new_w = intval($new_h * $aspect_ratio);
}
if ( !$new_h ) {
$new_h = intval($new_w / $aspect_ratio);
}
$size_ratio = max($new_w / $orig_w, $new_h / $orig_h);
$crop_w = round($new_w / $size_ratio);
$crop_h = round($new_h / $size_ratio);
$s_x = floor( ($orig_w - $crop_w) / 2 );
$s_y = floor( ($orig_h - $crop_h) / 2 );
} else {
// don't crop, just resize using $dest_w x $dest_h as a maximum bounding box
$crop_w = $dest_w;
$crop_h = $dest_h;
$s_x = 0;
$s_y = 0;
$new_w = $crop_w;
$new_h = $crop_h;
}
if( $far ) {
switch ( $far ) {
case 'L':
case 'TL':
case 'BL':
$s_x = 0;
$s_y = round(($dest_h - $origin_h) / 2);
break;
case 'R':
case 'TR':
case 'BR':
$s_x = round($dest_w - $origin_w);
$s_y = round(($dest_h - $origin_h) / 2);
break;
case 'T':
case 'TL':
case 'TR':
$s_x = round(($dest_w - $origin_w) / 2);
$s_y = 0;
break;
case 'B':
case 'BL':
case 'BR':
$s_x = round(($dest_w - $origin_w) / 2);
$s_y = round($dest_h - $origin_h);
break;
case 'C':
default:
$s_x = round(($dest_w - $origin_w) / 2);
$s_y = round(($dest_h - $origin_h) / 2);
}
}
if ( $iar ) {
//ignore aspect radio and resize the image
$crop_w = $orig_w;
$crop_h = $orig_h;
$s_x = 0;
$s_y = 0;
$new_w = ceil($orig_w * $dest_w / $orig_w);
$new_h = ceil($orig_h * $dest_h / $orig_h);
}
// if the resulting image would be the same size we don't want to resize it
if ( $new_w == $orig_w && $new_h == $orig_h )
return false;
// the return array matches the parameters to imagecopyresampled()
// int dst_x, int dst_y, int src_x, int src_y, int dst_w, int dst_h, int src_w, int src_h
return array( 0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h );
}
?>