Function Reference: geodir_exif
Summary
Fixes image orientation.
Package
GeoDirectory
Parameters
- $file
- (array) (required) The image file.
- Default: None
Return Values
- (mixed)
- Image file.
Change Log
Since: 1.0.0
Filters
‘geodir_image_upload_set_quality’ [Line: 1937]
Source File
geodir_exif() is located in geodirectory-functions/custom_functions.php [Line: 1846]
Source Code
function geodir_exif( $file ) { if ( empty( $file ) || ! is_array( $file ) ) { return $file; } $file_path = ! empty( $file['tmp_name'] ) ? sanitize_text_field( $file['tmp_name'] ) : ''; if ( ! ( $file_path && file_exists( $file_path ) ) ) { return $file; } $file['file'] = $file_path; if ( ! file_is_valid_image( $file_path ) ) { return $file; // Bail if file is not an image. } if ( ! function_exists( 'wp_get_image_editor' ) ) { return $file; } $mime_type = $file['type']; $exif = array(); if ( $mime_type == 'image/jpeg' && function_exists( 'exif_read_data' ) ) { try { $exif = exif_read_data( $file_path ); } catch ( Exception $e ) { $exif = array(); } } $rotate = false; $flip = false; $modify = false; $orientation = 0; if ( ! empty( $exif ) && isset( $exif['Orientation'] ) ) { switch ( (int) $exif['Orientation'] ) { case 1: // do nothing break; case 2: $flip = array( false, true ); $modify = true; break; case 3: $orientation = - 180; $rotate = true; $modify = true; break; case 4: $flip = array( true, false ); $modify = true; break; case 5: $orientation = - 90; $rotate = true; $flip = array( false, true ); $modify = true; break; case 6: $orientation = - 90; $rotate = true; $modify = true; break; case 7: $orientation = - 270; $rotate = true; $flip = array( false, true ); $modify = true; break; case 8: case 9: $orientation = - 270; $rotate = true; $modify = true; break; default: $orientation = 0; $rotate = true; $modify = true; break; } } $quality = null; /** * Filter the image quality. * * @since 1.5.7 * * @param int|null $quality Image Compression quality between 1-100% scale. Default null. * @param string $quality Image mime type. */ $quality = apply_filters( 'geodir_image_upload_set_quality', $quality, $mime_type ); if ( $quality !== null ) { $modify = true; } if ( ! $modify ) { return $file; // no change } $image = wp_get_image_editor( $file_path ); if ( ! is_wp_error( $image ) ) { if ( $rotate ) { $image->rotate( $orientation ); } if ( ! empty( $flip ) ) { $image->flip( $flip[0], $flip[1] ); } if ( $quality !== null ) { $image->set_quality( (int) $quality ); } $result = $image->save( $file_path ); if ( ! is_wp_error( $result ) ) { $file['file'] = $result['path']; $file['tmp_name'] = $result['path']; } } // The image orientation is fixed, pass it back for further processing return $file; }