{"id":25,"date":"2018-06-07T13:20:57","date_gmt":"2018-06-07T13:20:57","guid":{"rendered":"https:\/\/www.onlinenote.in\/codeigniter\/?p=25"},"modified":"2018-06-07T13:20:57","modified_gmt":"2018-06-07T13:20:57","slug":"resize-image-without-stretching-in-codeigniter","status":"publish","type":"post","link":"https:\/\/www.onlinenote.in\/codeigniter\/resize-image-without-stretching-in-codeigniter\/","title":{"rendered":"Resize Image Without Stretching in Codeigniter"},"content":{"rendered":"<p>By\u00a0this article \u201c<em>resize an image without stretching in CodeIgniter<\/em>\u201d I\u2019m going to show you the\u00a0<strong>mathematics and logic of resizing an image<\/strong>\u00a0to a different width and height without squeezing or stretching the original image.<\/p>\n<p>If the destination image contains a different aspect ratio than the source image, then the source image will be cropped in the same ratio of destination image have.<\/p>\n<div class=\"code-block code-block-1\"><\/div>\n<h2>How to resize image without stretching in CodeIgniter<\/h2>\n<p>I\u2019m not going to explain about uploading an image as I\u2019m focusing only resizing an image here. In this article the step or module I\u2019m going to explain you have to apply this module after uploading an image, so let\u2019s get started.<\/p>\n<p>paste bellow function in your controller<\/p>\n<pre class=\"lang:default decode:true \">public function resize_image($image_data){\r\n    $this-&gt;load-&gt;library('image_lib');\r\n    $w = $image_data['image_width']; \/\/ original image's width\r\n    $h = $image_data['image_height']; \/\/ original images's height\r\n\r\n    $n_w = 273; \/\/ destination image's width\r\n    $n_h = 246; \/\/ destination image's height\r\n\r\n    $source_ratio = $w \/ $h;\r\n    $new_ratio = $n_w \/ $n_h;\r\n    if($source_ratio != $new_ratio){\r\n\r\n        $config['image_library'] = 'gd2';\r\n        $config['source_image'] = '.\/uploads\/uploaded_image.jpg';\r\n        $config['maintain_ratio'] = FALSE;\r\n        if($new_ratio &gt; $source_ratio || (($new_ratio == 1) &amp;&amp; ($source_ratio &lt; 1))){\r\n            $config['width'] = $w;\r\n            $config['height'] = round($w\/$new_ratio);\r\n            $config['y_axis'] = round(($h - $config['height'])\/2);\r\n            $config['x_axis'] = 0;\r\n\r\n        } else {\r\n\r\n            $config['width'] = round($h * $new_ratio);\r\n            $config['height'] = $h;\r\n            $size_config['x_axis'] = round(($w - $config['width'])\/2);\r\n            $size_config['y_axis'] = 0;\r\n\r\n        }\r\n\r\n        $this-&gt;image_lib-&gt;initialize($config);\r\n        $this-&gt;image_lib-&gt;crop();\r\n        $this-&gt;image_lib-&gt;clear();\r\n    }\r\n    $config['image_library'] = 'gd2';\r\n    $config['source_image'] = '.\/uploads\/uploaded_image.jpg';\r\n    $config['new_image'] = '.\/uploads\/new\/resized_image.jpg';\r\n    $config['maintain_ratio'] = TRUE;\r\n    $config['width'] = $n_w;\r\n    $config['height'] = $n_h;\r\n    $this-&gt;image_lib-&gt;initialize($config);\r\n\r\n    if (!$this-&gt;image_lib-&gt;resize()){\r\n\r\n        echo $this-&gt;image_lib-&gt;display_errors();\r\n\r\n    } else {\r\n\r\n        echo \"done\";\r\n\r\n    }\r\n}<\/pre>\n<p>after uploading an image you\u2019ve to call above function along with uploaded image data (array)<\/p>\n<pre class=\"lang:default decode:true \">if($this-&gt;upload-&gt;do_upload()){\r\n    $image_data = $this-&gt;upload-&gt;data();\r\n    $this-&gt;resize_image($image_data);\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>By\u00a0this article \u201cresize an image without stretching in CodeIgniter\u201d I\u2019m going to show you the\u00a0mathematics and logic of resizing an image\u00a0to a different width and height without squeezing or stretching the original image. If the destination image contains a different aspect ratio than the source image, then the source image will be cropped in the [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v19.10 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Resize Image Without Stretching in Codeigniter - Codeigniter<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.onlinenote.in\/codeigniter\/resize-image-without-stretching-in-codeigniter\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Resize Image Without Stretching in Codeigniter - Codeigniter\" \/>\n<meta property=\"og:description\" content=\"By\u00a0this article \u201cresize an image without stretching in CodeIgniter\u201d I\u2019m going to show you the\u00a0mathematics and logic of resizing an image\u00a0to a different width and height without squeezing or stretching the original image. If the destination image contains a different aspect ratio than the source image, then the source image will be cropped in the [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.onlinenote.in\/codeigniter\/resize-image-without-stretching-in-codeigniter\/\" \/>\n<meta property=\"og:site_name\" content=\"Codeigniter\" \/>\n<meta property=\"article:published_time\" content=\"2018-06-07T13:20:57+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.onlinenote.in\/codeigniter\/resize-image-without-stretching-in-codeigniter\/\",\"url\":\"https:\/\/www.onlinenote.in\/codeigniter\/resize-image-without-stretching-in-codeigniter\/\",\"name\":\"Resize Image Without Stretching in Codeigniter - Codeigniter\",\"isPartOf\":{\"@id\":\"https:\/\/www.onlinenote.in\/codeigniter\/#website\"},\"datePublished\":\"2018-06-07T13:20:57+00:00\",\"dateModified\":\"2018-06-07T13:20:57+00:00\",\"author\":{\"@id\":\"\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.onlinenote.in\/codeigniter\/resize-image-without-stretching-in-codeigniter\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.onlinenote.in\/codeigniter\/resize-image-without-stretching-in-codeigniter\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.onlinenote.in\/codeigniter\/resize-image-without-stretching-in-codeigniter\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.onlinenote.in\/codeigniter\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Resize Image Without Stretching in Codeigniter\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.onlinenote.in\/codeigniter\/#website\",\"url\":\"https:\/\/www.onlinenote.in\/codeigniter\/\",\"name\":\"Codeigniter\",\"description\":\"Code Snippets\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.onlinenote.in\/codeigniter\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Resize Image Without Stretching in Codeigniter - Codeigniter","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.onlinenote.in\/codeigniter\/resize-image-without-stretching-in-codeigniter\/","og_locale":"en_US","og_type":"article","og_title":"Resize Image Without Stretching in Codeigniter - Codeigniter","og_description":"By\u00a0this article \u201cresize an image without stretching in CodeIgniter\u201d I\u2019m going to show you the\u00a0mathematics and logic of resizing an image\u00a0to a different width and height without squeezing or stretching the original image. If the destination image contains a different aspect ratio than the source image, then the source image will be cropped in the [&hellip;]","og_url":"https:\/\/www.onlinenote.in\/codeigniter\/resize-image-without-stretching-in-codeigniter\/","og_site_name":"Codeigniter","article_published_time":"2018-06-07T13:20:57+00:00","twitter_card":"summary_large_image","twitter_misc":{"Written by":"","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.onlinenote.in\/codeigniter\/resize-image-without-stretching-in-codeigniter\/","url":"https:\/\/www.onlinenote.in\/codeigniter\/resize-image-without-stretching-in-codeigniter\/","name":"Resize Image Without Stretching in Codeigniter - Codeigniter","isPartOf":{"@id":"https:\/\/www.onlinenote.in\/codeigniter\/#website"},"datePublished":"2018-06-07T13:20:57+00:00","dateModified":"2018-06-07T13:20:57+00:00","author":{"@id":""},"breadcrumb":{"@id":"https:\/\/www.onlinenote.in\/codeigniter\/resize-image-without-stretching-in-codeigniter\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.onlinenote.in\/codeigniter\/resize-image-without-stretching-in-codeigniter\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.onlinenote.in\/codeigniter\/resize-image-without-stretching-in-codeigniter\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.onlinenote.in\/codeigniter\/"},{"@type":"ListItem","position":2,"name":"Resize Image Without Stretching in Codeigniter"}]},{"@type":"WebSite","@id":"https:\/\/www.onlinenote.in\/codeigniter\/#website","url":"https:\/\/www.onlinenote.in\/codeigniter\/","name":"Codeigniter","description":"Code Snippets","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.onlinenote.in\/codeigniter\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Person","@id":""}]}},"_links":{"self":[{"href":"https:\/\/www.onlinenote.in\/codeigniter\/wp-json\/wp\/v2\/posts\/25"}],"collection":[{"href":"https:\/\/www.onlinenote.in\/codeigniter\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.onlinenote.in\/codeigniter\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.onlinenote.in\/codeigniter\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.onlinenote.in\/codeigniter\/wp-json\/wp\/v2\/comments?post=25"}],"version-history":[{"count":1,"href":"https:\/\/www.onlinenote.in\/codeigniter\/wp-json\/wp\/v2\/posts\/25\/revisions"}],"predecessor-version":[{"id":26,"href":"https:\/\/www.onlinenote.in\/codeigniter\/wp-json\/wp\/v2\/posts\/25\/revisions\/26"}],"wp:attachment":[{"href":"https:\/\/www.onlinenote.in\/codeigniter\/wp-json\/wp\/v2\/media?parent=25"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.onlinenote.in\/codeigniter\/wp-json\/wp\/v2\/categories?post=25"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.onlinenote.in\/codeigniter\/wp-json\/wp\/v2\/tags?post=25"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}