By this article “resize an image without stretching in CodeIgniter” I’m going to show you the mathematics and logic of resizing an image to 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 same ratio of destination image have.
I’m not going to explain about uploading an image as I’m focusing only resizing an image here. In this article the step or module I’m going to explain you have to apply this module after uploading an image, so let’s get started.
paste bellow function in your controller
public function resize_image($image_data){ $this->load->library('image_lib'); $w = $image_data['image_width']; // original image's width $h = $image_data['image_height']; // original images's height $n_w = 273; // destination image's width $n_h = 246; // destination image's height $source_ratio = $w / $h; $new_ratio = $n_w / $n_h; if($source_ratio != $new_ratio){ $config['image_library'] = 'gd2'; $config['source_image'] = './uploads/uploaded_image.jpg'; $config['maintain_ratio'] = FALSE; if($new_ratio > $source_ratio || (($new_ratio == 1) && ($source_ratio < 1))){ $config['width'] = $w; $config['height'] = round($w/$new_ratio); $config['y_axis'] = round(($h - $config['height'])/2); $config['x_axis'] = 0; } else { $config['width'] = round($h * $new_ratio); $config['height'] = $h; $size_config['x_axis'] = round(($w - $config['width'])/2); $size_config['y_axis'] = 0; } $this->image_lib->initialize($config); $this->image_lib->crop(); $this->image_lib->clear(); } $config['image_library'] = 'gd2'; $config['source_image'] = './uploads/uploaded_image.jpg'; $config['new_image'] = './uploads/new/resized_image.jpg'; $config['maintain_ratio'] = TRUE; $config['width'] = $n_w; $config['height'] = $n_h; $this->image_lib->initialize($config); if (!$this->image_lib->resize()){ echo $this->image_lib->display_errors(); } else { echo "done"; } }
after uploading an image you’ve to call above function along with uploaded image data (array)
if($this->upload->do_upload()){ $image_data = $this->upload->data(); $this->resize_image($image_data); }
Contact Controller [crayon-67a5ae8d51b08717889526/] Contact_form.php - view [crayon-67a5ae8d51b13640792293/] Contact_model [crayon-67a5ae8d51b18579207827/] Captcha Helper [crayon-67a5ae8d51b20282424259/] Notifications_model [crayon-67a5ae8d51b28400910785/] Database…
[crayon-67a5ae8d51e23508196092/] [crayon-67a5ae8d51e26155933287/]
[crayon-67a5ae8d51ec3375163722/] The first parameter specifies the type of string, the second parameter specifies the length.…
Create Controller [crayon-67a5ae8d51f4a005213161/] 2. Create View File [crayon-67a5ae8d51f4d978751007/]
[crayon-67a5ae8d5201a643478529/] [crayon-67a5ae8d5201d988762604/]
All of the native error messages are located in the following language file: system/language/english/form_validation_lang.php To set…