author | ymh <ymh.work@gmail.com> |
Mon, 14 Oct 2019 18:30:03 +0200 | |
changeset 10 | 372f2766ea20 |
parent 9 | 177826044cd9 |
child 16 | a86126ab1dd4 |
permissions | -rw-r--r-- |
0 | 1 |
<?php |
2 |
/** |
|
3 |
* File contains all the administration image manipulation functions. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Administration |
|
7 |
*/ |
|
8 |
||
9 |
/** |
|
10 |
* Crop an Image to a given size. |
|
11 |
* |
|
12 |
* @since 2.1.0 |
|
13 |
* |
|
14 |
* @param string|int $src The source file or Attachment ID. |
|
15 |
* @param int $src_x The start x position to crop from. |
|
16 |
* @param int $src_y The start y position to crop from. |
|
17 |
* @param int $src_w The width to crop. |
|
18 |
* @param int $src_h The height to crop. |
|
19 |
* @param int $dst_w The destination width. |
|
20 |
* @param int $dst_h The destination height. |
|
21 |
* @param int $src_abs Optional. If the source crop points are absolute. |
|
22 |
* @param string $dst_file Optional. The destination file to write to. |
|
23 |
* @return string|WP_Error New filepath on success, WP_Error on failure. |
|
24 |
*/ |
|
25 |
function wp_crop_image( $src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) { |
|
26 |
$src_file = $src; |
|
27 |
if ( is_numeric( $src ) ) { // Handle int as attachment ID |
|
28 |
$src_file = get_attached_file( $src ); |
|
29 |
||
30 |
if ( ! file_exists( $src_file ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
31 |
// If the file doesn't exist, attempt a URL fopen on the src link. |
0 | 32 |
// This can occur with certain file replication plugins. |
33 |
$src = _load_image_to_edit_path( $src, 'full' ); |
|
34 |
} else { |
|
35 |
$src = $src_file; |
|
36 |
} |
|
37 |
} |
|
38 |
||
39 |
$editor = wp_get_image_editor( $src ); |
|
9 | 40 |
if ( is_wp_error( $editor ) ) { |
0 | 41 |
return $editor; |
9 | 42 |
} |
0 | 43 |
|
44 |
$src = $editor->crop( $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs ); |
|
9 | 45 |
if ( is_wp_error( $src ) ) { |
0 | 46 |
return $src; |
9 | 47 |
} |
0 | 48 |
|
9 | 49 |
if ( ! $dst_file ) { |
50 |
$dst_file = str_replace( wp_basename( $src_file ), 'cropped-' . wp_basename( $src_file ), $src_file ); |
|
51 |
} |
|
0 | 52 |
|
5 | 53 |
/* |
54 |
* The directory containing the original file may no longer exist when |
|
55 |
* using a replication plugin. |
|
56 |
*/ |
|
0 | 57 |
wp_mkdir_p( dirname( $dst_file ) ); |
58 |
||
9 | 59 |
$dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), wp_basename( $dst_file ) ); |
0 | 60 |
|
61 |
$result = $editor->save( $dst_file ); |
|
9 | 62 |
if ( is_wp_error( $result ) ) { |
0 | 63 |
return $result; |
9 | 64 |
} |
0 | 65 |
|
66 |
return $dst_file; |
|
67 |
} |
|
68 |
||
69 |
/** |
|
70 |
* Generate post thumbnail attachment meta data. |
|
71 |
* |
|
72 |
* @since 2.1.0 |
|
73 |
* |
|
74 |
* @param int $attachment_id Attachment Id to process. |
|
75 |
* @param string $file Filepath of the Attached image. |
|
76 |
* @return mixed Metadata for attachment. |
|
77 |
*/ |
|
78 |
function wp_generate_attachment_metadata( $attachment_id, $file ) { |
|
79 |
$attachment = get_post( $attachment_id ); |
|
80 |
||
9 | 81 |
$metadata = array(); |
82 |
$support = false; |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
83 |
$mime_type = get_post_mime_type( $attachment ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
84 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
85 |
if ( preg_match( '!^image/!', $mime_type ) && file_is_displayable_image( $file ) ) { |
9 | 86 |
$imagesize = getimagesize( $file ); |
87 |
$metadata['width'] = $imagesize[0]; |
|
0 | 88 |
$metadata['height'] = $imagesize[1]; |
89 |
||
5 | 90 |
// Make the file path relative to the upload dir. |
9 | 91 |
$metadata['file'] = _wp_relative_upload_path( $file ); |
0 | 92 |
|
5 | 93 |
// Make thumbnails and other intermediate sizes. |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
94 |
$_wp_additional_image_sizes = wp_get_additional_image_sizes(); |
0 | 95 |
|
96 |
$sizes = array(); |
|
97 |
foreach ( get_intermediate_image_sizes() as $s ) { |
|
9 | 98 |
$sizes[ $s ] = array( |
99 |
'width' => '', |
|
100 |
'height' => '', |
|
101 |
'crop' => false, |
|
102 |
); |
|
103 |
if ( isset( $_wp_additional_image_sizes[ $s ]['width'] ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
104 |
// For theme-added sizes |
9 | 105 |
$sizes[ $s ]['width'] = intval( $_wp_additional_image_sizes[ $s ]['width'] ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
106 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
107 |
// For default sizes set in options |
9 | 108 |
$sizes[ $s ]['width'] = get_option( "{$s}_size_w" ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
109 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
110 |
|
9 | 111 |
if ( isset( $_wp_additional_image_sizes[ $s ]['height'] ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
112 |
// For theme-added sizes |
9 | 113 |
$sizes[ $s ]['height'] = intval( $_wp_additional_image_sizes[ $s ]['height'] ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
114 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
115 |
// For default sizes set in options |
9 | 116 |
$sizes[ $s ]['height'] = get_option( "{$s}_size_h" ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
117 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
118 |
|
9 | 119 |
if ( isset( $_wp_additional_image_sizes[ $s ]['crop'] ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
120 |
// For theme-added sizes |
9 | 121 |
$sizes[ $s ]['crop'] = $_wp_additional_image_sizes[ $s ]['crop']; |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
122 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
123 |
// For default sizes set in options |
9 | 124 |
$sizes[ $s ]['crop'] = get_option( "{$s}_crop" ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
125 |
} |
0 | 126 |
} |
127 |
||
5 | 128 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
129 |
* Filters the image sizes automatically generated when uploading an image. |
5 | 130 |
* |
131 |
* @since 2.9.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
132 |
* @since 4.4.0 Added the `$metadata` argument. |
9 | 133 |
* @since 5.1.0 Added the `$attachment_id` argument. |
5 | 134 |
* |
9 | 135 |
* @param array $sizes An associative array of image sizes. |
136 |
* @param array $metadata An associative array of image metadata: width, height, file. |
|
137 |
* @param int $attachment_id Current attachment ID. |
|
5 | 138 |
*/ |
9 | 139 |
$sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes, $metadata, $attachment_id ); |
0 | 140 |
|
141 |
if ( $sizes ) { |
|
142 |
$editor = wp_get_image_editor( $file ); |
|
143 |
||
9 | 144 |
if ( ! is_wp_error( $editor ) ) { |
0 | 145 |
$metadata['sizes'] = $editor->multi_resize( $sizes ); |
9 | 146 |
} |
0 | 147 |
} else { |
148 |
$metadata['sizes'] = array(); |
|
149 |
} |
|
150 |
||
5 | 151 |
// Fetch additional metadata from EXIF/IPTC. |
0 | 152 |
$image_meta = wp_read_image_metadata( $file ); |
9 | 153 |
if ( $image_meta ) { |
0 | 154 |
$metadata['image_meta'] = $image_meta; |
9 | 155 |
} |
5 | 156 |
} elseif ( wp_attachment_is( 'video', $attachment ) ) { |
0 | 157 |
$metadata = wp_read_video_metadata( $file ); |
9 | 158 |
$support = current_theme_supports( 'post-thumbnails', 'attachment:video' ) || post_type_supports( 'attachment:video', 'thumbnail' ); |
5 | 159 |
} elseif ( wp_attachment_is( 'audio', $attachment ) ) { |
0 | 160 |
$metadata = wp_read_audio_metadata( $file ); |
9 | 161 |
$support = current_theme_supports( 'post-thumbnails', 'attachment:audio' ) || post_type_supports( 'attachment:audio', 'thumbnail' ); |
0 | 162 |
} |
163 |
||
164 |
if ( $support && ! empty( $metadata['image']['data'] ) ) { |
|
5 | 165 |
// Check for existing cover. |
9 | 166 |
$hash = md5( $metadata['image']['data'] ); |
167 |
$posts = get_posts( |
|
168 |
array( |
|
169 |
'fields' => 'ids', |
|
170 |
'post_type' => 'attachment', |
|
171 |
'post_mime_type' => $metadata['image']['mime'], |
|
172 |
'post_status' => 'inherit', |
|
173 |
'posts_per_page' => 1, |
|
174 |
'meta_key' => '_cover_hash', |
|
175 |
'meta_value' => $hash, |
|
176 |
) |
|
177 |
); |
|
5 | 178 |
$exists = reset( $posts ); |
179 |
||
180 |
if ( ! empty( $exists ) ) { |
|
181 |
update_post_meta( $attachment_id, '_thumbnail_id', $exists ); |
|
182 |
} else { |
|
183 |
$ext = '.jpg'; |
|
184 |
switch ( $metadata['image']['mime'] ) { |
|
9 | 185 |
case 'image/gif': |
186 |
$ext = '.gif'; |
|
187 |
break; |
|
188 |
case 'image/png': |
|
189 |
$ext = '.png'; |
|
190 |
break; |
|
5 | 191 |
} |
9 | 192 |
$basename = str_replace( '.', '-', wp_basename( $file ) ) . '-image' . $ext; |
5 | 193 |
$uploaded = wp_upload_bits( $basename, '', $metadata['image']['data'] ); |
194 |
if ( false === $uploaded['error'] ) { |
|
195 |
$image_attachment = array( |
|
196 |
'post_mime_type' => $metadata['image']['mime'], |
|
9 | 197 |
'post_type' => 'attachment', |
198 |
'post_content' => '', |
|
5 | 199 |
); |
200 |
/** |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
201 |
* Filters the parameters for the attachment thumbnail creation. |
5 | 202 |
* |
203 |
* @since 3.9.0 |
|
204 |
* |
|
205 |
* @param array $image_attachment An array of parameters to create the thumbnail. |
|
206 |
* @param array $metadata Current attachment metadata. |
|
207 |
* @param array $uploaded An array containing the thumbnail path and url. |
|
208 |
*/ |
|
209 |
$image_attachment = apply_filters( 'attachment_thumbnail_args', $image_attachment, $metadata, $uploaded ); |
|
210 |
||
211 |
$sub_attachment_id = wp_insert_attachment( $image_attachment, $uploaded['file'] ); |
|
212 |
add_post_meta( $sub_attachment_id, '_cover_hash', $hash ); |
|
213 |
$attach_data = wp_generate_attachment_metadata( $sub_attachment_id, $uploaded['file'] ); |
|
214 |
wp_update_attachment_metadata( $sub_attachment_id, $attach_data ); |
|
215 |
update_post_meta( $attachment_id, '_thumbnail_id', $sub_attachment_id ); |
|
216 |
} |
|
0 | 217 |
} |
9 | 218 |
} elseif ( 'application/pdf' === $mime_type ) { |
219 |
// Try to create image thumbnails for PDFs. |
|
220 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
221 |
$fallback_sizes = array( |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
222 |
'thumbnail', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
223 |
'medium', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
224 |
'large', |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
225 |
); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
226 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
227 |
/** |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
228 |
* Filters the image sizes generated for non-image mime types. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
229 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
230 |
* @since 4.7.0 |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
231 |
* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
232 |
* @param array $fallback_sizes An array of image size names. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
233 |
* @param array $metadata Current attachment metadata. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
234 |
*/ |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
235 |
$fallback_sizes = apply_filters( 'fallback_intermediate_image_sizes', $fallback_sizes, $metadata ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
236 |
|
9 | 237 |
$sizes = array(); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
238 |
$_wp_additional_image_sizes = wp_get_additional_image_sizes(); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
239 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
240 |
foreach ( $fallback_sizes as $s ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
241 |
if ( isset( $_wp_additional_image_sizes[ $s ]['width'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
242 |
$sizes[ $s ]['width'] = intval( $_wp_additional_image_sizes[ $s ]['width'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
243 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
244 |
$sizes[ $s ]['width'] = get_option( "{$s}_size_w" ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
245 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
246 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
247 |
if ( isset( $_wp_additional_image_sizes[ $s ]['height'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
248 |
$sizes[ $s ]['height'] = intval( $_wp_additional_image_sizes[ $s ]['height'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
249 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
250 |
$sizes[ $s ]['height'] = get_option( "{$s}_size_h" ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
251 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
252 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
253 |
if ( isset( $_wp_additional_image_sizes[ $s ]['crop'] ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
254 |
$sizes[ $s ]['crop'] = $_wp_additional_image_sizes[ $s ]['crop']; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
255 |
} else { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
256 |
// Force thumbnails to be soft crops. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
257 |
if ( 'thumbnail' !== $s ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
258 |
$sizes[ $s ]['crop'] = get_option( "{$s}_crop" ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
259 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
260 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
261 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
262 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
263 |
// Only load PDFs in an image editor if we're processing sizes. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
264 |
if ( ! empty( $sizes ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
265 |
$editor = wp_get_image_editor( $file ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
266 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
267 |
if ( ! is_wp_error( $editor ) ) { // No support for this type of file |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
268 |
/* |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
269 |
* PDFs may have the same file filename as JPEGs. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
270 |
* Ensure the PDF preview image does not overwrite any JPEG images that already exist. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
271 |
*/ |
9 | 272 |
$dirname = dirname( $file ) . '/'; |
273 |
$ext = '.' . pathinfo( $file, PATHINFO_EXTENSION ); |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
274 |
$preview_file = $dirname . wp_unique_filename( $dirname, wp_basename( $file, $ext ) . '-pdf.jpg' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
275 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
276 |
$uploaded = $editor->save( $preview_file, 'image/jpeg' ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
277 |
unset( $editor ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
278 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
279 |
// Resize based on the full size image, rather than the source. |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
280 |
if ( ! is_wp_error( $uploaded ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
281 |
$editor = wp_get_image_editor( $uploaded['path'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
282 |
unset( $uploaded['path'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
283 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
284 |
if ( ! is_wp_error( $editor ) ) { |
9 | 285 |
$metadata['sizes'] = $editor->multi_resize( $sizes ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
286 |
$metadata['sizes']['full'] = $uploaded; |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
287 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
288 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
289 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
290 |
} |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
291 |
} |
5 | 292 |
|
293 |
// Remove the blob of binary data from the array. |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
294 |
if ( $metadata ) { |
5 | 295 |
unset( $metadata['image']['data'] ); |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
296 |
} |
0 | 297 |
|
5 | 298 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
299 |
* Filters the generated attachment meta data. |
5 | 300 |
* |
301 |
* @since 2.1.0 |
|
302 |
* |
|
303 |
* @param array $metadata An array of attachment meta data. |
|
304 |
* @param int $attachment_id Current attachment ID. |
|
305 |
*/ |
|
0 | 306 |
return apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id ); |
307 |
} |
|
308 |
||
309 |
/** |
|
310 |
* Convert a fraction string to a decimal. |
|
311 |
* |
|
312 |
* @since 2.5.0 |
|
313 |
* |
|
314 |
* @param string $str |
|
315 |
* @return int|float |
|
316 |
*/ |
|
9 | 317 |
function wp_exif_frac2dec( $str ) { |
0 | 318 |
@list( $n, $d ) = explode( '/', $str ); |
9 | 319 |
if ( ! empty( $d ) ) { |
0 | 320 |
return $n / $d; |
9 | 321 |
} |
0 | 322 |
return $str; |
323 |
} |
|
324 |
||
325 |
/** |
|
326 |
* Convert the exif date format to a unix timestamp. |
|
327 |
* |
|
328 |
* @since 2.5.0 |
|
329 |
* |
|
330 |
* @param string $str |
|
331 |
* @return int |
|
332 |
*/ |
|
9 | 333 |
function wp_exif_date2ts( $str ) { |
334 |
@list( $date, $time ) = explode( ' ', trim( $str ) ); |
|
335 |
@list( $y, $m, $d ) = explode( ':', $date ); |
|
0 | 336 |
|
337 |
return strtotime( "{$y}-{$m}-{$d} {$time}" ); |
|
338 |
} |
|
339 |
||
340 |
/** |
|
341 |
* Get extended image metadata, exif or iptc as available. |
|
342 |
* |
|
343 |
* Retrieves the EXIF metadata aperture, credit, camera, caption, copyright, iso |
|
344 |
* created_timestamp, focal_length, shutter_speed, and title. |
|
345 |
* |
|
346 |
* The IPTC metadata that is retrieved is APP13, credit, byline, created date |
|
347 |
* and time, caption, copyright, and title. Also includes FNumber, Model, |
|
348 |
* DateTimeDigitized, FocalLength, ISOSpeedRatings, and ExposureTime. |
|
349 |
* |
|
350 |
* @todo Try other exif libraries if available. |
|
351 |
* @since 2.5.0 |
|
352 |
* |
|
353 |
* @param string $file |
|
354 |
* @return bool|array False on failure. Image metadata array on success. |
|
355 |
*/ |
|
356 |
function wp_read_image_metadata( $file ) { |
|
9 | 357 |
if ( ! file_exists( $file ) ) { |
0 | 358 |
return false; |
9 | 359 |
} |
0 | 360 |
|
9 | 361 |
list( , , $image_type ) = @getimagesize( $file ); |
0 | 362 |
|
5 | 363 |
/* |
364 |
* EXIF contains a bunch of data we'll probably never need formatted in ways |
|
365 |
* that are difficult to use. We'll normalize it and just extract the fields |
|
366 |
* that are likely to be useful. Fractions and numbers are converted to |
|
367 |
* floats, dates to unix timestamps, and everything else to strings. |
|
368 |
*/ |
|
0 | 369 |
$meta = array( |
9 | 370 |
'aperture' => 0, |
371 |
'credit' => '', |
|
372 |
'camera' => '', |
|
373 |
'caption' => '', |
|
0 | 374 |
'created_timestamp' => 0, |
9 | 375 |
'copyright' => '', |
376 |
'focal_length' => 0, |
|
377 |
'iso' => 0, |
|
378 |
'shutter_speed' => 0, |
|
379 |
'title' => '', |
|
380 |
'orientation' => 0, |
|
381 |
'keywords' => array(), |
|
0 | 382 |
); |
383 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
384 |
$iptc = array(); |
5 | 385 |
/* |
386 |
* Read IPTC first, since it might contain data not available in exif such |
|
387 |
* as caption, description etc. |
|
388 |
*/ |
|
0 | 389 |
if ( is_callable( 'iptcparse' ) ) { |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
390 |
@getimagesize( $file, $info ); |
0 | 391 |
|
392 |
if ( ! empty( $info['APP13'] ) ) { |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
393 |
$iptc = @iptcparse( $info['APP13'] ); |
0 | 394 |
|
5 | 395 |
// Headline, "A brief synopsis of the caption." |
396 |
if ( ! empty( $iptc['2#105'][0] ) ) { |
|
0 | 397 |
$meta['title'] = trim( $iptc['2#105'][0] ); |
9 | 398 |
/* |
399 |
* Title, "Many use the Title field to store the filename of the image, |
|
400 |
* though the field may be used in many ways." |
|
401 |
*/ |
|
5 | 402 |
} elseif ( ! empty( $iptc['2#005'][0] ) ) { |
0 | 403 |
$meta['title'] = trim( $iptc['2#005'][0] ); |
5 | 404 |
} |
0 | 405 |
|
406 |
if ( ! empty( $iptc['2#120'][0] ) ) { // description / legacy caption |
|
407 |
$caption = trim( $iptc['2#120'][0] ); |
|
5 | 408 |
|
409 |
mbstring_binary_safe_encoding(); |
|
410 |
$caption_length = strlen( $caption ); |
|
411 |
reset_mbstring_encoding(); |
|
412 |
||
413 |
if ( empty( $meta['title'] ) && $caption_length < 80 ) { |
|
0 | 414 |
// Assume the title is stored in 2:120 if it's short. |
5 | 415 |
$meta['title'] = $caption; |
0 | 416 |
} |
5 | 417 |
|
418 |
$meta['caption'] = $caption; |
|
0 | 419 |
} |
420 |
||
9 | 421 |
if ( ! empty( $iptc['2#110'][0] ) ) { // credit |
0 | 422 |
$meta['credit'] = trim( $iptc['2#110'][0] ); |
9 | 423 |
} elseif ( ! empty( $iptc['2#080'][0] ) ) { // creator / legacy byline |
0 | 424 |
$meta['credit'] = trim( $iptc['2#080'][0] ); |
9 | 425 |
} |
0 | 426 |
|
9 | 427 |
if ( ! empty( $iptc['2#055'][0] ) && ! empty( $iptc['2#060'][0] ) ) { // created date and time |
0 | 428 |
$meta['created_timestamp'] = strtotime( $iptc['2#055'][0] . ' ' . $iptc['2#060'][0] ); |
9 | 429 |
} |
0 | 430 |
|
9 | 431 |
if ( ! empty( $iptc['2#116'][0] ) ) { // copyright |
0 | 432 |
$meta['copyright'] = trim( $iptc['2#116'][0] ); |
9 | 433 |
} |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
434 |
|
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
435 |
if ( ! empty( $iptc['2#025'][0] ) ) { // keywords array |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
436 |
$meta['keywords'] = array_values( $iptc['2#025'] ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
437 |
} |
9 | 438 |
} |
0 | 439 |
} |
440 |
||
9 | 441 |
$exif = array(); |
442 |
||
5 | 443 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
444 |
* Filters the image types to check for exif data. |
5 | 445 |
* |
446 |
* @since 2.5.0 |
|
447 |
* |
|
448 |
* @param array $image_types Image types to check for exif data. |
|
449 |
*/ |
|
9 | 450 |
$exif_image_types = apply_filters( 'wp_read_image_metadata_types', array( IMAGETYPE_JPEG, IMAGETYPE_TIFF_II, IMAGETYPE_TIFF_MM ) ); |
451 |
||
452 |
if ( is_callable( 'exif_read_data' ) && in_array( $image_type, $exif_image_types ) ) { |
|
0 | 453 |
$exif = @exif_read_data( $file ); |
454 |
||
5 | 455 |
if ( ! empty( $exif['ImageDescription'] ) ) { |
456 |
mbstring_binary_safe_encoding(); |
|
457 |
$description_length = strlen( $exif['ImageDescription'] ); |
|
458 |
reset_mbstring_encoding(); |
|
0 | 459 |
|
5 | 460 |
if ( empty( $meta['title'] ) && $description_length < 80 ) { |
0 | 461 |
// Assume the title is stored in ImageDescription |
462 |
$meta['title'] = trim( $exif['ImageDescription'] ); |
|
5 | 463 |
} |
464 |
||
465 |
if ( empty( $meta['caption'] ) && ! empty( $exif['COMPUTED']['UserComment'] ) ) { |
|
466 |
$meta['caption'] = trim( $exif['COMPUTED']['UserComment'] ); |
|
467 |
} |
|
468 |
||
469 |
if ( empty( $meta['caption'] ) ) { |
|
0 | 470 |
$meta['caption'] = trim( $exif['ImageDescription'] ); |
471 |
} |
|
5 | 472 |
} elseif ( empty( $meta['caption'] ) && ! empty( $exif['Comments'] ) ) { |
0 | 473 |
$meta['caption'] = trim( $exif['Comments'] ); |
474 |
} |
|
475 |
||
5 | 476 |
if ( empty( $meta['credit'] ) ) { |
477 |
if ( ! empty( $exif['Artist'] ) ) { |
|
478 |
$meta['credit'] = trim( $exif['Artist'] ); |
|
9 | 479 |
} elseif ( ! empty( $exif['Author'] ) ) { |
5 | 480 |
$meta['credit'] = trim( $exif['Author'] ); |
481 |
} |
|
482 |
} |
|
0 | 483 |
|
5 | 484 |
if ( empty( $meta['copyright'] ) && ! empty( $exif['Copyright'] ) ) { |
0 | 485 |
$meta['copyright'] = trim( $exif['Copyright'] ); |
5 | 486 |
} |
487 |
if ( ! empty( $exif['FNumber'] ) ) { |
|
0 | 488 |
$meta['aperture'] = round( wp_exif_frac2dec( $exif['FNumber'] ), 2 ); |
5 | 489 |
} |
490 |
if ( ! empty( $exif['Model'] ) ) { |
|
0 | 491 |
$meta['camera'] = trim( $exif['Model'] ); |
5 | 492 |
} |
493 |
if ( empty( $meta['created_timestamp'] ) && ! empty( $exif['DateTimeDigitized'] ) ) { |
|
494 |
$meta['created_timestamp'] = wp_exif_date2ts( $exif['DateTimeDigitized'] ); |
|
495 |
} |
|
496 |
if ( ! empty( $exif['FocalLength'] ) ) { |
|
0 | 497 |
$meta['focal_length'] = (string) wp_exif_frac2dec( $exif['FocalLength'] ); |
5 | 498 |
} |
499 |
if ( ! empty( $exif['ISOSpeedRatings'] ) ) { |
|
0 | 500 |
$meta['iso'] = is_array( $exif['ISOSpeedRatings'] ) ? reset( $exif['ISOSpeedRatings'] ) : $exif['ISOSpeedRatings']; |
501 |
$meta['iso'] = trim( $meta['iso'] ); |
|
502 |
} |
|
5 | 503 |
if ( ! empty( $exif['ExposureTime'] ) ) { |
0 | 504 |
$meta['shutter_speed'] = (string) wp_exif_frac2dec( $exif['ExposureTime'] ); |
5 | 505 |
} |
506 |
if ( ! empty( $exif['Orientation'] ) ) { |
|
507 |
$meta['orientation'] = $exif['Orientation']; |
|
508 |
} |
|
0 | 509 |
} |
510 |
||
511 |
foreach ( array( 'title', 'caption', 'credit', 'copyright', 'camera', 'iso' ) as $key ) { |
|
5 | 512 |
if ( $meta[ $key ] && ! seems_utf8( $meta[ $key ] ) ) { |
0 | 513 |
$meta[ $key ] = utf8_encode( $meta[ $key ] ); |
5 | 514 |
} |
0 | 515 |
} |
516 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
517 |
foreach ( $meta['keywords'] as $key => $keyword ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
518 |
if ( ! seems_utf8( $keyword ) ) { |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
519 |
$meta['keywords'][ $key ] = utf8_encode( $keyword ); |
5 | 520 |
} |
521 |
} |
|
522 |
||
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
523 |
$meta = wp_kses_post_deep( $meta ); |
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
524 |
|
5 | 525 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
526 |
* Filters the array of meta data read from an image's exif data. |
5 | 527 |
* |
528 |
* @since 2.5.0 |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
529 |
* @since 4.4.0 The `$iptc` parameter was added. |
9 | 530 |
* @since 5.0.0 The `$exif` parameter was added. |
5 | 531 |
* |
9 | 532 |
* @param array $meta Image meta data. |
533 |
* @param string $file Path to image file. |
|
534 |
* @param int $image_type Type of image, one of the `IMAGETYPE_XXX` constants. |
|
535 |
* @param array $iptc IPTC data. |
|
536 |
* @param array $exif EXIF data. |
|
5 | 537 |
*/ |
9 | 538 |
return apply_filters( 'wp_read_image_metadata', $meta, $file, $image_type, $iptc, $exif ); |
0 | 539 |
|
540 |
} |
|
541 |
||
542 |
/** |
|
543 |
* Validate that file is an image. |
|
544 |
* |
|
545 |
* @since 2.5.0 |
|
546 |
* |
|
547 |
* @param string $path File path to test if valid image. |
|
548 |
* @return bool True if valid image, false if not valid image. |
|
549 |
*/ |
|
9 | 550 |
function file_is_valid_image( $path ) { |
551 |
$size = @getimagesize( $path ); |
|
552 |
return ! empty( $size ); |
|
0 | 553 |
} |
554 |
||
555 |
/** |
|
556 |
* Validate that file is suitable for displaying within a web page. |
|
557 |
* |
|
558 |
* @since 2.5.0 |
|
559 |
* |
|
560 |
* @param string $path File path to test. |
|
561 |
* @return bool True if suitable, false if not suitable. |
|
562 |
*/ |
|
9 | 563 |
function file_is_displayable_image( $path ) { |
5 | 564 |
$displayable_image_types = array( IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG, IMAGETYPE_BMP ); |
565 |
||
9 | 566 |
// IMAGETYPE_ICO is only defined in PHP 5.3+. |
567 |
if ( defined( 'IMAGETYPE_ICO' ) ) { |
|
568 |
$displayable_image_types[] = IMAGETYPE_ICO; |
|
569 |
} |
|
570 |
||
5 | 571 |
$info = @getimagesize( $path ); |
572 |
if ( empty( $info ) ) { |
|
0 | 573 |
$result = false; |
5 | 574 |
} elseif ( ! in_array( $info[2], $displayable_image_types ) ) { |
0 | 575 |
$result = false; |
5 | 576 |
} else { |
0 | 577 |
$result = true; |
5 | 578 |
} |
0 | 579 |
|
5 | 580 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
581 |
* Filters whether the current image is displayable in the browser. |
5 | 582 |
* |
583 |
* @since 2.5.0 |
|
584 |
* |
|
585 |
* @param bool $result Whether the image can be displayed. Default true. |
|
586 |
* @param string $path Path to the image. |
|
587 |
*/ |
|
588 |
return apply_filters( 'file_is_displayable_image', $result, $path ); |
|
0 | 589 |
} |
590 |
||
591 |
/** |
|
592 |
* Load an image resource for editing. |
|
593 |
* |
|
594 |
* @since 2.9.0 |
|
595 |
* |
|
596 |
* @param string $attachment_id Attachment ID. |
|
597 |
* @param string $mime_type Image mime type. |
|
598 |
* @param string $size Optional. Image size, defaults to 'full'. |
|
599 |
* @return resource|false The resulting image resource on success, false on failure. |
|
600 |
*/ |
|
601 |
function load_image_to_edit( $attachment_id, $mime_type, $size = 'full' ) { |
|
602 |
$filepath = _load_image_to_edit_path( $attachment_id, $size ); |
|
9 | 603 |
if ( empty( $filepath ) ) { |
0 | 604 |
return false; |
9 | 605 |
} |
0 | 606 |
|
607 |
switch ( $mime_type ) { |
|
608 |
case 'image/jpeg': |
|
9 | 609 |
$image = imagecreatefromjpeg( $filepath ); |
0 | 610 |
break; |
611 |
case 'image/png': |
|
9 | 612 |
$image = imagecreatefrompng( $filepath ); |
0 | 613 |
break; |
614 |
case 'image/gif': |
|
9 | 615 |
$image = imagecreatefromgif( $filepath ); |
0 | 616 |
break; |
617 |
default: |
|
618 |
$image = false; |
|
619 |
break; |
|
620 |
} |
|
9 | 621 |
if ( is_resource( $image ) ) { |
5 | 622 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
623 |
* Filters the current image being loaded for editing. |
5 | 624 |
* |
625 |
* @since 2.9.0 |
|
626 |
* |
|
627 |
* @param resource $image Current image. |
|
628 |
* @param string $attachment_id Attachment ID. |
|
629 |
* @param string $size Image size. |
|
630 |
*/ |
|
631 |
$image = apply_filters( 'load_image_to_edit', $image, $attachment_id, $size ); |
|
9 | 632 |
if ( function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) { |
633 |
imagealphablending( $image, false ); |
|
634 |
imagesavealpha( $image, true ); |
|
0 | 635 |
} |
636 |
} |
|
637 |
return $image; |
|
638 |
} |
|
639 |
||
640 |
/** |
|
641 |
* Retrieve the path or url of an attachment's attached file. |
|
642 |
* |
|
643 |
* If the attached file is not present on the local filesystem (usually due to replication plugins), |
|
644 |
* then the url of the file is returned if url fopen is supported. |
|
645 |
* |
|
646 |
* @since 3.4.0 |
|
647 |
* @access private |
|
648 |
* |
|
649 |
* @param string $attachment_id Attachment ID. |
|
650 |
* @param string $size Optional. Image size, defaults to 'full'. |
|
651 |
* @return string|false File path or url on success, false on failure. |
|
652 |
*/ |
|
653 |
function _load_image_to_edit_path( $attachment_id, $size = 'full' ) { |
|
654 |
$filepath = get_attached_file( $attachment_id ); |
|
655 |
||
656 |
if ( $filepath && file_exists( $filepath ) ) { |
|
657 |
if ( 'full' != $size && ( $data = image_get_intermediate_size( $attachment_id, $size ) ) ) { |
|
5 | 658 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
659 |
* Filters the path to the current image. |
5 | 660 |
* |
661 |
* The filter is evaluated for all image sizes except 'full'. |
|
662 |
* |
|
663 |
* @since 3.1.0 |
|
664 |
* |
|
665 |
* @param string $path Path to the current image. |
|
666 |
* @param string $attachment_id Attachment ID. |
|
667 |
* @param string $size Size of the image. |
|
668 |
*/ |
|
0 | 669 |
$filepath = apply_filters( 'load_image_to_edit_filesystempath', path_join( dirname( $filepath ), $data['file'] ), $attachment_id, $size ); |
670 |
} |
|
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
671 |
} elseif ( function_exists( 'fopen' ) && true == ini_get( 'allow_url_fopen' ) ) { |
5 | 672 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
673 |
* Filters the image URL if not in the local filesystem. |
5 | 674 |
* |
675 |
* The filter is only evaluated if fopen is enabled on the server. |
|
676 |
* |
|
677 |
* @since 3.1.0 |
|
678 |
* |
|
679 |
* @param string $image_url Current image URL. |
|
680 |
* @param string $attachment_id Attachment ID. |
|
681 |
* @param string $size Size of the image. |
|
682 |
*/ |
|
0 | 683 |
$filepath = apply_filters( 'load_image_to_edit_attachmenturl', wp_get_attachment_url( $attachment_id ), $attachment_id, $size ); |
684 |
} |
|
685 |
||
5 | 686 |
/** |
7
cf61fcea0001
resynchronize code repo with production
ymh <ymh.work@gmail.com>
parents:
5
diff
changeset
|
687 |
* Filters the returned path or URL of the current image. |
5 | 688 |
* |
689 |
* @since 2.9.0 |
|
690 |
* |
|
691 |
* @param string|bool $filepath File path or URL to current image, or false. |
|
692 |
* @param string $attachment_id Attachment ID. |
|
693 |
* @param string $size Size of the image. |
|
694 |
*/ |
|
0 | 695 |
return apply_filters( 'load_image_to_edit_path', $filepath, $attachment_id, $size ); |
696 |
} |
|
697 |
||
698 |
/** |
|
699 |
* Copy an existing image file. |
|
700 |
* |
|
701 |
* @since 3.4.0 |
|
702 |
* @access private |
|
703 |
* |
|
704 |
* @param string $attachment_id Attachment ID. |
|
705 |
* @return string|false New file path on success, false on failure. |
|
706 |
*/ |
|
707 |
function _copy_image_file( $attachment_id ) { |
|
708 |
$dst_file = $src_file = get_attached_file( $attachment_id ); |
|
9 | 709 |
if ( ! file_exists( $src_file ) ) { |
0 | 710 |
$src_file = _load_image_to_edit_path( $attachment_id ); |
9 | 711 |
} |
0 | 712 |
|
713 |
if ( $src_file ) { |
|
9 | 714 |
$dst_file = str_replace( wp_basename( $dst_file ), 'copy-' . wp_basename( $dst_file ), $dst_file ); |
715 |
$dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), wp_basename( $dst_file ) ); |
|
0 | 716 |
|
5 | 717 |
/* |
718 |
* The directory containing the original file may no longer |
|
719 |
* exist when using a replication plugin. |
|
720 |
*/ |
|
0 | 721 |
wp_mkdir_p( dirname( $dst_file ) ); |
722 |
||
9 | 723 |
if ( ! @copy( $src_file, $dst_file ) ) { |
0 | 724 |
$dst_file = false; |
9 | 725 |
} |
0 | 726 |
} else { |
727 |
$dst_file = false; |
|
728 |
} |
|
729 |
||
730 |
return $dst_file; |
|
731 |
} |