/** * Resize images dynamically using wp built in functions * Original by Victor Teixeira * Modified by Elio Rivero: multisite check, usage of WP Image Editor class * * @global int $blog_id * @param int $attach_id * @param string $img_url * @param int $width * @param int $height * @param bool $crop * @return array */ function themify_do_img($attach_id = null, $img_url = null, $width, $height, $crop = false) { $upload_info = wp_upload_dir(); $upload_url = $upload_info['baseurl']; // this is an attachment, so we have the ID if ($attach_id) { $image_src = wp_get_attachment_image_src($attach_id, 'full'); $file_path = get_attached_file($attach_id); // this is not an attachment, let's use the image url } elseif ($img_url) { $http = 'http://'; $https = 'https://'; if (false !== stripos($img_url, $https)) { // if image url begins with https:// make $upload_url match the scheme $upload_url = str_replace($http, $https, $upload_url); } elseif (false !== stripos($img_url, $http)) { // if image url begins with http:// make $upload_url match the scheme $upload_url = str_replace($https, $http, $upload_url); } // If image is remote, return the original image. $url_host = parse_url($img_url, PHP_URL_HOST); $this_host = parse_url(get_site_url(), PHP_URL_HOST); // Return URL as is if it's an image from a different domain if (str_replace('www.', '', $url_host) != str_replace('www.', '', $this_host)) { return array('url' => $img_url); } // Define path of image. if (is_multisite() && false === stripos($img_url, $upload_url)) { $rel_path = preg_replace("#http(.*?)files#", '', $img_url); } else { $rel_path = str_replace($upload_url, '', $img_url); } $file_path = $upload_info['basedir'] . $rel_path; /** * Keeps width and height of original image. * @var array $orig_size */ $orig_size = getimagesize($file_path); $image_src[0] = $img_url; $image_src[1] = $orig_size[0]; $image_src[2] = $orig_size[1]; } else { // if nothing was provided, return original image url return array('url' => $img_url); } $file_info = pathinfo($file_path); $extension = isset($file_info['extension']) ? '.' . $file_info['extension'] : ''; // the image path without the extension if (isset($file_info['dirname']) && isset($file_info['filename'])) { $no_ext_path = $file_info['dirname'] . '/' . $file_info['filename']; } else { // we can't do anything, return original image url return array('url' => $img_url); } add_filter('image_resize_dimensions', 'themify_img_resize_dimensions', 10, 5); $dims = image_resize_dimensions($image_src[1], $image_src[2], $width, $height, $crop); $new_w = $dims[4]; $new_h = $dims[5]; // Expected image path $cropped_img_path = $no_ext_path . '-' . $new_w . 'x' . $new_h . $extension; if ($image_src[1] == $new_w && $image_src[2] == $new_h) { // default output - without resizing $ql_image = array('url' => $image_src[0], 'width' => $image_src[1], 'height' => $image_src[2]); return $ql_image; } // check if the resized version exists (for $crop = true, also works for $crop = false if the sizes match) if (is_file($cropped_img_path)) { $cropped_img_url = str_replace(basename($image_src[0]), basename($cropped_img_path), $image_src[0]); if (is_multisite() && false === stripos($img_url, $upload_url)) { $cropped_img_url = preg_replace("#http(.*?)files#", $upload_url, $cropped_img_url); } if (themify_maybe_do_retina_size()) { $img_file = str_replace($new_w . 'x' . $new_h, $new_w . 'x' . $new_h . '@2x', $cropped_img_path); themify_do_retina_img($img_url, $width, $height, $crop, $new_w, $new_h, $image_src, $img_file, $file_path); } $ql_image = array('url' => $cropped_img_url, 'width' => $new_w, 'height' => $new_h); return $ql_image; } // no cache files - let's finally resize it $image = wp_get_image_editor($file_path); if (!is_wp_error($image)) { $image->set_quality(95); $image->resize($new_w, $new_h, $crop); $img_file = $image->generate_filename(); $new_image = $image->save($img_file); $new_img = str_replace(basename($image_src[0]), basename($new_image['path']), $image_src[0]); if (is_multisite() && false === stripos($img_url, $upload_url)) { $new_img = preg_replace("#http(.*?)files#", $upload_url, $new_img); } remove_filter('image_resize_dimensions', 'themify_img_resize_dimensions'); // resized output $ql_image = array('url' => $new_img, 'width' => $new_image['width'], 'height' => $new_image['height']); // Add the resized dimensions to original image metadata, // so we can delete resized images if the original image is deleted from Media Library $attachment_id = themify_get_attachment_id_from_url($img_url); if ($attachment_id) { $metadata = wp_get_attachment_metadata($attachment_id); if (isset($metadata['image_meta'])) { $metadata['image_meta']['resized_images'][] = $new_w . 'x' . $new_h; wp_update_attachment_metadata($attachment_id, $metadata); } } if (themify_maybe_do_retina_size()) { themify_do_retina_img($img_url, $width, $height, $crop, $new_w, $new_h, $image_src, $img_file, $file_path); } // Return resized image return $ql_image; } else { // there was an error, return original image url return array('url' => $img_url); } }
/** * Enqueues the chosen skin, if there was one selected and custom_style.css if it exists * @since 1.7.4 */ function themify_enqueue_framework_assets() { // Skin stylesheet if ($skin = themify_get('skin')) { wp_enqueue_style('themify-skin', themify_https_esc($skin), array('theme-style'), THEMIFY_VERSION); } // User stylesheet if (is_file(get_template_directory() . '/custom_style.css')) { wp_enqueue_style('custom-style', THEME_URI . '/custom_style.css', array('theme-style'), THEMIFY_VERSION); } // Enqueue font icon stylesheet wp_enqueue_style('themify-icon-font', THEMIFY_URI . '/fontawesome/css/font-awesome.min.css', array(), THEMIFY_VERSION); // Checks if user enabled retina images. if (themify_maybe_do_retina_size()) { // Enqueues retina detection wp_enqueue_script('themify-retina', THEMIFY_URI . '/js/retina.js', array(), THEMIFY_VERSION, true); } }