Im brand new to coding in wordpress, usually I use ModX. In ModX there is a plugin called phpthumb which automatically crops images to a specified h and w. How can I achieve this in Wordpress?

Image Code:

<?php
$thumbnail_id = get_post_thumbnail_id($post->ID);
$thumbnail = wp_get_attachment_image_src($thumbnail_id,'featured works');
echo '<div data-src="'.$thumbnail[0].'" alt="[[+name]]"></div>';
?>

Thanks for any insight.

up vote 2 down vote accepted

You can use add_image_size to create your custom size like this:

add_image_size( 'featured-works', 220, 180, true );

then get it like this:

wp_get_attachment_image_src($thumbnail_id,'featured-works');

take into account how the crop works here

If you use the add_image_size() function you won't be able to crop items "on-the-fly" like phpthumb. But there are scripts out there you can use for that, here is one of the popular ones - https://github.com/syamilmj/Aqua-Resizer - this one uses URL's for the input though. Personally I would recommend tweaking it a bit to take in attachment "ID's" instead of URL's this way you can use the core functions - wp_get_attachment_metadata(), get_attached_file() and wp_get_attachment_image_src() to return data this way it passes through the WP filters so it can better support 3rd party plugins. Of course there are many using it as is which works very good.

You can also check out this great plugin - https://wordpress.org/plugins/fly-dynamic-image-resizer/

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.