{"id":102,"date":"2014-11-23T14:55:04","date_gmt":"2014-11-23T14:55:04","guid":{"rendered":"https:\/\/www.kolkataonweb.com\/code-bank\/?p=102"},"modified":"2014-11-25T21:00:32","modified_gmt":"2014-11-25T21:00:32","slug":"image-cropping-in-php-part-2","status":"publish","type":"post","link":"https:\/\/www.kolkataonweb.com\/code-bank\/php\/image-cropping-in-php-part-2\/","title":{"rendered":"Image Cropping in PHP &#8211; Part 2"},"content":{"rendered":"<p>Here is the second part of the article &#8220;Image Cropping in PHP&#8221;. The first part is <a href=\"https:\/\/www.kolkataonweb.com\/code-bank\/php\/image-cropping-in-php-part-1\/\" title=\"Image Cropping in PHP \u2013 Part 1\">here<\/a><\/p>\n<p>This part describes the function imagecrop introduced in PHP 5.5.0 for cropping images. <em>imagecrop<\/em> can be used to crop an image based on a start and end point\/co-ordinate and dimensions. <em>(It doesn&#8217;t take into account any color like imagecropauto )<\/em><br \/>\nIt is a relatively simpler and easier to use. <\/p>\n<h5>Syntax:<\/h5>\n<pre class='wp-code-highlight prettyprint'>$resource imagecrop ( resource $image , array $rect )<\/pre>\n<h5>Parameters:<\/h5>\n<p><strong>$image<\/strong><br \/>\nresource returned by any create image function like imagecreatefromjpeg or imagecreatetruecolor etc.<\/p>\n<p><strong>$rect<\/strong><br \/>\nThis will be an array that will hold the x, y co-ordinate and the dimensions. This will define the <em>rect<\/em>angular area of the image that will be kept.<br \/>\nThe array keys must be &#8220;x&#8221;, &#8220;y&#8221;, &#8220;width&#8221; and &#8220;height&#8221;.<\/p>\n<p>For example in the below image the light green border needs to be removed, so the rect will be <em>array(&#8220;x&#8221;=>27, &#8220;y&#8221;=>26, &#8220;width&#8221;=>163, &#8220;height&#8221;=>142)<\/em> <em style=\"font-size:14px\">(the values might not be pixel perfect &ndash; it is just for demonstration)<\/em><br \/>\n<a href=\"https:\/\/www.kolkataonweb.com\/code-bank\/wp-content\/uploads\/2014\/11\/imagecrop1.png\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.kolkataonweb.com\/code-bank\/wp-content\/uploads\/2014\/11\/imagecrop1-300x253.png\" alt=\"imagecrop1\" width=\"300\" height=\"253\" class=\"alignnone size-medium wp-image-103\" srcset=\"https:\/\/www.kolkataonweb.com\/code-bank\/wp-content\/uploads\/2014\/11\/imagecrop1-300x253.png 300w, https:\/\/www.kolkataonweb.com\/code-bank\/wp-content\/uploads\/2014\/11\/imagecrop1.png 386w\" sizes=\"auto, (max-width: 300px) 100vw, 300px\" \/><\/a><\/p>\n<h5>Example Code:<\/h5>\n<pre class='wp-code-highlight prettyprint'>\r\n....\r\n$image_src = imagecreatefrompng($_FILES['image']['tmp_name']);\r\n\r\n$croppedImage = imagecrop($image_src,array(\"x\"=>27,\"y\"=>26,\"width\"=>163,\"height\"=>142));\r\n \r\nheader( 'Content-Type: image\/png');\r\n\r\nimagepng($croppedImage);\r\n<\/pre>\n<p>Below left is the original image and right is the cropped image (using the above example).<br \/>\n<br \/>\n<a href=\"https:\/\/www.kolkataonweb.com\/code-bank\/wp-content\/uploads\/2014\/11\/Untitled2.png\"><img loading=\"lazy\" decoding=\"async\" style=\"float:left\" src=\"https:\/\/www.kolkataonweb.com\/code-bank\/wp-content\/uploads\/2014\/11\/Untitled2.png\" alt=\"Untitled2\" width=\"212\" height=\"200\" class=\"alignnone size-full wp-image-86\" \/><\/a><\/p>\n<p><a href=\"https:\/\/www.kolkataonweb.com\/code-bank\/wp-content\/uploads\/2014\/11\/final2.png\"><img loading=\"lazy\" decoding=\"async\" style=\"float:right\"  src=\"https:\/\/www.kolkataonweb.com\/code-bank\/wp-content\/uploads\/2014\/11\/final2.png\" alt=\"final2\" width=\"163\" height=\"142\" class=\"alignnone size-full wp-image-104\" \/><\/a><\/p>\n<p style=\"clear:both;\">&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Here is the second part of the article &#8220;Image Cropping in PHP&#8221;. The first part is here This part describes the function imagecrop introduced in PHP 5.5.0 for cropping images. imagecrop can be used to crop an image based on a start and end point\/co-ordinate and dimensions. (It doesn&#8217;t take into account any color like&hellip; <a class=\"more-link\" href=\"https:\/\/www.kolkataonweb.com\/code-bank\/php\/image-cropping-in-php-part-2\/\">Continue reading <span class=\"screen-reader-text\">Image Cropping in PHP &#8211; Part 2<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[5],"class_list":["post-102","post","type-post","status-publish","format-standard","hentry","category-php","tag-imagecrop-php","entry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts\/102","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/comments?post=102"}],"version-history":[{"count":8,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts\/102\/revisions"}],"predecessor-version":[{"id":156,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts\/102\/revisions\/156"}],"wp:attachment":[{"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/media?parent=102"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/categories?post=102"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/tags?post=102"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}