| 1 |
<?php |
| 2 |
|
| 3 |
if ( ! function_exists( 'qi_blocks_get_attachment_id_from_url' ) ) { |
| 4 |
/** |
| 5 |
* Function that retrieves attachment id for passed attachment url |
| 6 |
* |
| 7 |
* @param string $attachment_url |
| 8 |
* |
| 9 |
* @return null|string |
| 10 |
*/ |
| 11 |
function qi_blocks_get_attachment_id_from_url( $attachment_url ) { |
| 12 |
global $wpdb; |
| 13 |
$attachment_id = ''; |
| 14 |
|
| 15 |
if ( '' !== $attachment_url ) { |
| 16 |
|
| 17 |
$attachment_id = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE guid=%s", $attachment_url ) ); |
| 18 |
|
| 19 |
// Additional check for undefined reason when guid is not image src |
| 20 |
if ( empty( $attachment_id ) ) { |
| 21 |
$modified_url = substr( $attachment_url, strrpos( $attachment_url, '/' ) + 1 ); |
| 22 |
|
| 23 |
// Get attachment id |
| 24 |
$attachment_id = $wpdb->get_var( $wpdb->prepare( "SELECT post_id FROM $wpdb->postmeta WHERE meta_key='_wp_attached_file' AND meta_value LIKE %s", '%' . $modified_url . '%' ) ); |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
return $attachment_id; |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
if ( ! function_exists( 'qi_blocks_resize_image' ) ) { |
| 33 |
/** |
| 34 |
* Function that generates custom thumbnail for given attachment |
| 35 |
* |
| 36 |
* @param int|string $attachment - attachment id or url of image to resize |
| 37 |
* @param array $custom_size desired - width and height of custom thumbnail |
| 38 |
* @param bool $crop - whether to crop image or not |
| 39 |
* |
| 40 |
* @return array returns array containing img_url, width and height |
| 41 |
* |
| 42 |
* @see qi_blocks_get_attachment_id_from_url() |
| 43 |
* @see get_attached_file() |
| 44 |
* @see wp_get_attachment_url() |
| 45 |
* @see wp_get_image_editor() |
| 46 |
*/ |
| 47 |
function qi_blocks_resize_image( $attachment, $custom_size = array(), $crop = true ) { |
| 48 |
$return_array = array(); |
| 49 |
|
| 50 |
if ( ! empty( $attachment ) ) { |
| 51 |
if ( is_int( $attachment ) ) { |
| 52 |
$attachment_id = $attachment; |
| 53 |
} else { |
| 54 |
$attachment_id = qi_blocks_get_attachment_id_from_url( $attachment ); |
| 55 |
} |
| 56 |
|
| 57 |
$is_size_set = ! empty( $custom_size ) && isset( $custom_size['width'] ) && isset( $custom_size['height'] ); |
| 58 |
|
| 59 |
if ( ! empty( $attachment_id ) && $is_size_set ) { |
| 60 |
$width = intval( $custom_size['width'] ); |
| 61 |
$height = intval( $custom_size['height'] ); |
| 62 |
|
| 63 |
// Get file path of the attachment |
| 64 |
$img_path = get_attached_file( $attachment_id ); |
| 65 |
|
| 66 |
// Get attachment url |
| 67 |
$img_url = wp_get_attachment_url( $attachment_id ); |
| 68 |
|
| 69 |
// Break down img path to array, so we can use its components in building thumbnail path |
| 70 |
$img_path_array = pathinfo( $img_path ); |
| 71 |
|
| 72 |
// Build thumbnail path |
| 73 |
$new_img_path = $img_path_array['dirname'] . '/' . $img_path_array['filename'] . '-' . $width . 'x' . $height . '.' . $img_path_array['extension']; |
| 74 |
|
| 75 |
// Build thumbnail url |
| 76 |
$new_img_url = str_replace( $img_path_array['filename'], $img_path_array['filename'] . '-' . $width . 'x' . $height, $img_url ); |
| 77 |
|
| 78 |
// Check if thumbnail exists by its path |
| 79 |
if ( ! file_exists( $new_img_path ) ) { |
| 80 |
// Get image manipulation object |
| 81 |
$image_object = wp_get_image_editor( $img_path ); |
| 82 |
|
| 83 |
if ( ! is_wp_error( $image_object ) ) { |
| 84 |
// Resize image and save it new to path |
| 85 |
$image_object->resize( $width, $height, $crop ); |
| 86 |
$image_object->save( $new_img_path ); |
| 87 |
|
| 88 |
// Get sizes of newly created thumbnail. |
| 89 |
// We don't use $width and $height because those might differ from end result based on $crop parameter |
| 90 |
$image_sizes = $image_object->get_size(); |
| 91 |
|
| 92 |
$width = $image_sizes['width']; |
| 93 |
$height = $image_sizes['height']; |
| 94 |
|
| 95 |
qi_blocks_update_cropped_images_option( $attachment_id, $width, $height ); |
| 96 |
} else { |
| 97 |
return array(); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
// Generate data to be returned |
| 102 |
$return_array = array( |
| 103 |
'url' => $new_img_url, |
| 104 |
'width' => $width, |
| 105 |
'height' => $height, |
| 106 |
); |
| 107 |
|
| 108 |
// Attachment wasn't found in gallery, but it is not empty |
| 109 |
} elseif ( '' !== $attachment && $is_size_set ) { |
| 110 |
$width = intval( $custom_size['width'] ); |
| 111 |
$height = intval( $custom_size['height'] ); |
| 112 |
|
| 113 |
// Generate data to be returned |
| 114 |
$return_array = array( |
| 115 |
'url' => $attachment, |
| 116 |
'width' => $width, |
| 117 |
'height' => $height, |
| 118 |
); |
| 119 |
} |
| 120 |
} |
| 121 |
|
| 122 |
return $return_array; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
if( ! function_exists( 'qi_blocks_update_cropped_images_option' ) ) { |
| 127 |
function qi_blocks_update_cropped_images_option( $attachment_id, $width, $height ) { |
| 128 |
$cropped_images = get_option( 'qi_blocks_cropped_images' ); |
| 129 |
|
| 130 |
$cropped_images[] = array( |
| 131 |
'attachment_id' => $attachment_id, |
| 132 |
'width' => $width, |
| 133 |
'height' => $height, |
| 134 |
); |
| 135 |
|
| 136 |
update_option( 'qi_blocks_cropped_images', $cropped_images ); |
| 137 |
} |
| 138 |
} |
| 139 |
|