{"id":84,"date":"2014-11-18T05:00:19","date_gmt":"2014-11-18T05:00:19","guid":{"rendered":"https:\/\/www.kolkataonweb.com\/code-bank\/?p=84"},"modified":"2014-11-23T14:59:40","modified_gmt":"2014-11-23T14:59:40","slug":"image-cropping-in-php-part-1","status":"publish","type":"post","link":"https:\/\/www.kolkataonweb.com\/code-bank\/php\/image-cropping-in-php-part-1\/","title":{"rendered":"Image Cropping in PHP &#8211; Part 1"},"content":{"rendered":"<p>With PHP 5.5.0 two functions, imagecropauto and imagecrop, have been added for advanced image resizing. This article describes the <em>imagecropauto<\/em> function.<\/p>\n<p>The <em>imagecropauto<\/em> function is useful to remove borders or background\/outer area of a photo. The mode parameter defines the border to be removed e.g. IMG_CROP_BLACK will remove black borders, IMG_CROP_WHITE will remove white borders etc.<br \/>\nMulticolored borders can be also be removed using the IMG_CROP_THRESHOLD mode, though it will be a bit tricky &mdash; couldn&#8217;t yet find a calculator to calculate the value of threshold. <\/p>\n<h5>Syntax:<\/h5>\n<pre class='wp-code-highlight prettyprint'>resource imagecropauto ( resource $image [, int $mode = -1 [, float $threshold = .5 [, int $color = -1 ]]] )<\/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>$modes<\/strong><br \/>\nIMG_CROP_TRANSPARENT \/ IMG_CROP_BLACK \/ IMG_CROP_WHITE \/ IMG_CROP_SIDES \/ IMG_CROP_THRESHOLD \/ IMG_CROP_DEFAULT<\/p>\n<p><strong>$threshold<\/strong><br \/>\nApplicable if IMG_CROP_THRESHOLD is used in mode. The value is based on the color distance in the RGB cube model.<\/p>\n<p><strong>$color<\/strong><br \/>\nThis code can be derived by converting the HEX value (of a color) to decimal value.<\/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 = imagecropauto($image_src,IMG_CROP_THRESHOLD,27.8,10746029);\r\n \r\nheader( 'Content-Type: image\/png');\r\n\r\nimagepng($croppedImage);\r\n<\/pre>\n<p>In the above code 10746029 decimal is equivalent to A3F8AD Hex &#8211; the outermost light green color in the below test image.<br \/>\nBelow 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\/Untitled.png\"><img loading=\"lazy\" decoding=\"async\" style=\"float:right\" src=\"https:\/\/www.kolkataonweb.com\/code-bank\/wp-content\/uploads\/2014\/11\/Untitled.png\" alt=\"Untitled\" width=\"86\" height=\"57\" class=\"alignnone size-full wp-image-87\" \/><\/a><\/p>\n<div style=\"clear:both\">&nbsp;<\/div>\n<div>&nbsp;<\/div>\n<p><a href=\"https:\/\/www.kolkataonweb.com\/code-bank\/miscellaneous\/image-cropping-in-php-part-2\/\" title=\"Image Cropping in PHP \u2013 Part 2\">Here<\/a> is the second part of this article.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>With PHP 5.5.0 two functions, imagecropauto and imagecrop, have been added for advanced image resizing. This article describes the imagecropauto function. The imagecropauto function is useful to remove borders or background\/outer area of a photo. The mode parameter defines the border to be removed e.g. IMG_CROP_BLACK will remove black borders, IMG_CROP_WHITE will remove white borders&hellip; <a class=\"more-link\" href=\"https:\/\/www.kolkataonweb.com\/code-bank\/php\/image-cropping-in-php-part-1\/\">Continue reading <span class=\"screen-reader-text\">Image Cropping in PHP &#8211; Part 1<\/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":[4],"class_list":["post-84","post","type-post","status-publish","format-standard","hentry","category-php","tag-imagecropauto-php","entry"],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts\/84","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=84"}],"version-history":[{"count":16,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts\/84\/revisions"}],"predecessor-version":[{"id":115,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/posts\/84\/revisions\/115"}],"wp:attachment":[{"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/media?parent=84"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/categories?post=84"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.kolkataonweb.com\/code-bank\/wp-json\/wp\/v2\/tags?post=84"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}