/** * Resize images dynamically using wp built in functions * * @param string $img_url * @param int $width * @param int $height * @param bool $crop * @return array */ function themify_do_img($img_url = null, $width, $height, $crop = false) { $src = esc_url($img_url); $upload_dir = wp_upload_dir(); $base_url = $upload_dir['baseurl']; // Check if the image is an attachment. If it's external return url, width and height. if (substr($src, -strlen($base_url)) === $base_url) { return array('url' => $src, 'width' => $width, 'height' => $height); } // Get post's attachment meta data to look for references to the requested image size $attachment_id = themify_get_attachment_id_from_url($src, $base_url); // If no relationship between a post and a image size was found, return url, width and height. if (!$attachment_id) { return array('url' => $src, 'width' => $width, 'height' => $height); } // Go through the attachment meta data sizes looking for an image size match. $meta = wp_get_attachment_metadata($attachment_id); if (is_array($meta) && isset($meta['sizes']) && is_array($meta['sizes'])) { foreach ($meta['sizes'] as $key => $size) { if ($size['width'] == $width && $size['height'] == $height) { setlocale(LC_CTYPE, get_locale() . '.UTF-8'); return array('url' => str_replace(basename($src), $size['file'], $src), 'width' => $width, 'height' => $height); } } } // Requested image size doesn't exists, so let's create one if (true == $crop) { add_filter('image_resize_dimensions', 'themify_img_resize_dimensions', 10, 5); } $image = themify_make_image_size($attachment_id, $width, $height, $meta, $src); if (true == $crop) { remove_filter('image_resize_dimensions', 'themify_img_resize_dimensions', 10); } return $image; }
/** * Resize images dynamically using wp built in functions * * @param string $img_url * @param int $width * @param int $height * @param bool $crop * @return array */ function themify_do_img($img_url = null, $width, $height, $crop = false) { $img_url = esc_url($img_url); $upload_dir = wp_upload_dir(); $base_url = $upload_dir['baseurl']; // Check if the image is an attachment. If it's external return url, width and height. if (substr($img_url, -strlen($base_url)) === $base_url) { return array('url' => $img_url, 'width' => $width, 'height' => $height); } // Get post's attachment meta data to look for references to the requested image size $attachment_id = themify_get_attachment_id_from_url($img_url, $base_url); // If no relationship between a post and a image size was found, return url, width and height. if (!$attachment_id) { return array('url' => $img_url, 'width' => $width, 'height' => $height); } // Fetch attachment meta data. Up to this point we know the attachment ID is valid. $meta = wp_get_attachment_metadata($attachment_id); // Go through the attachment meta data sizes looking for an image size match. if (is_array($meta) && isset($meta['sizes']) && is_array($meta['sizes'])) { // Perform calculations when height = 0 if (is_null($height) || 0 === $height || '0' === $height) { // If width and height or original image are available as metadata if (isset($meta['width']) && isset($meta['height'])) { // Divide width by original image aspect ratio to obtain projected height // The floor function is used so it returns an int and metadata can be written $height = floor($width / ($meta['width'] / $meta['height'])); } else { $height = 0; } } foreach ($meta['sizes'] as $key => $size) { if ($size['width'] == $width && $size['height'] == $height) { setlocale(LC_CTYPE, get_locale() . '.UTF-8'); return array('url' => str_replace(basename($img_url), $size['file'], $img_url), 'width' => $width, 'height' => $height, 'attachment_id' => $attachment_id); } } } elseif (is_array($meta) && (!isset($meta['sizes']) || !is_array($meta['sizes']))) { // If the meta is an array, but doesn't have the 'sizes' element or if it has it, it's not an array, // return original image since there's something wrong with this attachment meta data: it exists, // but its format is not correct. return array('url' => $img_url, 'width' => $width, 'height' => $height, 'attachment_id' => $attachment_id); } // Requested image size doesn't exists, so let's create one if (true == $crop) { add_filter('image_resize_dimensions', 'themify_img_resize_dimensions', 10, 5); } // Patch meta because if we're here, there's a valid attachment ID for sure, but maybe the meta data is not ok. if (!is_array($meta) && empty($meta)) { $meta['sizes'] = array('large' => array()); } // Generate image returning an array with image url, width and height. If image can't generated, original url, width and height are used. $image = themify_make_image_size($attachment_id, $width, $height, $meta, $img_url); if (true == $crop) { remove_filter('image_resize_dimensions', 'themify_img_resize_dimensions', 10); } $image['attachment_id'] = $attachment_id; return $image; }
/** * Returns the post image, either from Themify Custom Panel fields or from WordPress Featured Image. * @param string $args Format string. * @return string String with <img> tag and optional content prepended and/or appended */ function themify_get_image($args) { global $themify; /** * List of parameters * @var array */ $args = wp_parse_args($args, array('id' => '', 'src' => '', 'class' => '', 'ignore' => '', 'w' => '', 'h' => '', 'before' => '', 'after' => '', 'alt' => '', 'crop' => true, 'setting' => '', 'field_name' => 'post_image,image,wp_thumb,feature_image', 'urlonly' => false, 'image_size' => '', 'image_meta' => false, 'f_image' => false)); /** * Post ID for single, query or archive views. * Page ID is stored separately in $themify->page_id. * @var string */ $post_id = get_the_ID(); /** * URL of the image to use * @var string */ $img_url = ''; /** * Image width * @var string */ $width = ''; /** * Image height * @var string */ $height = ''; /** * Alt text of the attachment * @var string */ $img_alt = ''; // If ignore is set, just use width and height passed if (!empty($args['ignore']) && !themify_is_image_script_disabled() || isset($themify->is_shortcode) && $themify->is_shortcode) { if (!empty($args['w'])) { $width = $args['w']; } if (!empty($args['h'])) { $height = $args['h']; } } elseif (in_the_loop()) { // Main query area if (is_single()) { // Single Entry if (!($width = get_post_meta($post_id, 'image_width', true))) { if (!($width = themify_get('setting-default_' . get_post_type() . '_single_image_post_width'))) { $width = themify_get("setting-{$args['setting']}_width"); } } if (!($height = get_post_meta($post_id, 'image_height', true))) { if (!($height = themify_get('setting-default_' . get_post_type() . '_single_image_post_height'))) { $height = themify_get("setting-{$args['setting']}_height"); } } } elseif (themify_is_query_page()) { // Query pages like Query Posts or Query Portfolios $query_post_type = isset($themify->query_post_type) && 'post' != $themify->query_post_type ? $themify->query_post_type . '_' : ''; if (!($width = get_post_meta($themify->page_id, $query_post_type . 'image_width', true))) { if (!($width = themify_get('setting-default_' . get_post_type() . '_index_image_post_width'))) { $width = themify_get('setting-image_post_width'); } } if (!($height = get_post_meta($themify->page_id, $query_post_type . 'image_height', true))) { if (!($height = themify_get('setting-default_' . get_post_type() . '_index_image_post_height'))) { $height = themify_get('setting-image_post_height'); } } } elseif (is_archive() || is_tax() || is_search() || is_home()) { // Category, Tag, Author, Date || Custom Taxonomy || Search if (!($width = themify_get('setting-default_' . get_post_type() . '_index_image_post_width'))) { $width = themify_get('setting-image_post_width'); } if (!($height = themify_get('setting-default_' . get_post_type() . '_index_image_post_height'))) { $height = themify_get('setting-image_post_height'); } } // Catch height before width so we can check if user entered something for width. if (('' === $height || is_null($height)) && ('' === $width || is_null($width))) { $height = $themify->height; } if ('' === $width || is_null($width)) { $width = $themify->width; } } else { if (!($width = get_post_meta($post_id, 'image_width', true))) { if (!empty($args['h'])) { $height = $args['h']; } } if (!($height = get_post_meta($post_id, 'image_height', true))) { if (!empty($args['w'])) { $width = $args['w']; } } } if (themify_is_image_script_disabled()) { // Use WP standard image sizes if (!empty($args['image_size'])) { // If image_size parameter is set $feature_size = $args['image_size']; } elseif (isset($themify->image_size) && !empty($themify->image_size)) { // or if Themify::image_size is set $feature_size = $themify->image_size; } else { if (in_the_loop() && (!isset($themify->is_shortcode) || !$themify->is_shortcode)) { // Main query area if (is_single()) { $feature_size = get_post_meta($post_id, 'feature_size', true); if (empty($feature_size) || 'blank' == $feature_size) { $feature_size = themify_get('setting-image_post_single_feature_size'); } } elseif (themify_is_query_page()) { $feature_size = get_post_meta($themify->page_id, $query_post_type . 'feature_size_page', true); if (empty($feature_size) || 'blank' == $feature_size) { $feature_size = themify_get('setting-image_post_feature_size'); } } elseif (is_archive() || is_tax() || is_search() || is_home()) { $feature_size = themify_get('setting-image_post_feature_size'); } } } if (!isset($feature_size) || 'blank' == $feature_size) { $feature_size = themify_get('setting-global_feature_size'); if (empty($feature_size) || 'blank' == $feature_size) { $feature_size = apply_filters('themify_global_feature_size', 'large'); } } if (empty($args['src'])) { // Set URL to use for final output. $img_url = themify_image_url(false, $feature_size); } else { $img_url = $args['src']; } } else { // Use Image Script if (empty($args['src'])) { if (has_post_thumbnail()) { $img_url = themify_image_url(); } else { foreach (explode(',', $args['field_name']) as $field) { if ($img_url = get_post_meta($post_id, trim($field), true)) { break; } } } } else { $img_url = $args['src']; } if (0 === $height) { $args['crop'] = false; } // Set URL to use for final output. $temp = themify_do_img($img_url, $width, $height, (bool) $args['crop']); $img_url = $temp['url']; // Get alt text by attachment id if it was returned. if (isset($temp['attachment_id'])) { $img_alt = get_post_meta($temp['attachment_id'], '_wp_attachment_image_alt', true); } } // No image was defined, parse content to find the first image. if (empty($img_url) && ($args['f_image'] || themify_check('setting-auto_featured_image'))) { $content = get_the_content(); foreach (array('img', 'embed', 'iframe') as $tag) { $count = substr_count($content, '<' . $tag); if ($count >= 1) { $start = strpos($content, '<' . $tag, 0); $pos = substr($content, $start); $end = strpos($pos, '>'); $temp = themify_prep_image(substr($pos, 0, $end + 1)); $ext = explode('.', $temp['src']); $ext = strtolower($ext[count($ext) - 1]); if (strpos($temp['src'], '.') && ($ext == 'jpg' || $ext == 'jpeg' || $ext == 'gif' || $ext == 'png')) { $auto_image_url = isset($temp['src']) ? $temp['src'] : ''; $args['class'] .= isset($temp['class']) ? ' ' . $temp['class'] : ''; $args['alt'] = $temp['alt']; if (themify_is_image_script_disabled()) { $upload_dir = wp_upload_dir(); $img_url = themify_image_url(false, $feature_size, themify_get_attachment_id_from_url($auto_image_url, $upload_dir['baseurl'])); if (empty($img_url)) { $img_url = esc_url($auto_image_url); } } elseif ($temp = themify_do_img($auto_image_url, $width, $height, (bool) $args['crop'])) { $img_url = $temp['url']; } break; } } } } if (!empty($img_url)) { if ($args['urlonly']) { $out = $img_url; } else { // Build final image $out = ''; if ($args['image_meta'] == true) { $out .= "<meta itemprop=\"image\" content=\"{$img_url}\"></meta>"; } $out .= "<img src=\"{$img_url}\""; if ($width) { $out .= " width=\"{$width}\""; } if ($height) { $out .= " height=\"{$height}\""; } if (!empty($args['class'])) { $out .= " class=\"{$args['class']}\""; } // If alt was passed as parameter, use it. Otherwise use alt text by attachment id if it was fetched or post title. if (!empty($args['alt'])) { $out_alt = $args['alt']; } elseif (!empty($img_alt)) { $out_alt = $img_alt; } else { $out_alt = the_title_attribute('echo=0'); } $out .= ' alt="' . esc_attr($out_alt) . '" />'; } $out = $args['before'] . $out . $args['after']; } else { $out = ''; } return $out; }
/** * Generate image for high resolution devices. * * @since 1.9.0 * * @param string $img_url * @param int $width * @param int $height * @param bool $crop * @param int $new_w * @param int $new_h * @param array $image_src * @param string $img_file * @param string $file_path * * @return array */ function themify_do_retina_img($img_url, $width, $height, $crop, $new_w, $new_h, $image_src, $img_file, $file_path) { // @2x image file path $destfilename = preg_replace('/([0-9]+)x([0-9]+)/', '$1x$2@2x', $img_file); // check if retina image file exists if (!is_file($destfilename) || !getimagesize($destfilename)) { // Retina dimensions $retina_w = $width * 2; $retina_h = $height * 2; // Get expected image size after cropping $dims_x2 = image_resize_dimensions($image_src[1], $image_src[2], $retina_w, $retina_h, $crop); $dst_x2_w = $dims_x2[4]; $dst_x2_h = $dims_x2[5]; // If possible, make the @2x image if ($dst_x2_h) { $retina_img = wp_get_image_editor($file_path); if (!is_wp_error($retina_img)) { $retina_img->resize($retina_w, $retina_h, $crop); $retina_img->set_quality(95); $suffix = $new_w . 'x' . $new_h . '@2x'; $filename = $retina_img->generate_filename($suffix); $retina_img = $retina_img->save($filename); // 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'][] = $suffix; wp_update_attachment_metadata($attachment_id, $metadata); } } } } } }