{"id":210,"date":"2020-06-29T12:25:32","date_gmt":"2020-06-29T12:25:32","guid":{"rendered":"https:\/\/codexjunction.com\/?p=210"},"modified":"2023-10-29T16:30:49","modified_gmt":"2023-10-29T16:30:49","slug":"crop-thumbnail-images-custom-code","status":"publish","type":"post","link":"https:\/\/codexjunction.com\/crop-thumbnail-images-custom-code\/","title":{"rendered":"Crop Thumbnail Images Custom Code"},"content":{"rendered":"<div class=\"logo-gutter ng-tns-c628290711-170 ng-star-inserted\">\n<div class=\"resize-observable\"><\/div>\n<\/div>\n<div class=\"response-container-content ng-tns-c628290711-170\">\n<div class=\"response-content ng-trigger ng-trigger-responsePopulation ng-tns-c628290711-170\">\n<div class=\"markdown markdown-main-panel\" dir=\"ltr\">\n<p data-sourcepos=\"1:1-1:93\">To crop thumbnail images in WordPress\/PHP using custom code, you can use the following steps:<\/p>\n<ol data-sourcepos=\"3:1-5:0\">\n<li data-sourcepos=\"3:1-3:72\">Create a new <a href=\"https:\/\/www.php.net\/\" rel=\"nofollow\">PHP<\/a> file in your WordPress theme&#8217;s\u00a0<code>functions.php<\/code>\u00a0file.<\/li>\n<li data-sourcepos=\"4:1-5:0\">Add the following code to your PHP file:<\/li>\n<\/ol>\n<div class=\"code-block ng-star-inserted\">\n<div class=\"code-block-decoration header gmat-subhead-2 ng-star-inserted\">PHP<\/div>\n<pre><code class=\"code-container\" role=\"text\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">my_custom_crop_thumbnail<\/span>(<span class=\"hljs-params\"><span class=\"hljs-variable\">$image_id<\/span>, <span class=\"hljs-variable\">$width<\/span>, <span class=\"hljs-variable\">$height<\/span>, <span class=\"hljs-variable\">$crop<\/span><\/span>) <\/span>{\r\n  <span class=\"hljs-comment\">\/\/ Get the original image.<\/span>\r\n  <span class=\"hljs-variable\">$original_image<\/span> = wp_get_attachment_image_src(<span class=\"hljs-variable\">$image_id<\/span>, <span class=\"hljs-string\">'full'<\/span>);\r\n\r\n  <span class=\"hljs-comment\">\/\/ Create a new image and crop it to the desired dimensions.<\/span>\r\n  <span class=\"hljs-variable\">$cropped_image<\/span> = wp_get_image_editor(<span class=\"hljs-variable\">$original_image<\/span>[<span class=\"hljs-number\">0<\/span>]);\r\n  <span class=\"hljs-variable\">$cropped_image<\/span>-&gt;crop(<span class=\"hljs-variable\">$original_image<\/span>[<span class=\"hljs-number\">0<\/span>], <span class=\"hljs-variable\">$width<\/span>, <span class=\"hljs-variable\">$height<\/span>, <span class=\"hljs-variable\">$crop<\/span>);\r\n\r\n  <span class=\"hljs-comment\">\/\/ Save the cropped image.<\/span>\r\n  <span class=\"hljs-variable\">$cropped_image<\/span>-&gt;save(<span class=\"hljs-variable\">$original_image<\/span>[<span class=\"hljs-number\">0<\/span>]);\r\n\r\n  <span class=\"hljs-comment\">\/\/ Return the URL of the cropped image.<\/span>\r\n  <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-variable\">$original_image<\/span>[<span class=\"hljs-number\">0<\/span>];\r\n}\r\n<\/code><\/pre>\n<\/div>\n<p data-sourcepos=\"23:1-23:35\">This function takes four arguments:<\/p>\n<ul data-sourcepos=\"25:1-29:0\">\n<li data-sourcepos=\"25:1-25:43\"><code>$image_id<\/code>: The ID of the image to crop.<\/li>\n<li data-sourcepos=\"26:1-26:43\"><code>$width<\/code>: The width of the cropped image.<\/li>\n<li data-sourcepos=\"27:1-27:45\"><code>$height<\/code>: The height of the cropped image.<\/li>\n<li data-sourcepos=\"28:1-29:0\"><code>$crop<\/code>: The crop position. This can be one of the following values:\u00a0<code>top<\/code>,\u00a0<code>bottom<\/code>,\u00a0<code>left<\/code>,\u00a0<code>right<\/code>, or\u00a0<code>center<\/code>.<\/li>\n<\/ul>\n<ol start=\"3\" data-sourcepos=\"30:1-32:0\">\n<li data-sourcepos=\"30:1-30:96\">Once you have added the code to your PHP file, save it and upload it to your <a href=\"https:\/\/codexjunction.com\/how-to-create-a-custom-wordpress-theme\/\">WordPress theme<\/a>.<\/li>\n<li data-sourcepos=\"31:1-32:0\">To crop a thumbnail image, you can use the following code:<\/li>\n<\/ol>\n<div class=\"code-block ng-star-inserted\">\n<div class=\"code-block-decoration header gmat-subhead-2 ng-star-inserted\">PHP<\/div>\n<pre><code class=\"code-container\" role=\"text\"><span class=\"hljs-variable\">$image_id<\/span> = get_post_thumbnail_id(<span class=\"hljs-variable\">$post_id<\/span>);\r\n<span class=\"hljs-variable\">$cropped_image_url<\/span> = my_custom_crop_thumbnail(<span class=\"hljs-variable\">$image_id<\/span>, <span class=\"hljs-number\">150<\/span>, <span class=\"hljs-number\">150<\/span>, <span class=\"hljs-string\">'center'<\/span>);\r\n\r\n<span class=\"hljs-comment\">\/\/ Echo the URL of the cropped image.<\/span>\r\n<span class=\"hljs-keyword\">echo<\/span> <span class=\"hljs-variable\">$cropped_image_url<\/span>;\r\n<\/code><\/pre>\n<\/div>\n<p data-sourcepos=\"41:1-41:125\">This code will crop the thumbnail image to a width of 150 pixels and a height of 150 pixels, with the crop position centered.<\/p>\n<p data-sourcepos=\"43:1-43:231\">You can use the <code>my_custom_crop_thumbnail()<\/code> function to crop thumbnail images anywhere in your WordPress theme. For example, you could use it to crop the thumbnail images in your blog posts, archive pages, or search results pages.<\/p>\n<p data-sourcepos=\"45:1-45:20\"><strong>Additional tips:<\/strong><\/p>\n<ul data-sourcepos=\"47:1-50:0\">\n<li data-sourcepos=\"47:1-47:93\">You can use the\u00a0<code>my_custom_crop_thumbnail()<\/code>\u00a0function to crop thumbnail images of any size.<\/li>\n<li data-sourcepos=\"48:1-48:107\">You can use the\u00a0<code>my_custom_crop_thumbnail()<\/code>\u00a0function to crop thumbnail images from any custom post type.<\/li>\n<li data-sourcepos=\"49:1-50:0\">You can use the\u00a0<code>my_custom_crop_thumbnail()<\/code>\u00a0function to crop thumbnail images from any taxonomy.<\/li>\n<\/ul>\n<p data-sourcepos=\"51:1-51:163\">If you are not sure how to use the <code>my_custom_crop_thumbnail()<\/code> function, you can consult the WordPress documentation or contact a WordPress expert for assistance.<\/p>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>To crop thumbnail images in WordPress\/PHP using custom code, you can use the following steps: Create a new PHP file in your WordPress theme&#8217;s\u00a0functions.php\u00a0file. Add the following code to your PHP file: PHP function my_custom_crop_thumbnail($image_id, $width, $height, $crop) { \/\/ Get the original image. $original_image = wp_get_attachment_image_src($image_id, &#8216;full&#8217;); \/\/ Create a new image and crop it to the desired dimensions. $cropped_image = wp_get_image_editor($original_image[0]); $cropped_image-&gt;crop($original_image[0], $width, $height, $crop); \/\/ Save the cropped image. $cropped_image-&gt;save($original_image[0]); \/\/ Return the URL of the cropped image. return $original_image[0]; } This function takes four arguments: $image_id: The ID of the image to crop. $width: The width [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18,51],"tags":[238],"class_list":["post-210","post","type-post","status-publish","format-standard","hentry","category-php","category-wordpress","tag-crop-thumbnail-images"],"_links":{"self":[{"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/posts\/210","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/comments?post=210"}],"version-history":[{"count":4,"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/posts\/210\/revisions"}],"predecessor-version":[{"id":2649,"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/posts\/210\/revisions\/2649"}],"wp:attachment":[{"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/media?parent=210"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/categories?post=210"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codexjunction.com\/wp-json\/wp\/v2\/tags?post=210"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}