Created
July 23, 2014 19:09
-
-
Save drrobotnik/723ba43b00e57c01952f to your computer and use it in GitHub Desktop.
resize
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php /** | |
* On the fly image cropping based on WP 3.5 image editor | |
* | |
* @since 1.0 | |
* | |
* @param int $id | |
* @param int $width | |
* @param int $height | |
* @param boolean $crop | |
* @return string | |
*/ | |
function cv_resize( $id = 0, $width = 50, $height = 50, $crop = true ){ | |
// Check if attachment is an image | |
if ( !$id || !wp_attachment_is_image($id) ) | |
return false; | |
$upload_dir = wp_upload_dir(); | |
$img_meta = wp_get_attachment_metadata( $id ); | |
if( empty($img_meta) ) | |
return false; | |
// attachment url converted to image path | |
$file = $upload_dir['basedir'] . '/' . $img_meta['file']; | |
$image = wp_get_image_editor( $file ); | |
// legacy error explanation. | |
if ( is_wp_error( $image ) ) | |
return false; | |
// generate intended file name and check if the image exists. | |
$new_file_path = $image->generate_filename($suffix = $width.'x'.$height); | |
// If this file already exists, return the image url. | |
if(file_exists($new_file_path)) | |
return str_replace($upload_dir['basedir'], $upload_dir['baseurl'], $new_file_path); | |
// resize image and save | |
$image->resize( $width, $height, $crop ); | |
$new_image_info = $image->save(); | |
// legacy error explanation. | |
if ( is_wp_error( $new_image_info ) ) | |
return false; | |
// convert path to url | |
$url = str_replace($upload_dir['basedir'], $upload_dir['baseurl'], $new_image_info['path']); | |
return $url; | |
} | |
function cv_resize_cached( $id = 0, $width = 50, $height = 50, $crop = true, $timeout = HOUR_IN_SECONDS ){ | |
$transient = 'cv_gallery_'.$id.'_'.$width.'_'.$height.'_'.$crop; | |
if ( false === ( $result = get_transient( $transient ) ) ) { | |
$result = cv_resize( $id, $width, $height, $crop ); | |
if( !empty( $result ) ) | |
set_transient( $transient, $result, $timeout ); | |
} | |
return $result; | |
} | |
function cv_resize_thumbnail( $id = 0, $width = 50, $height = 50, $crop = true, $attr = '' ){ | |
$src = cv_resize_cached( $id, $width, $height, $crop ); | |
$hwstring = image_hwstring($width, $height); | |
$size = $width . 'x' . $height; | |
$attachment = get_post($id); | |
$default_attr = array( | |
'src' => $src, | |
'class' => "attachment-$size", | |
'alt' => trim(strip_tags( get_post_meta($id, '_wp_attachment_image_alt', true) )), // Use Alt field first | |
); | |
if ( empty($default_attr['alt']) ) | |
$default_attr['alt'] = trim(strip_tags( $attachment->post_excerpt )); // If not, Use the Caption | |
if ( empty($default_attr['alt']) ) | |
$default_attr['alt'] = trim(strip_tags( $attachment->post_title )); // Finally, use the title | |
$attr = wp_parse_args($attr, $default_attr); | |
$attr = apply_filters( 'wp_get_attachment_image_attributes', $attr, $attachment ); | |
$attr = array_map( 'esc_attr', $attr ); | |
$html = rtrim("<img $hwstring"); | |
foreach ( $attr as $name => $value ) { | |
$html .= " $name=" . '"' . $value . '"'; | |
} | |
$html .= ' />'; | |
return $html; | |
} | |
function cv_resize_gallery( $ids = array(), $width = 50, $height = 50, $crop = true, $timeout = DAY_IN_SECONDS ){ | |
$list = implode('_', $ids); | |
$transient = 'cv_gallery_'.$list.'_'.$width.'_'.$height.'_'.$crop; | |
if ( false === ( $result = get_transient( $transient ) ) ) { | |
$result = array(); | |
foreach ($ids as $id) { | |
$result[] = cv_resize( $id, $width, $height, $crop ); | |
} | |
set_transient( $transient, $result, $timeout ); | |
} | |
return $result; | |
} | |
function cv_resize_sizes( $id = 0, $sizes = array(), $crop = true, $timeout = DAY_IN_SECONDS ){ | |
$dimensions = array_map(function($item) { return $item['width'].'x'.$item['height']; }, $sizes); | |
$output = implode('_', $dimensions); | |
$transient = 'cv_sizes_'.$id.'_'.md5($output).'_'.$crop; | |
if ( false === ( $result = get_transient( $transient ) ) ) { | |
$result = array(); | |
foreach ($sizes as $size) { | |
$width = $size['width']; | |
$height = $size['height']; | |
$result[] = cv_resize( $id, $width, $height, $crop ); | |
} | |
set_transient( $transient, $result, $timeout ); | |
} | |
return $result; | |
} | |
function tt_get_resized_attachment_url( $id = 0, $width = 50, $height = 50, $crop = true ) { | |
return ( defined('WPCOM_IS_VIP_ENV') && true === WPCOM_IS_VIP_ENV ) ? wpcom_vip_get_resized_attachment_url( $id, $width, $height, $crop ) : cv_resize_cached( $id, $width, $height, $crop ); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment