PluginProbe
Qi Blocks / trunk
Qi Blocks vtrunk
1.5.3 1.5.2 1.5.1 trunk 1.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1 1.1.1 1.1.2 1.2 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.2.6 1.2.7 All 45 releases
qi-blocks / inc / media / helper.php

helper.php in Qi Blocks trunk, at inc/media/helper.php

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