Having a problem in the wordpress images when cropping doesn’t follow in your way. This tutorial will help you how can you crop images using functionality code build in to wordpress.

Final Code

Let me explain the code so we can easily know what all the purpose of this final code

This code is to get the absolute path of the images you want to re-crop make sure you have the right post ID more info for get_attached_file.

$image_size is the code you want a specific size for your images to be crop then I just explode it to be able to input in the crop images area $image->resize( $img_width, $img_height, true );

This code is just to specify if the path is exist you will not need to re-crop that images so it will not cause any duplication.

This code where the images crop involve as you can use this wp_get_image_editor as build in wordpress functionality to crop any images that you want then save it to a specific path. Now it was save then do a passing variable condition and output it into the $final_image. This $final_image variable contain the new url link image that you can pass to the img tags.

You can use this code when generating some images you want to crop specially in masonry grid or blog post that you want to be a same size product thumbnails and etc. Hope will help you overcome some  good way and controllable wordpress images crop. Consider this a part of Techniques for Web development.