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