Resize images dynamically using wp built in functions. For : php 5.2+
Example ::
---------------------------------------------
<?php
$thumb = get_post_thumbnail_id();
$image = vt_resize( $thumb,'' , 140, 110, true );
?>
<img src="<?php echo $image[url]; ?>" width="<?php echo $image[width]; ?>" height="<?php echo $image[height]; ?>" />
----------------------------------------------
Put the below code in theme function.php file
----------------------------------------------
function vt_resize( $attach_id = null, $img_url = null, $width, $height, $crop = false ) {
if( $attach_id ) {
$img_url = get_attached_file( $attach_id );
}
$old_img_info = pathinfo( $img_url );
$old_img_ext = '.'. $old_img_info['extension'];
$old_img_path = $old_img_info['dirname'] .'/'. $old_img_info['filename'];
$new_img_dir = str_replace(ABSPATH, '/', $old_img_info['dirname']);
$new_img_path = $old_img_path .'-'. $width .'x'. $height . $old_img_ext;
$new_img = wp_get_image_editor( $img_url );
$new_img->resize( $width, $height, $crop );
$new_img = $new_img->save( $new_file_path );
$vt_image = array (
'url' => $new_img_dir .'/'. $new_img['file'],
'width' => $new_img['width'],
'height' => $new_img['height']
);
return $vt_image;
}
-------------------------------------------------
Let me know your thoughts and questions in the comments.
Email: vyasankit2008@gmail.com
Example ::
---------------------------------------------
<?php
$thumb = get_post_thumbnail_id();
$image = vt_resize( $thumb,'' , 140, 110, true );
?>
<img src="<?php echo $image[url]; ?>" width="<?php echo $image[width]; ?>" height="<?php echo $image[height]; ?>" />
----------------------------------------------
Put the below code in theme function.php file
----------------------------------------------
function vt_resize( $attach_id = null, $img_url = null, $width, $height, $crop = false ) {
if( $attach_id ) {
$img_url = get_attached_file( $attach_id );
}
$old_img_info = pathinfo( $img_url );
$old_img_ext = '.'. $old_img_info['extension'];
$old_img_path = $old_img_info['dirname'] .'/'. $old_img_info['filename'];
$new_img_dir = str_replace(ABSPATH, '/', $old_img_info['dirname']);
$new_img_path = $old_img_path .'-'. $width .'x'. $height . $old_img_ext;
$new_img = wp_get_image_editor( $img_url );
$new_img->resize( $width, $height, $crop );
$new_img = $new_img->save( $new_file_path );
$vt_image = array (
'url' => $new_img_dir .'/'. $new_img['file'],
'width' => $new_img['width'],
'height' => $new_img['height']
);
return $vt_image;
}
-------------------------------------------------
Let me know your thoughts and questions in the comments.
Email: vyasankit2008@gmail.com
No comments:
Post a Comment