[44] | 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.
|
---|
| 20 | * @return string Thumbnail path on success, Error string on failure.
|
---|
| 21 | */
|
---|
| 22 | function wp_create_thumbnail( $file, $max_side, $deprecated = '' ) {
|
---|
| 23 | $thumbpath = image_resize( $file, $max_side, $max_side );
|
---|
| 24 | return apply_filters( 'wp_create_thumbnail', $thumbpath );
|
---|
| 25 | }
|
---|
| 26 |
|
---|
| 27 | /**
|
---|
| 28 | * Crop an Image to a given size.
|
---|
| 29 | *
|
---|
| 30 | * @since 2.1.0
|
---|
| 31 | *
|
---|
| 32 | * @param string|int $src_file The source file or Attachment ID.
|
---|
| 33 | * @param int $src_x The start x position to crop from.
|
---|
| 34 | * @param int $src_y The start y position to crop from.
|
---|
| 35 | * @param int $src_w The width to crop.
|
---|
| 36 | * @param int $src_h The height to crop.
|
---|
| 37 | * @param int $dst_w The destination width.
|
---|
| 38 | * @param int $dst_h The destination height.
|
---|
| 39 | * @param int $src_abs Optional. If the source crop points are absolute.
|
---|
| 40 | * @param string $dst_file Optional. The destination file to write to.
|
---|
| 41 | * @return string New filepath on success, String error message on failure.
|
---|
| 42 | */
|
---|
| 43 | function wp_crop_image( $src_file, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false ) {
|
---|
| 44 | if ( is_numeric( $src_file ) ) // Handle int as attachment ID
|
---|
| 45 | $src_file = get_attached_file( $src_file );
|
---|
| 46 |
|
---|
| 47 | $src = wp_load_image( $src_file );
|
---|
| 48 |
|
---|
| 49 | if ( !is_resource( $src ))
|
---|
| 50 | return $src;
|
---|
| 51 |
|
---|
| 52 | $dst = imagecreatetruecolor( $dst_w, $dst_h );
|
---|
| 53 |
|
---|
| 54 | if ( $src_abs ) {
|
---|
| 55 | $src_w -= $src_x;
|
---|
| 56 | $src_h -= $src_y;
|
---|
| 57 | }
|
---|
| 58 |
|
---|
| 59 | if (function_exists('imageantialias'))
|
---|
| 60 | imageantialias( $dst, true );
|
---|
| 61 |
|
---|
| 62 | imagecopyresampled( $dst, $src, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h );
|
---|
| 63 |
|
---|
| 64 | imagedestroy( $src ); // Free up memory
|
---|
| 65 |
|
---|
| 66 | if ( ! $dst_file )
|
---|
| 67 | $dst_file = str_replace( basename( $src_file ), 'cropped-' . basename( $src_file ), $src_file );
|
---|
| 68 |
|
---|
| 69 | $dst_file = preg_replace( '/\\.[^\\.]+$/', '.jpg', $dst_file );
|
---|
| 70 |
|
---|
| 71 | if ( imagejpeg( $dst, $dst_file, apply_filters( 'jpeg_quality', 90, 'wp_crop_image' ) ) )
|
---|
| 72 | return $dst_file;
|
---|
| 73 | else
|
---|
| 74 | return false;
|
---|
| 75 | }
|
---|
| 76 |
|
---|
| 77 | /**
|
---|
| 78 | * Generate post image attachment meta data.
|
---|
| 79 | *
|
---|
| 80 | * @since 2.1.0
|
---|
| 81 | *
|
---|
| 82 | * @param int $attachment_id Attachment Id to process.
|
---|
| 83 | * @param string $file Filepath of the Attached image.
|
---|
| 84 | * @return mixed Metadata for attachment.
|
---|
| 85 | */
|
---|
| 86 | function wp_generate_attachment_metadata( $attachment_id, $file ) {
|
---|
| 87 | $attachment = get_post( $attachment_id );
|
---|
| 88 |
|
---|
| 89 | $metadata = array();
|
---|
| 90 | if ( preg_match('!^image/!', get_post_mime_type( $attachment )) && file_is_displayable_image($file) ) {
|
---|
| 91 | $full_path_file = $file;
|
---|
| 92 | $imagesize = getimagesize( $full_path_file );
|
---|
| 93 | $metadata['width'] = $imagesize[0];
|
---|
| 94 | $metadata['height'] = $imagesize[1];
|
---|
| 95 | list($uwidth, $uheight) = wp_shrink_dimensions($metadata['width'], $metadata['height']);
|
---|
| 96 | $metadata['hwstring_small'] = "height='$uheight' width='$uwidth'";
|
---|
| 97 |
|
---|
| 98 | // Make the file path relative to the upload dir
|
---|
| 99 | if ( ($uploads = wp_upload_dir()) && false === $uploads['error'] ) { // Get upload directory
|
---|
| 100 | if ( 0 === strpos($file, $uploads['basedir']) ) {// Check that the upload base exists in the file path
|
---|
| 101 | $file = str_replace($uploads['basedir'], '', $file); // Remove upload dir from the file path
|
---|
| 102 | $file = ltrim($file, '/');
|
---|
| 103 | }
|
---|
| 104 | }
|
---|
| 105 | $metadata['file'] = $file;
|
---|
| 106 |
|
---|
| 107 | // make thumbnails and other intermediate sizes
|
---|
| 108 | $sizes = array('thumbnail', 'medium', 'large');
|
---|
| 109 | $sizes = apply_filters('intermediate_image_sizes', $sizes);
|
---|
| 110 |
|
---|
| 111 | foreach ($sizes as $size) {
|
---|
| 112 | $resized = image_make_intermediate_size( $full_path_file, get_option("{$size}_size_w"), get_option("{$size}_size_h"), get_option("{$size}_crop") );
|
---|
| 113 | if ( $resized )
|
---|
| 114 | $metadata['sizes'][$size] = $resized;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | // fetch additional metadata from exif/iptc
|
---|
| 118 | $image_meta = wp_read_image_metadata( $full_path_file );
|
---|
| 119 | if ($image_meta)
|
---|
| 120 | $metadata['image_meta'] = $image_meta;
|
---|
| 121 |
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | return apply_filters( 'wp_generate_attachment_metadata', $metadata );
|
---|
| 125 | }
|
---|
| 126 |
|
---|
| 127 | /**
|
---|
| 128 | * Load an image from a string, if PHP supports it.
|
---|
| 129 | *
|
---|
| 130 | * @since 2.1.0
|
---|
| 131 | *
|
---|
| 132 | * @param string $file Filename of the image to load.
|
---|
| 133 | * @return resource The resulting image resource on success, Error string on failure.
|
---|
| 134 | */
|
---|
| 135 | function wp_load_image( $file ) {
|
---|
| 136 | if ( is_numeric( $file ) )
|
---|
| 137 | $file = get_attached_file( $file );
|
---|
| 138 |
|
---|
| 139 | if ( ! file_exists( $file ) )
|
---|
| 140 | return sprintf(__('File “%s” doesn’t exist?'), $file);
|
---|
| 141 |
|
---|
| 142 | if ( ! function_exists('imagecreatefromstring') )
|
---|
| 143 | return __('The GD image library is not installed.');
|
---|
| 144 |
|
---|
| 145 | // Set artificially high because GD uses uncompressed images in memory
|
---|
| 146 | @ini_set('memory_limit', '256M');
|
---|
| 147 | $image = imagecreatefromstring( file_get_contents( $file ) );
|
---|
| 148 |
|
---|
| 149 | if ( !is_resource( $image ) )
|
---|
| 150 | return sprintf(__('File “%s” is not an image.'), $file);
|
---|
| 151 |
|
---|
| 152 | return $image;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | /**
|
---|
| 156 | * Calculated the new dimentions for a downsampled image.
|
---|
| 157 | *
|
---|
| 158 | * @since 2.0.0
|
---|
| 159 | * @see wp_shrink_dimensions()
|
---|
| 160 | *
|
---|
| 161 | * @param int $width Current width of the image
|
---|
| 162 | * @param int $height Current height of the image
|
---|
| 163 | * @return mixed Array(height,width) of shrunk dimensions.
|
---|
| 164 | */
|
---|
| 165 | function get_udims( $width, $height) {
|
---|
| 166 | return wp_shrink_dimensions( $width, $height );
|
---|
| 167 | }
|
---|
| 168 |
|
---|
| 169 | /**
|
---|
| 170 | * Calculates the new dimentions for a downsampled image.
|
---|
| 171 | *
|
---|
| 172 | * @since 2.0.0
|
---|
| 173 | * @see wp_constrain_dimensions()
|
---|
| 174 | *
|
---|
| 175 | * @param int $width Current width of the image
|
---|
| 176 | * @param int $height Current height of the image
|
---|
| 177 | * @param int $wmax Maximum wanted width
|
---|
| 178 | * @param int $hmax Maximum wanted height
|
---|
| 179 | * @return mixed Array(height,width) of shrunk dimensions.
|
---|
| 180 | */
|
---|
| 181 | function wp_shrink_dimensions( $width, $height, $wmax = 128, $hmax = 96 ) {
|
---|
| 182 | return wp_constrain_dimensions( $width, $height, $wmax, $hmax );
|
---|
| 183 | }
|
---|
| 184 |
|
---|
| 185 | /**
|
---|
| 186 | * Convert a fraction string to a decimal.
|
---|
| 187 | *
|
---|
| 188 | * @since 2.5.0
|
---|
| 189 | *
|
---|
| 190 | * @param string $str
|
---|
| 191 | * @return int|float
|
---|
| 192 | */
|
---|
| 193 | function wp_exif_frac2dec($str) {
|
---|
| 194 | @list( $n, $d ) = explode( '/', $str );
|
---|
| 195 | if ( !empty($d) )
|
---|
| 196 | return $n / $d;
|
---|
| 197 | return $str;
|
---|
| 198 | }
|
---|
| 199 |
|
---|
| 200 | /**
|
---|
| 201 | * Convert the exif date format to a unix timestamp.
|
---|
| 202 | *
|
---|
| 203 | * @since 2.5.0
|
---|
| 204 | *
|
---|
| 205 | * @param string $str
|
---|
| 206 | * @return int
|
---|
| 207 | */
|
---|
| 208 | function wp_exif_date2ts($str) {
|
---|
| 209 | @list( $date, $time ) = explode( ' ', trim($str) );
|
---|
| 210 | @list( $y, $m, $d ) = explode( ':', $date );
|
---|
| 211 |
|
---|
| 212 | return strtotime( "{$y}-{$m}-{$d} {$time}" );
|
---|
| 213 | }
|
---|
| 214 |
|
---|
| 215 | /**
|
---|
| 216 | * Get extended image metadata, exif or iptc as available.
|
---|
| 217 | *
|
---|
| 218 | * Retrieves the EXIF metadata aperture, credit, camera, caption, copyright, iso
|
---|
| 219 | * created_timestamp, focal_length, shutter_speed, and title.
|
---|
| 220 | *
|
---|
| 221 | * The IPTC metadata that is retrieved is APP13, credit, byline, created date
|
---|
| 222 | * and time, caption, copyright, and title. Also includes FNumber, Model,
|
---|
| 223 | * DateTimeDigitized, FocalLength, ISOSpeedRatings, and ExposureTime.
|
---|
| 224 | *
|
---|
| 225 | * @todo Try other exif libraries if available.
|
---|
| 226 | * @since 2.5.0
|
---|
| 227 | *
|
---|
| 228 | * @param string $file
|
---|
| 229 | * @return bool|array False on failure. Image metadata array on success.
|
---|
| 230 | */
|
---|
| 231 | function wp_read_image_metadata( $file ) {
|
---|
| 232 | if ( !file_exists( $file ) )
|
---|
| 233 | return false;
|
---|
| 234 |
|
---|
| 235 | list(,,$sourceImageType) = getimagesize( $file );
|
---|
| 236 |
|
---|
| 237 | // exif contains a bunch of data we'll probably never need formatted in ways
|
---|
| 238 | // that are difficult to use. We'll normalize it and just extract the fields
|
---|
| 239 | // that are likely to be useful. Fractions and numbers are converted to
|
---|
| 240 | // floats, dates to unix timestamps, and everything else to strings.
|
---|
| 241 | $meta = array(
|
---|
| 242 | 'aperture' => 0,
|
---|
| 243 | 'credit' => '',
|
---|
| 244 | 'camera' => '',
|
---|
| 245 | 'caption' => '',
|
---|
| 246 | 'created_timestamp' => 0,
|
---|
| 247 | 'copyright' => '',
|
---|
| 248 | 'focal_length' => 0,
|
---|
| 249 | 'iso' => 0,
|
---|
| 250 | 'shutter_speed' => 0,
|
---|
| 251 | 'title' => '',
|
---|
| 252 | );
|
---|
| 253 |
|
---|
| 254 | // read iptc first, since it might contain data not available in exif such
|
---|
| 255 | // as caption, description etc
|
---|
| 256 | if ( is_callable('iptcparse') ) {
|
---|
| 257 | getimagesize($file, $info);
|
---|
| 258 | if ( !empty($info['APP13']) ) {
|
---|
| 259 | $iptc = iptcparse($info['APP13']);
|
---|
| 260 | if ( !empty($iptc['2#110'][0]) ) // credit
|
---|
| 261 | $meta['credit'] = utf8_encode(trim($iptc['2#110'][0]));
|
---|
| 262 | elseif ( !empty($iptc['2#080'][0]) ) // byline
|
---|
| 263 | $meta['credit'] = utf8_encode(trim($iptc['2#080'][0]));
|
---|
| 264 | if ( !empty($iptc['2#055'][0]) and !empty($iptc['2#060'][0]) ) // created date and time
|
---|
| 265 | $meta['created_timestamp'] = strtotime($iptc['2#055'][0] . ' ' . $iptc['2#060'][0]);
|
---|
| 266 | if ( !empty($iptc['2#120'][0]) ) // caption
|
---|
| 267 | $meta['caption'] = utf8_encode(trim($iptc['2#120'][0]));
|
---|
| 268 | if ( !empty($iptc['2#116'][0]) ) // copyright
|
---|
| 269 | $meta['copyright'] = utf8_encode(trim($iptc['2#116'][0]));
|
---|
| 270 | if ( !empty($iptc['2#005'][0]) ) // title
|
---|
| 271 | $meta['title'] = utf8_encode(trim($iptc['2#005'][0]));
|
---|
| 272 | }
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | // fetch additional info from exif if available
|
---|
| 276 | 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)) ) ) {
|
---|
| 277 | $exif = @exif_read_data( $file );
|
---|
| 278 | if (!empty($exif['FNumber']))
|
---|
| 279 | $meta['aperture'] = round( wp_exif_frac2dec( $exif['FNumber'] ), 2 );
|
---|
| 280 | if (!empty($exif['Model']))
|
---|
| 281 | $meta['camera'] = trim( $exif['Model'] );
|
---|
| 282 | if (!empty($exif['DateTimeDigitized']))
|
---|
| 283 | $meta['created_timestamp'] = wp_exif_date2ts($exif['DateTimeDigitized']);
|
---|
| 284 | if (!empty($exif['FocalLength']))
|
---|
| 285 | $meta['focal_length'] = wp_exif_frac2dec( $exif['FocalLength'] );
|
---|
| 286 | if (!empty($exif['ISOSpeedRatings']))
|
---|
| 287 | $meta['iso'] = $exif['ISOSpeedRatings'];
|
---|
| 288 | if (!empty($exif['ExposureTime']))
|
---|
| 289 | $meta['shutter_speed'] = wp_exif_frac2dec( $exif['ExposureTime'] );
|
---|
| 290 | }
|
---|
| 291 |
|
---|
| 292 | return apply_filters( 'wp_read_image_metadata', $meta, $file, $sourceImageType );
|
---|
| 293 |
|
---|
| 294 | }
|
---|
| 295 |
|
---|
| 296 | /**
|
---|
| 297 | * Validate that file is an image.
|
---|
| 298 | *
|
---|
| 299 | * @since 2.5.0
|
---|
| 300 | *
|
---|
| 301 | * @param string $path File path to test if valid image.
|
---|
| 302 | * @return bool True if valid image, false if not valid image.
|
---|
| 303 | */
|
---|
| 304 | function file_is_valid_image($path) {
|
---|
| 305 | $size = @getimagesize($path);
|
---|
| 306 | return !empty($size);
|
---|
| 307 | }
|
---|
| 308 |
|
---|
| 309 | /**
|
---|
| 310 | * Validate that file is suitable for displaying within a web page.
|
---|
| 311 | *
|
---|
| 312 | * @since 2.5.0
|
---|
| 313 | * @uses apply_filters() Calls 'file_is_displayable_image' on $result and $path.
|
---|
| 314 | *
|
---|
| 315 | * @param string $path File path to test.
|
---|
| 316 | * @return bool True if suitable, false if not suitable.
|
---|
| 317 | */
|
---|
| 318 | function file_is_displayable_image($path) {
|
---|
| 319 | $info = @getimagesize($path);
|
---|
| 320 | if ( empty($info) )
|
---|
| 321 | $result = false;
|
---|
| 322 | elseif ( !in_array($info[2], array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG)) ) // only gif, jpeg and png images can reliably be displayed
|
---|
| 323 | $result = false;
|
---|
| 324 | else
|
---|
| 325 | $result = true;
|
---|
| 326 |
|
---|
| 327 | return apply_filters('file_is_displayable_image', $result, $path);
|
---|
| 328 | }
|
---|
| 329 |
|
---|
| 330 | ?>
|
---|