Last active
August 29, 2015 14:25
-
-
Save iamcanadian1973/89ca0e975cde962a4096 to your computer and use it in GitHub Desktop.
WordPress remote image resize
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| /** | |
| * WordPress - Resize and save remote images | |
| * | |
| * @author Kyle Rumble | |
| * | |
| * @param string $image | |
| * @param string $new_name | |
| * @param int $width | |
| * @param int $height | |
| * @param string $sub_directory | |
| * @param boolean $regenerate (default: FALSE) | |
| * @return string | |
| */ | |
| function _remote_img_resize_and_save( $image, $new_name, $width, $height, $sub_directory, $regenerate = FALSE ) { | |
| $upload_dir = wp_upload_dir(); | |
| $basedir = $upload_dir['basedir']; | |
| $baseurl = $upload_dir['baseurl']; | |
| $file_info = pathinfo( $image ); | |
| $file_name = $file_info['filename']; | |
| $extension = '.'. $file_info['extension']; | |
| $new_file_name = $new_name.'-'.$width.'x'.$height.$extension; | |
| $new_file_full_path = trailingslashit( $basedir ) . $sub_directory . '/' . $new_file_name; | |
| $new_file_full_url = trailingslashit( $baseurl ) . $sub_directory . '/' . $new_file_name; | |
| // To Do: set caching by filemtime | |
| // return resized image if exists | |
| if ( file_exists( $new_file_full_path ) && !$regenerate ) { | |
| return $new_file_full_url; | |
| } | |
| $img = wp_get_image_editor( $image ); | |
| if ( ! is_wp_error( $img ) ) { | |
| $img->resize( $width, $height, true ); | |
| $details = $img->save( $new_file_full_path ); | |
| return $new_file_full_url; | |
| } | |
| else { | |
| return $image; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment