Esempio n. 1
0
 /**
  * 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;
 }
Esempio n. 2
0
 /**
  * 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;
 }