| 1 |
<?php |
| 2 |
/** |
| 3 |
* Imsanity utility functions. |
| 4 |
* |
| 5 |
* @package Imsanity |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Util function returns an array value, if not defined then returns default instead. |
| 10 |
* |
| 11 |
* @param array $arr Any array. |
| 12 |
* @param string $key Any index from that array. |
| 13 |
* @param mixed $default Whatever you want. |
| 14 |
*/ |
| 15 |
function imsanity_val( $arr, $key, $default = '' ) { |
| 16 |
return isset( $arr[ $key ] ) ? $arr[ $key ] : $default; |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Retrieves the path of an attachment via the $id and the $meta. |
| 21 |
* |
| 22 |
* @param array $meta The attachment metadata. |
| 23 |
* @param int $id The attachment ID number. |
| 24 |
* @param string $file Optional. Path relative to the uploads folder. Default ''. |
| 25 |
* @param bool $refresh_cache Optional. True to flush cache prior to fetching path. Default true. |
| 26 |
* @return string The full path to the image. |
| 27 |
*/ |
| 28 |
function imsanity_attachment_path( $meta, $id, $file = '', $refresh_cache = true ) { |
| 29 |
// Retrieve the location of the WordPress upload folder. |
| 30 |
$upload_dir = wp_upload_dir( null, false, $refresh_cache ); |
| 31 |
$upload_path = trailingslashit( $upload_dir['basedir'] ); |
| 32 |
if ( is_array( $meta ) && ! empty( $meta['file'] ) ) { |
| 33 |
$file_path = $meta['file']; |
| 34 |
if ( strpos( $file_path, 's3' ) === 0 ) { |
| 35 |
return ''; |
| 36 |
} |
| 37 |
if ( is_file( $file_path ) ) { |
| 38 |
return $file_path; |
| 39 |
} |
| 40 |
$file_path = $upload_path . $file_path; |
| 41 |
if ( is_file( $file_path ) ) { |
| 42 |
return $file_path; |
| 43 |
} |
| 44 |
$upload_path = trailingslashit( WP_CONTENT_DIR ) . 'uploads/'; |
| 45 |
$file_path = $upload_path . $meta['file']; |
| 46 |
if ( is_file( $file_path ) ) { |
| 47 |
return $file_path; |
| 48 |
} |
| 49 |
} |
| 50 |
if ( ! $file ) { |
| 51 |
$file = get_post_meta( $id, '_wp_attached_file', true ); |
| 52 |
} |
| 53 |
$file_path = ( 0 !== strpos( $file, '/' ) && ! preg_match( '|^.:\\\|', $file ) ? $upload_path . $file : $file ); |
| 54 |
$filtered_file_path = apply_filters( 'get_attached_file', $file_path, $id ); |
| 55 |
if ( strpos( $filtered_file_path, 's3' ) === false && is_file( $filtered_file_path ) ) { |
| 56 |
return str_replace( '//_imsgalleries/', '/_imsgalleries/', $filtered_file_path ); |
| 57 |
} |
| 58 |
if ( strpos( $file_path, 's3' ) === false && is_file( $file_path ) ) { |
| 59 |
return str_replace( '//_imsgalleries/', '/_imsgalleries/', $file_path ); |
| 60 |
} |
| 61 |
return ''; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Get mimetype based on file extension instead of file contents when speed outweighs accuracy. |
| 66 |
* |
| 67 |
* @param string $path The name of the file. |
| 68 |
* @return string|bool The mime type based on the extension or false. |
| 69 |
*/ |
| 70 |
function imsanity_quick_mimetype( $path ) { |
| 71 |
$pathextension = strtolower( pathinfo( $path, PATHINFO_EXTENSION ) ); |
| 72 |
switch ( $pathextension ) { |
| 73 |
case 'jpg': |
| 74 |
case 'jpeg': |
| 75 |
case 'jpe': |
| 76 |
return 'image/jpeg'; |
| 77 |
case 'png': |
| 78 |
return 'image/png'; |
| 79 |
case 'gif': |
| 80 |
return 'image/gif'; |
| 81 |
case 'pdf': |
| 82 |
return 'application/pdf'; |
| 83 |
default: |
| 84 |
return false; |
| 85 |
} |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Gets the orientation/rotation of a JPG image using the EXIF data. |
| 90 |
* |
| 91 |
* @param string $file Name of the file. |
| 92 |
* @param string $type Mime type of the file. |
| 93 |
* @return int|bool The orientation value or false. |
| 94 |
*/ |
| 95 |
function imsanity_get_orientation( $file, $type ) { |
| 96 |
if ( function_exists( 'exif_read_data' ) && 'image/jpeg' === $type ) { |
| 97 |
$exif = @exif_read_data( $file ); |
| 98 |
if ( is_array( $exif ) && array_key_exists( 'Orientation', $exif ) ) { |
| 99 |
return (int) $exif['Orientation']; |
| 100 |
} |
| 101 |
} |
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* Output a fatal error and optionally die. |
| 107 |
* |
| 108 |
* @param string $message The message to output. |
| 109 |
* @param string $title A title/header for the message. |
| 110 |
* @param bool $die Default false. Whether we should die. |
| 111 |
*/ |
| 112 |
function imsanity_fatal( $message, $title = '', $die = false ) { |
| 113 |
echo ( "<div style='margin:5px 0px 5px 0px;padding:10px;border: solid 1px red; background-color: #ff6666; color: black;'>" |
| 114 |
. ( $title ? "<h4 style='font-weight: bold; margin: 3px 0px 8px 0px;'>" . $title . '</h4>' : '' ) |
| 115 |
. $message |
| 116 |
. '</div>' ); |
| 117 |
if ( $die ) { |
| 118 |
die(); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* Replacement for deprecated image_resize function |
| 124 |
* |
| 125 |
* @param string $file Image file path. |
| 126 |
* @param int $max_w Maximum width to resize to. |
| 127 |
* @param int $max_h Maximum height to resize to. |
| 128 |
* @param bool $crop Optional. Whether to crop image or resize. |
| 129 |
* @param string $suffix Optional. File suffix. |
| 130 |
* @param string $dest_path Optional. New image file path. |
| 131 |
* @param int $jpeg_quality Optional, default is 82. Image quality level (1-100). |
| 132 |
* @return mixed WP_Error on failure. String with new destination path. |
| 133 |
*/ |
| 134 |
function imsanity_image_resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 82 ) { |
| 135 |
if ( function_exists( 'wp_get_image_editor' ) ) { |
| 136 |
$editor = wp_get_image_editor( $file ); |
| 137 |
if ( is_wp_error( $editor ) ) { |
| 138 |
return $editor; |
| 139 |
} |
| 140 |
$editor->set_quality( min( 92, $jpeg_quality ) ); |
| 141 |
|
| 142 |
$ftype = imsanity_quick_mimetype( $file ); |
| 143 |
|
| 144 |
// Return 1 to override auto-rotate. |
| 145 |
$orientation = (int) apply_filters( 'imsanity_orientation', imsanity_get_orientation( $file, $ftype ) ); |
| 146 |
// Try to correct for auto-rotation if the info is available. |
| 147 |
switch ( $orientation ) { |
| 148 |
case 3: |
| 149 |
$editor->rotate( 180 ); |
| 150 |
break; |
| 151 |
case 6: |
| 152 |
$editor->rotate( -90 ); |
| 153 |
break; |
| 154 |
case 8: |
| 155 |
$editor->rotate( 90 ); |
| 156 |
break; |
| 157 |
} |
| 158 |
|
| 159 |
$resized = $editor->resize( $max_w, $max_h, $crop ); |
| 160 |
if ( is_wp_error( $resized ) ) { |
| 161 |
return $resized; |
| 162 |
} |
| 163 |
|
| 164 |
$dest_file = $editor->generate_filename( $suffix, $dest_path ); |
| 165 |
|
| 166 |
// Make sure that the destination file does not exist. |
| 167 |
if ( file_exists( $dest_file ) ) { |
| 168 |
$dest_file = $editor->generate_filename( 'TMP', $dest_path ); |
| 169 |
} |
| 170 |
|
| 171 |
$saved = $editor->save( $dest_file ); |
| 172 |
|
| 173 |
if ( is_wp_error( $saved ) ) { |
| 174 |
return $saved; |
| 175 |
} |
| 176 |
|
| 177 |
return $dest_file; |
| 178 |
} |
| 179 |
return false; |
| 180 |
} |
| 181 |
|