author | Anthony Ly <anthonyly.com@gmail.com> |
Mon, 19 Nov 2012 18:26:13 +0100 | |
changeset 194 | 32102edaa81b |
parent 136 | bde1974c263b |
child 204 | 09a1c134465b |
permissions | -rw-r--r-- |
136 | 1 |
<?php |
2 |
/** |
|
3 |
* File contains all the administration image manipulation functions. |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
* @subpackage Administration |
|
7 |
*/ |
|
8 |
||
9 |
/** |
|
10 |
* Create a thumbnail from an Image given a maximum side size. |
|
11 |
* |
|
12 |
* This function can handle most image file formats which PHP supports. If PHP |
|
13 |
* does not have the functionality to save in a file of the same format, the |
|
14 |
* thumbnail will be created as a jpeg. |
|
15 |
* |
|
16 |
* @since 1.2.0 |
|
17 |
* |
|
18 |
* @param mixed $file Filename of the original image, Or attachment id. |
|
19 |
* @param int $max_side Maximum length of a single side for the thumbnail. |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
20 |
* @param mixed $deprecated Never used. |
136 | 21 |
* @return string Thumbnail path on success, Error string on failure. |
22 |
*/ |
|
23 |
function wp_create_thumbnail( $file, $max_side, $deprecated = '' ) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
24 |
if ( !empty( $deprecated ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
25 |
_deprecated_argument( __FUNCTION__, '1.2' ); |
136 | 26 |
$thumbpath = image_resize( $file, $max_side, $max_side ); |
27 |
return apply_filters( 'wp_create_thumbnail', $thumbpath ); |
|
28 |
} |
|
29 |
||
30 |
/** |
|
31 |
* Crop an Image to a given size. |
|
32 |
* |
|
33 |
* @since 2.1.0 |
|
34 |
* |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
35 |
* @param string|int $src The source file or Attachment ID. |
136 | 36 |
* @param int $src_x The start x position to crop from. |
37 |
* @param int $src_y The start y position to crop from. |
|
38 |
* @param int $src_w The width to crop. |
|
39 |
* @param int $src_h The height to crop. |
|
40 |
* @param int $dst_w The destination width. |
|
41 |
* @param int $dst_h The destination height. |
|
42 |
* @param int $src_abs Optional. If the source crop points are absolute. |
|
43 |
* @param string $dst_file Optional. The destination file to write to. |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
44 |
* @return string|WP_Error|false New filepath on success, WP_Error or false on failure. |
136 | 45 |
*/ |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
46 |
function wp_crop_image( $src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
47 |
if ( is_numeric( $src ) ) { // Handle int as attachment ID |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
48 |
$src_file = get_attached_file( $src ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
49 |
if ( ! file_exists( $src_file ) ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
50 |
// If the file doesn't exist, attempt a url fopen on the src link. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
51 |
// This can occur with certain file replication plugins. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
52 |
$post = get_post( $src ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
53 |
$image_type = $post->post_mime_type; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
54 |
$src = load_image_to_edit( $src, $post->post_mime_type, 'full' ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
55 |
} else { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
56 |
$size = @getimagesize( $src_file ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
57 |
$image_type = ( $size ) ? $size['mime'] : ''; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
58 |
$src = wp_load_image( $src_file ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
59 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
60 |
} else { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
61 |
$size = @getimagesize( $src ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
62 |
$image_type = ( $size ) ? $size['mime'] : ''; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
63 |
$src = wp_load_image( $src ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
64 |
} |
136 | 65 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
66 |
if ( ! is_resource( $src ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
67 |
return new WP_Error( 'error_loading_image', $src, $src_file ); |
136 | 68 |
|
69 |
$dst = wp_imagecreatetruecolor( $dst_w, $dst_h ); |
|
70 |
||
71 |
if ( $src_abs ) { |
|
72 |
$src_w -= $src_x; |
|
73 |
$src_h -= $src_y; |
|
74 |
} |
|
75 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
76 |
if ( function_exists( 'imageantialias' ) ) |
136 | 77 |
imageantialias( $dst, true ); |
78 |
||
79 |
imagecopyresampled( $dst, $src, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h ); |
|
80 |
||
81 |
imagedestroy( $src ); // Free up memory |
|
82 |
||
83 |
if ( ! $dst_file ) |
|
84 |
$dst_file = str_replace( basename( $src_file ), 'cropped-' . basename( $src_file ), $src_file ); |
|
85 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
86 |
if ( 'image/png' != $image_type ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
87 |
$dst_file = preg_replace( '/\\.[^\\.]+$/', '.jpg', $dst_file ); |
136 | 88 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
89 |
// The directory containing the original file may no longer exist when |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
90 |
// using a replication plugin. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
91 |
wp_mkdir_p( dirname( $dst_file ) ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
92 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
93 |
$dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), basename( $dst_file ) ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
94 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
95 |
if ( 'image/png' == $image_type && imagepng( $dst, $dst_file ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
96 |
return $dst_file; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
97 |
elseif ( imagejpeg( $dst, $dst_file, apply_filters( 'jpeg_quality', 90, 'wp_crop_image' ) ) ) |
136 | 98 |
return $dst_file; |
99 |
else |
|
100 |
return false; |
|
101 |
} |
|
102 |
||
103 |
/** |
|
104 |
* Generate post thumbnail attachment meta data. |
|
105 |
* |
|
106 |
* @since 2.1.0 |
|
107 |
* |
|
108 |
* @param int $attachment_id Attachment Id to process. |
|
109 |
* @param string $file Filepath of the Attached image. |
|
110 |
* @return mixed Metadata for attachment. |
|
111 |
*/ |
|
112 |
function wp_generate_attachment_metadata( $attachment_id, $file ) { |
|
113 |
$attachment = get_post( $attachment_id ); |
|
114 |
||
115 |
$metadata = array(); |
|
116 |
if ( preg_match('!^image/!', get_post_mime_type( $attachment )) && file_is_displayable_image($file) ) { |
|
117 |
$imagesize = getimagesize( $file ); |
|
118 |
$metadata['width'] = $imagesize[0]; |
|
119 |
$metadata['height'] = $imagesize[1]; |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
120 |
list($uwidth, $uheight) = wp_constrain_dimensions($metadata['width'], $metadata['height'], 128, 96); |
136 | 121 |
$metadata['hwstring_small'] = "height='$uheight' width='$uwidth'"; |
122 |
||
123 |
// Make the file path relative to the upload dir |
|
124 |
$metadata['file'] = _wp_relative_upload_path($file); |
|
125 |
||
126 |
// make thumbnails and other intermediate sizes |
|
127 |
global $_wp_additional_image_sizes; |
|
128 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
129 |
foreach ( get_intermediate_image_sizes() as $s ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
130 |
$sizes[$s] = array( 'width' => '', 'height' => '', 'crop' => false ); |
136 | 131 |
if ( isset( $_wp_additional_image_sizes[$s]['width'] ) ) |
132 |
$sizes[$s]['width'] = intval( $_wp_additional_image_sizes[$s]['width'] ); // For theme-added sizes |
|
133 |
else |
|
134 |
$sizes[$s]['width'] = get_option( "{$s}_size_w" ); // For default sizes set in options |
|
135 |
if ( isset( $_wp_additional_image_sizes[$s]['height'] ) ) |
|
136 |
$sizes[$s]['height'] = intval( $_wp_additional_image_sizes[$s]['height'] ); // For theme-added sizes |
|
137 |
else |
|
138 |
$sizes[$s]['height'] = get_option( "{$s}_size_h" ); // For default sizes set in options |
|
139 |
if ( isset( $_wp_additional_image_sizes[$s]['crop'] ) ) |
|
140 |
$sizes[$s]['crop'] = intval( $_wp_additional_image_sizes[$s]['crop'] ); // For theme-added sizes |
|
141 |
else |
|
142 |
$sizes[$s]['crop'] = get_option( "{$s}_crop" ); // For default sizes set in options |
|
143 |
} |
|
144 |
||
145 |
$sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes ); |
|
146 |
||
147 |
foreach ($sizes as $size => $size_data ) { |
|
148 |
$resized = image_make_intermediate_size( $file, $size_data['width'], $size_data['height'], $size_data['crop'] ); |
|
149 |
if ( $resized ) |
|
150 |
$metadata['sizes'][$size] = $resized; |
|
151 |
} |
|
152 |
||
153 |
// fetch additional metadata from exif/iptc |
|
154 |
$image_meta = wp_read_image_metadata( $file ); |
|
155 |
if ( $image_meta ) |
|
156 |
$metadata['image_meta'] = $image_meta; |
|
157 |
||
158 |
} |
|
159 |
||
160 |
return apply_filters( 'wp_generate_attachment_metadata', $metadata, $attachment_id ); |
|
161 |
} |
|
162 |
||
163 |
/** |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
164 |
* Calculated the new dimensions for a downsampled image. |
136 | 165 |
* |
166 |
* @since 2.0.0 |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
167 |
* @see wp_constrain_dimensions() |
136 | 168 |
* |
169 |
* @param int $width Current width of the image |
|
170 |
* @param int $height Current height of the image |
|
171 |
* @return mixed Array(height,width) of shrunk dimensions. |
|
172 |
*/ |
|
173 |
function get_udims( $width, $height) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
174 |
return wp_constrain_dimensions( $width, $height, 128, 96 ); |
136 | 175 |
} |
176 |
||
177 |
/** |
|
178 |
* Convert a fraction string to a decimal. |
|
179 |
* |
|
180 |
* @since 2.5.0 |
|
181 |
* |
|
182 |
* @param string $str |
|
183 |
* @return int|float |
|
184 |
*/ |
|
185 |
function wp_exif_frac2dec($str) { |
|
186 |
@list( $n, $d ) = explode( '/', $str ); |
|
187 |
if ( !empty($d) ) |
|
188 |
return $n / $d; |
|
189 |
return $str; |
|
190 |
} |
|
191 |
||
192 |
/** |
|
193 |
* Convert the exif date format to a unix timestamp. |
|
194 |
* |
|
195 |
* @since 2.5.0 |
|
196 |
* |
|
197 |
* @param string $str |
|
198 |
* @return int |
|
199 |
*/ |
|
200 |
function wp_exif_date2ts($str) { |
|
201 |
@list( $date, $time ) = explode( ' ', trim($str) ); |
|
202 |
@list( $y, $m, $d ) = explode( ':', $date ); |
|
203 |
||
204 |
return strtotime( "{$y}-{$m}-{$d} {$time}" ); |
|
205 |
} |
|
206 |
||
207 |
/** |
|
208 |
* Get extended image metadata, exif or iptc as available. |
|
209 |
* |
|
210 |
* Retrieves the EXIF metadata aperture, credit, camera, caption, copyright, iso |
|
211 |
* created_timestamp, focal_length, shutter_speed, and title. |
|
212 |
* |
|
213 |
* The IPTC metadata that is retrieved is APP13, credit, byline, created date |
|
214 |
* and time, caption, copyright, and title. Also includes FNumber, Model, |
|
215 |
* DateTimeDigitized, FocalLength, ISOSpeedRatings, and ExposureTime. |
|
216 |
* |
|
217 |
* @todo Try other exif libraries if available. |
|
218 |
* @since 2.5.0 |
|
219 |
* |
|
220 |
* @param string $file |
|
221 |
* @return bool|array False on failure. Image metadata array on success. |
|
222 |
*/ |
|
223 |
function wp_read_image_metadata( $file ) { |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
224 |
if ( ! file_exists( $file ) ) |
136 | 225 |
return false; |
226 |
||
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
227 |
list( , , $sourceImageType ) = getimagesize( $file ); |
136 | 228 |
|
229 |
// exif contains a bunch of data we'll probably never need formatted in ways |
|
230 |
// that are difficult to use. We'll normalize it and just extract the fields |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
231 |
// that are likely to be useful. Fractions and numbers are converted to |
136 | 232 |
// floats, dates to unix timestamps, and everything else to strings. |
233 |
$meta = array( |
|
234 |
'aperture' => 0, |
|
235 |
'credit' => '', |
|
236 |
'camera' => '', |
|
237 |
'caption' => '', |
|
238 |
'created_timestamp' => 0, |
|
239 |
'copyright' => '', |
|
240 |
'focal_length' => 0, |
|
241 |
'iso' => 0, |
|
242 |
'shutter_speed' => 0, |
|
243 |
'title' => '', |
|
244 |
); |
|
245 |
||
246 |
// read iptc first, since it might contain data not available in exif such |
|
247 |
// as caption, description etc |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
248 |
if ( is_callable( 'iptcparse' ) ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
249 |
getimagesize( $file, $info ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
250 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
251 |
if ( ! empty( $info['APP13'] ) ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
252 |
$iptc = iptcparse( $info['APP13'] ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
253 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
254 |
// headline, "A brief synopsis of the caption." |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
255 |
if ( ! empty( $iptc['2#105'][0] ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
256 |
$meta['title'] = utf8_encode( trim( $iptc['2#105'][0] ) ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
257 |
// title, "Many use the Title field to store the filename of the image, though the field may be used in many ways." |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
258 |
elseif ( ! empty( $iptc['2#005'][0] ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
259 |
$meta['title'] = utf8_encode( trim( $iptc['2#005'][0] ) ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
260 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
261 |
if ( ! empty( $iptc['2#120'][0] ) ) { // description / legacy caption |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
262 |
$caption = utf8_encode( trim( $iptc['2#120'][0] ) ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
263 |
if ( empty( $meta['title'] ) ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
264 |
// Assume the title is stored in 2:120 if it's short. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
265 |
if ( strlen( $caption ) < 80 ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
266 |
$meta['title'] = $caption; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
267 |
else |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
268 |
$meta['caption'] = $caption; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
269 |
} elseif ( $caption != $meta['title'] ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
270 |
$meta['caption'] = $caption; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
271 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
272 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
273 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
274 |
if ( ! empty( $iptc['2#110'][0] ) ) // credit |
136 | 275 |
$meta['credit'] = utf8_encode(trim($iptc['2#110'][0])); |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
276 |
elseif ( ! empty( $iptc['2#080'][0] ) ) // creator / legacy byline |
136 | 277 |
$meta['credit'] = utf8_encode(trim($iptc['2#080'][0])); |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
278 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
279 |
if ( ! empty( $iptc['2#055'][0] ) and ! empty( $iptc['2#060'][0] ) ) // created date and time |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
280 |
$meta['created_timestamp'] = strtotime( $iptc['2#055'][0] . ' ' . $iptc['2#060'][0] ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
281 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
282 |
if ( ! empty( $iptc['2#116'][0] ) ) // copyright |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
283 |
$meta['copyright'] = utf8_encode( trim( $iptc['2#116'][0] ) ); |
136 | 284 |
} |
285 |
} |
|
286 |
||
287 |
// fetch additional info from exif if available |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
288 |
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 ) ) ) ) { |
136 | 289 |
$exif = @exif_read_data( $file ); |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
290 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
291 |
if ( !empty( $exif['Title'] ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
292 |
$meta['title'] = utf8_encode( trim( $exif['Title'] ) ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
293 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
294 |
if ( ! empty( $exif['ImageDescription'] ) ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
295 |
if ( empty( $meta['title'] ) && strlen( $exif['ImageDescription'] ) < 80 ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
296 |
// Assume the title is stored in ImageDescription |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
297 |
$meta['title'] = utf8_encode( trim( $exif['ImageDescription'] ) ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
298 |
if ( ! empty( $exif['COMPUTED']['UserComment'] ) && trim( $exif['COMPUTED']['UserComment'] ) != $meta['title'] ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
299 |
$meta['caption'] = utf8_encode( trim( $exif['COMPUTED']['UserComment'] ) ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
300 |
} elseif ( trim( $exif['ImageDescription'] ) != $meta['title'] ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
301 |
$meta['caption'] = utf8_encode( trim( $exif['ImageDescription'] ) ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
302 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
303 |
} elseif ( ! empty( $exif['Comments'] ) && trim( $exif['Comments'] ) != $meta['title'] ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
304 |
$meta['caption'] = utf8_encode( trim( $exif['Comments'] ) ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
305 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
306 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
307 |
if ( ! empty( $exif['Artist'] ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
308 |
$meta['credit'] = utf8_encode( trim( $exif['Artist'] ) ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
309 |
elseif ( ! empty($exif['Author'] ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
310 |
$meta['credit'] = utf8_encode( trim( $exif['Author'] ) ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
311 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
312 |
if ( ! empty( $exif['Copyright'] ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
313 |
$meta['copyright'] = utf8_encode( trim( $exif['Copyright'] ) ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
314 |
if ( ! empty($exif['FNumber'] ) ) |
136 | 315 |
$meta['aperture'] = round( wp_exif_frac2dec( $exif['FNumber'] ), 2 ); |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
316 |
if ( ! empty($exif['Model'] ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
317 |
$meta['camera'] = utf8_encode( trim( $exif['Model'] ) ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
318 |
if ( ! empty($exif['DateTimeDigitized'] ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
319 |
$meta['created_timestamp'] = wp_exif_date2ts($exif['DateTimeDigitized'] ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
320 |
if ( ! empty($exif['FocalLength'] ) ) |
136 | 321 |
$meta['focal_length'] = wp_exif_frac2dec( $exif['FocalLength'] ); |
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
322 |
if ( ! empty($exif['ISOSpeedRatings'] ) ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
323 |
$meta['iso'] = is_array( $exif['ISOSpeedRatings'] ) ? reset( $exif['ISOSpeedRatings'] ) : $exif['ISOSpeedRatings']; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
324 |
$meta['iso'] = utf8_encode( trim( $meta['iso'] ) ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
325 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
326 |
if ( ! empty($exif['ExposureTime'] ) ) |
136 | 327 |
$meta['shutter_speed'] = wp_exif_frac2dec( $exif['ExposureTime'] ); |
328 |
} |
|
329 |
||
330 |
return apply_filters( 'wp_read_image_metadata', $meta, $file, $sourceImageType ); |
|
331 |
||
332 |
} |
|
333 |
||
334 |
/** |
|
335 |
* Validate that file is an image. |
|
336 |
* |
|
337 |
* @since 2.5.0 |
|
338 |
* |
|
339 |
* @param string $path File path to test if valid image. |
|
340 |
* @return bool True if valid image, false if not valid image. |
|
341 |
*/ |
|
342 |
function file_is_valid_image($path) { |
|
343 |
$size = @getimagesize($path); |
|
344 |
return !empty($size); |
|
345 |
} |
|
346 |
||
347 |
/** |
|
348 |
* Validate that file is suitable for displaying within a web page. |
|
349 |
* |
|
350 |
* @since 2.5.0 |
|
351 |
* @uses apply_filters() Calls 'file_is_displayable_image' on $result and $path. |
|
352 |
* |
|
353 |
* @param string $path File path to test. |
|
354 |
* @return bool True if suitable, false if not suitable. |
|
355 |
*/ |
|
356 |
function file_is_displayable_image($path) { |
|
357 |
$info = @getimagesize($path); |
|
358 |
if ( empty($info) ) |
|
359 |
$result = false; |
|
360 |
elseif ( !in_array($info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG)) ) // only gif, jpeg and png images can reliably be displayed |
|
361 |
$result = false; |
|
362 |
else |
|
363 |
$result = true; |
|
364 |
||
365 |
return apply_filters('file_is_displayable_image', $result, $path); |
|
366 |
} |
|
194
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
367 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
368 |
/** |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
369 |
* Load an image resource for editing. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
370 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
371 |
* @since 2.9.0 |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
372 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
373 |
* @param string $attachment_id Attachment ID. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
374 |
* @param string $mime_type Image mime type. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
375 |
* @param string $size Optional. Image size, defaults to 'full'. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
376 |
* @return resource|false The resulting image resource on success, false on failure. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
377 |
*/ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
378 |
function load_image_to_edit( $attachment_id, $mime_type, $size = 'full' ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
379 |
$filepath = _load_image_to_edit_path( $attachment_id, $size ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
380 |
if ( empty( $filepath ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
381 |
return false; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
382 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
383 |
switch ( $mime_type ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
384 |
case 'image/jpeg': |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
385 |
$image = imagecreatefromjpeg($filepath); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
386 |
break; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
387 |
case 'image/png': |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
388 |
$image = imagecreatefrompng($filepath); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
389 |
break; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
390 |
case 'image/gif': |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
391 |
$image = imagecreatefromgif($filepath); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
392 |
break; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
393 |
default: |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
394 |
$image = false; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
395 |
break; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
396 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
397 |
if ( is_resource($image) ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
398 |
$image = apply_filters('load_image_to_edit', $image, $attachment_id, $size); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
399 |
if ( function_exists('imagealphablending') && function_exists('imagesavealpha') ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
400 |
imagealphablending($image, false); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
401 |
imagesavealpha($image, true); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
402 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
403 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
404 |
return $image; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
405 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
406 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
407 |
/** |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
408 |
* Retrieve the path or url of an attachment's attached file. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
409 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
410 |
* If the attached file is not present on the local filesystem (usually due to replication plugins), |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
411 |
* then the url of the file is returned if url fopen is supported. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
412 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
413 |
* @since 3.4.0 |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
414 |
* @access private |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
415 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
416 |
* @param string $attachment_id Attachment ID. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
417 |
* @param string $size Optional. Image size, defaults to 'full'. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
418 |
* @return string|false File path or url on success, false on failure. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
419 |
*/ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
420 |
function _load_image_to_edit_path( $attachment_id, $size = 'full' ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
421 |
$filepath = get_attached_file( $attachment_id ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
422 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
423 |
if ( $filepath && file_exists( $filepath ) ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
424 |
if ( 'full' != $size && ( $data = image_get_intermediate_size( $attachment_id, $size ) ) ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
425 |
$filepath = apply_filters( 'load_image_to_edit_filesystempath', path_join( dirname( $filepath ), $data['file'] ), $attachment_id, $size ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
426 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
427 |
} elseif ( function_exists( 'fopen' ) && function_exists( 'ini_get' ) && true == ini_get( 'allow_url_fopen' ) ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
428 |
$filepath = apply_filters( 'load_image_to_edit_attachmenturl', wp_get_attachment_url( $attachment_id ), $attachment_id, $size ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
429 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
430 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
431 |
return apply_filters( 'load_image_to_edit_path', $filepath, $attachment_id, $size ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
432 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
433 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
434 |
/** |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
435 |
* Copy an existing image file. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
436 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
437 |
* @since 3.4.0 |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
438 |
* @access private |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
439 |
* |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
440 |
* @param string $attachment_id Attachment ID. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
441 |
* @return string|false New file path on success, false on failure. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
442 |
*/ |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
443 |
function _copy_image_file( $attachment_id ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
444 |
$dst_file = $src_file = get_attached_file( $attachment_id ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
445 |
if ( ! file_exists( $src_file ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
446 |
$src_file = _load_image_to_edit_path( $attachment_id ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
447 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
448 |
if ( $src_file ) { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
449 |
$dst_file = str_replace( basename( $dst_file ), 'copy-' . basename( $dst_file ), $dst_file ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
450 |
$dst_file = dirname( $dst_file ) . '/' . wp_unique_filename( dirname( $dst_file ), basename( $dst_file ) ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
451 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
452 |
// The directory containing the original file may no longer exist when |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
453 |
// using a replication plugin. |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
454 |
wp_mkdir_p( dirname( $dst_file ) ); |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
455 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
456 |
if ( ! @copy( $src_file, $dst_file ) ) |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
457 |
$dst_file = false; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
458 |
} else { |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
459 |
$dst_file = false; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
460 |
} |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
461 |
|
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
462 |
return $dst_file; |
32102edaa81b
MAJ wordpress et ajout de plugin
Anthony Ly <anthonyly.com@gmail.com>
parents:
136
diff
changeset
|
463 |
} |