How to Automatic Crop Image in WordPress

Shortlink:

Today i will teach you How to Automatic Crop Image in WordPress, if you know how to development WordPress theme, then you need to this functions. examples: If you have WordPress themes all posts featured images option, and when you written any articles and when publish your article then you can use featured image, so when you upload any width and height then automatic crop your require size then automatic saved in your post. so today i will teach you how to do this work.

Default Crop

WordPress default crop functions is

thumbnail: Thumbnail (default 150px x 150px max)
medium: Medium resolution (default 300px x 300px max)
large: Large resolution (default 640px x 640px max)
full: Original image resolution (unmodified)

If you want then you can use this functions name. if you have upload any image then automatic crop your image is WordPress default functions.  so if you need this size image then you don’t need create custom functions just use above functions code, but if you need any custom crop then you can create custom functions.

Note: if you want to use any crop functions and posts featured image then you have to must be  use below function code. you will check your theme functions.php page if you can’t find this code then you will input this codes.

add_theme_support( 'post-thumbnails');

 

 

And If you want to use custom crop functions then you can use this code in your theme functions.php file.

add_image_size($name, $width, $height, $cropBoolean);

Example:

add_image_size( 'post-image', 600, 200, true );

Note: 1. post-image: this is unique id, 2. 600: 600px width, 3. 200: 200px height.

You can see WordPress codex Post Thumbnails for good recurse

And input below code for show your featured image

<?php the_post_thumbnail('post-image'); ?>

Images URL

If you need Featured images url then you can use below code

If you want to use WordPress resize just add your size, I’ve added large size image in example

$your_variable = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'large' );

And input below code for show your featured image

echo $your_variable[0];

If you want to use your custom size

$your_variable = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'post-image' ); 

 

And input below code for show your featured image

echo $your_variable[0];

You can get lot of resource

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.

Subscribe For Latest Updates

Signup for our newsletter and get notified when we publish new articles for free!




%d bloggers like this: