'300', 'resize' => array( 123, 456 ) ), or in string form (w=123&h=456). * @param string|null $scheme One of http or https. * @return string The raw final URL. You should run this through esc_url() before displaying it. */ function thumbor_url( $image_url, $args = [], $scheme = null ) { if ( ! defined( 'THUMBOR_URL' ) || empty( THUMBOR_URL ) ) { return; } /* * Cache result for unique set of args to save reruns. This is because we're seeing the same image being re-run within a single * request and there's a chance that filters applied within are expensive. A short TTL is used in case persistent cache is used * but it doesn't need to be longed lived. */ $cache_key = md5( $image_url . json_encode( $args ) . ( $scheme ?? '' ) ); $cache_ttl = 60; $cached_url = wp_cache_get( $cache_key, 'thumbor_url' ); if ( $cached_url ) { return $cached_url; } $upload_dir = wp_upload_dir(); $upload_baseurl = $upload_dir['baseurl']; if ( is_multisite() ) { $upload_baseurl = preg_replace( '#/sites/[\d]+#', '', $upload_baseurl ); } $image_url = trim( $image_url ); $image_file = basename( parse_url( $image_url, PHP_URL_PATH ) ); $image_url = str_replace( $image_file, urlencode( $image_file ), $image_url ); if ( strpos( $image_url, $upload_baseurl ) !== 0 ) { return $image_url; } if ( false !== apply_filters( 'thumbor_skip_for_url', false, $image_url, $args, $scheme ) ) { return $image_url; } $image_url = apply_filters( 'thumbor_pre_image_url', $image_url, $args, $scheme ); $args = apply_filters( 'thumbor_pre_args', $args, $image_url, $scheme ); if ( isset( $args['fit'] ) ) { $scale = 'fit-in'; $width = $args['fit'][0]; $height = $args['fit'][1]; } elseif ( isset( $args['resize'] ) ) { $scale = null; $width = $args['resize'][0]; $height = $args['resize'][1]; } elseif ( isset( $args['w'] ) ) { $scale = 'fit-in'; $width = $args['w']; $height = 'orig'; } elseif ( isset( $args['h'] ) ) { $scale = 'fit-in'; $width = 'orig'; $height = $args['h']; } else { $scale = 'fit-in'; $width = 'orig'; $height = 'orig'; } $url_parts = [ 'scale' => $scale, 'size' => "{$width}x{$height}", 'filters' => null, 'smart' => null, ]; $thumbor_url = implode( '/', array_filter( $url_parts ) ) . '/' . urlencode( $image_url ); if ( defined( 'THUMBOR_SECRET_KEY' ) && ! empty( THUMBOR_SECRET_KEY ) ) { $signature = hash_hmac( 'sha1', $thumbor_url, THUMBOR_SECRET_KEY, true ); $thumbor_url = rtrim( THUMBOR_URL, '/' ) . '/' . strtr( base64_encode( $signature ), '/+', '_-' ) . '/' . $thumbor_url; } else { $thumbor_url = rtrim( THUMBOR_URL, '/' ) . '/unsafe/' . $thumbor_url; } /** * Allows a final modification of the generated Thumbor URL. * * @param string $thumbor_url The final Thumbor image URL including query args. * @param string $image_url The image URL without query args. * @param array $args A key value array of the query args appended to $image_url. */ $final_thumbor_url = apply_filters( 'thumbor_url', $thumbor_url, $image_url, $args ); // Cache result to save reruns wp_cache_set( $cache_key, $final_thumbor_url, 'thumbor_url', $cache_ttl ); return $final_thumbor_url; }