{"id":1443,"date":"2019-04-04T12:28:58","date_gmt":"2019-04-04T06:58:58","guid":{"rendered":"http:\/\/blog.openwebsolutions.in\/?p=1443"},"modified":"2019-04-04T12:31:46","modified_gmt":"2019-04-04T07:01:46","slug":"best-way-of-cropping-image","status":"publish","type":"post","link":"https:\/\/openwebsolutions.in\/blog\/best-way-of-cropping-image\/","title":{"rendered":"Learn The Best Way of cropping image by using Javascript Library"},"content":{"rendered":"<p>If you ever get stuck in a situation where you have to crop an area of an\u00a0image\u00a0like a circle or like a square or\u00a0maybe\u00a0a rectangle area in a\u00a0web\u00a0application then\u00a0there\u2019s a\u00a0way of achieving that by using a\u00a0Javascript\u00a0library\u00a0or plugin\u00a0called<b>\u00a0<\/b><b>Croppie<\/b>. It is fast,\u00a0and you will get a lot of configuration options to customize it as\u00a0you\u00a0need. If I can quote from the\u00a0website\u00a0then it is \u201cCroppie\u00a0is fast, easy to use image cropping plugin with tons of configurations options\u201d.<\/p>\n<p>So if you want to use it in your project\u00a0first\u00a0you have to install it in\u00a0your\u00a0project, and you can do that with\u00a0<b>NPM<\/b>\u00a0or\u00a0<b>BOWER<\/b><b>.\u00a0<\/b>Just run this following command as you see fit.<\/p>\n<p>NPM:\u00a0\u00a0npm\u00a0install\u00a0croppie<\/p>\n<p>Bower: bower install\u00a0croppie<\/p>\n<p>After\u00a0that,\u00a0you can require it or import it in your project<b>.<\/b>\u00a0Now to use this\u00a0Croppie\u00a0first you have to create a div tag in your\u00a0HTML\u00a0file and give\u00a0an id\u00a0to the div tag, now this div tag will be responsible for showing the image<b>.\u00a0<\/b>Like this one.<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;div\u00a0class=&#8221;demo&#8221;&gt;&lt;\/div&gt;<\/p>\n<p>&lt;script&gt;<\/p>\n<p>$(&#8216;.demo&#8217;).croppie({<\/p>\n<p>url:\u00a0&#8216;demo\/demo-1.jpg&#8217;,\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\/\/ although you can use the base64 format of an\u00a0image in the place of the\u00a0URL<\/p>\n<p>});<\/p>\n<p><b>&lt;\/<\/b><b>script<\/b><b>&gt;<\/b><\/p>\n<p>Now this will produce something like this. You can grab and select a<\/p>\n<p><img loading=\"lazy\" class=\"size-medium wp-image-1452 alignright\" src=\"http:\/\/blog.openwebsolutions.in\/wp-content\/uploads\/2019\/03\/cropp1-275x300.jpg\" alt=\"\" width=\"275\" height=\"300\" srcset=\"https:\/\/openwebsolutions.in\/blog\/wp-content\/uploads\/2019\/03\/cropp1-275x300.jpg 275w, https:\/\/openwebsolutions.in\/blog\/wp-content\/uploads\/2019\/03\/cropp1.jpg 329w\" sizes=\"(max-width: 275px) 100vw, 275px\" \/>portion of the selected image to crop. Then you can zoom in or out using the bar under. By\u00a0default,\u00a0it is enabled and you can also disable it, we will discuss that later.\u00a0Now,\u00a0remember the output of the image will always be the area that you have selected and\u00a0it will be encoded in base64.<\/p>\n<p>Now here\u2019s an example with some more functionality.<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;script&gt;<\/p>\n<p>$uploadCrop\u00a0=\u00a0$(&#8216;#upload-demo&#8217;).croppie({<\/p>\n<p>enableExif:\u00a0true,<\/p>\n<p>viewport:\u00a0{<\/p>\n<p>width:\u00a0200,<\/p>\n<p>height:\u00a0200,<\/p>\n<p>type:\u00a0&#8216;circle&#8217;<\/p>\n<p>},<\/p>\n<p>boundary:\u00a0{<\/p>\n<p>width:\u00a0300,<\/p>\n<p>height:\u00a0300<\/p>\n<p>}<\/p>\n<p>});<\/p>\n<p><b>&lt;\/<\/b><b>script<\/b><b>&gt;<\/b><\/p>\n<p>&nbsp;<\/p>\n<p>Here we can see some options like\u00a0enableExif\u00a0or viewport, they are used to customize the visual. Here\u00a0<b>enableExif<\/b><b>\u00a0<\/b>tells\u00a0Croppie\u00a0to read\u00a0Exif\u00a0orientation from the image data and fixes the image before rendering it to the page.<\/p>\n<p><b>Viewport<\/b><b>\u00a0<\/b>is an object, it is responsible for the inner part of the image which is visible to the user. You can set height, width, and\u00a0type of the viewport, which is circle by default, but it can be changed into square or rectangle.<\/p>\n<p><b>Boundary<\/b>\u00a0is also an object which is the outer container of the cropped image.<\/p>\n<p>Now to get the cropped area of the image as a result you have to use\u00a0the\u00a0<b>result<\/b><b>\u00a0<\/b>method.<\/p>\n<p>&nbsp;<\/p>\n<p>&lt;script&gt;<\/p>\n<p>$uploadCrop.croppie(&#8216;result&#8217;, {<\/p>\n<p>type:\u00a0&#8216;canvas&#8217;,<\/p>\n<p>size:\u00a0&#8216;original&#8217;,<\/p>\n<p>}).then(response\u00a0=&gt;{<\/p>\n<p>});<\/p>\n<p><b>&lt;\/<\/b><b>script<\/b><b>&gt;<\/b><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" class=\"size-full wp-image-1453 alignright\" src=\"http:\/\/blog.openwebsolutions.in\/wp-content\/uploads\/2019\/03\/cropp2.jpg\" alt=\"\" width=\"101\" height=\"94\" \/><\/p>\n<p>Here\u00a0<b>response\u00a0<\/b>is the image what you\u2019d desired. You\u2019ll get something like this.<\/p>\n<p>Although this\u00a0<b>response\u00a0<\/b>will be in base64 encoded, so you have to convert it into the<\/p>\n<p>file\u00a0format, how\u00a0are you going to do\u00a0that?\u00a0That I\u2019m not going to tell.\u00a0 \ud83d\ude42<\/p>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>If you ever get stuck in a situation where you have to crop an area of an\u00a0image\u00a0like a circle or like a square or\u00a0maybe\u00a0a rectangle area in a\u00a0web\u00a0application then\u00a0there\u2019s a\u00a0way of achieving that by using a\u00a0Javascript\u00a0library\u00a0or plugin\u00a0called\u00a0Croppie. It is fast,\u00a0and you will get a lot of configuration options to customize it as\u00a0you\u00a0need. If I can [&hellip;]<\/p>\n","protected":false},"author":24,"featured_media":1456,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[72,5],"tags":[73,107],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v14.8.1 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Learn The Best Way of cropping image by using Javascript Library<\/title>\n<meta name=\"robots\" content=\"index, follow\" \/>\n<meta name=\"googlebot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<meta name=\"bingbot\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/openwebsolutions.in\/blog\/best-way-of-cropping-image\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Learn The Best Way of cropping image by using Javascript Library\" \/>\n<meta property=\"og:description\" content=\"If you ever get stuck in a situation where you have to crop an area of an\u00a0image\u00a0like a circle or like a square or\u00a0maybe\u00a0a rectangle area in a\u00a0web\u00a0application then\u00a0there\u2019s a\u00a0way of achieving that by using a\u00a0Javascript\u00a0library\u00a0or plugin\u00a0called\u00a0Croppie. It is fast,\u00a0and you will get a lot of configuration options to customize it as\u00a0you\u00a0need. If I can [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/openwebsolutions.in\/blog\/best-way-of-cropping-image\/\" \/>\n<meta property=\"og:site_name\" content=\"Openweb Solutions Blog\" \/>\n<meta property=\"article:published_time\" content=\"2019-04-04T06:58:58+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-04-04T07:01:46+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/openwebsolutions.in\/blog\/wp-content\/uploads\/2019\/03\/Manual-Crop.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"985\" \/>\n\t<meta property=\"og:image:height\" content=\"618\" \/>\n<meta name=\"twitter:card\" content=\"summary\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#website\",\"url\":\"https:\/\/openwebsolutions.in\/blog\/\",\"name\":\"Openweb Solutions Blog\",\"description\":\"Transforming ideas into reality\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/openwebsolutions.in\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/best-way-of-cropping-image\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/openwebsolutions.in\/blog\/wp-content\/uploads\/2019\/03\/Manual-Crop.jpg\",\"width\":985,\"height\":618},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/best-way-of-cropping-image\/#webpage\",\"url\":\"https:\/\/openwebsolutions.in\/blog\/best-way-of-cropping-image\/\",\"name\":\"Learn The Best Way of cropping image by using Javascript Library\",\"isPartOf\":{\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/openwebsolutions.in\/blog\/best-way-of-cropping-image\/#primaryimage\"},\"datePublished\":\"2019-04-04T06:58:58+00:00\",\"dateModified\":\"2019-04-04T07:01:46+00:00\",\"author\":{\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#\/schema\/person\/cff51d2d1d78c9bc212b51a03c18a1b7\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/openwebsolutions.in\/blog\/best-way-of-cropping-image\/\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#\/schema\/person\/cff51d2d1d78c9bc212b51a03c18a1b7\",\"name\":\"Meghnath Kar\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/openwebsolutions.in\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/9b167b3cce9828ae8ffc02e4a58f4ab8?s=96&r=g\",\"caption\":\"Meghnath Kar\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts\/1443"}],"collection":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/users\/24"}],"replies":[{"embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/comments?post=1443"}],"version-history":[{"count":7,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts\/1443\/revisions"}],"predecessor-version":[{"id":1512,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/posts\/1443\/revisions\/1512"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/media\/1456"}],"wp:attachment":[{"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/media?parent=1443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/categories?post=1443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/openwebsolutions.in\/blog\/wp-json\/wp\/v2\/tags?post=1443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}