How to restrict the upload image ratio to 16:9 in codeigniter?
maintain_ratio
it the Preference you to maintain ratio
Specifies whether to maintain the original aspect ratio when resizing or use hard values.
For more details
$data_upload = $this->upload->data();$file_name = $data_upload["file_name"];$file_name_thumb = $data_upload['raw_name'].'_thumb' . $data_upload['file_ext'];$this->load->library('image_lib');$config_resize['image_library'] = 'gd2'; $config_resize['create_thumb'] = TRUE;$config_resize['maintain_ratio'] = TRUE;$config_resize['master_dim'] = 'height';//Check link 1 below$config_resize['quality'] = "100%";$config_resize['source_image'] = './' . $user_upload_path . $file_name;//for 16:9 width is 640 and height is 360(Check link 2 below)$config_resize['height'] = 360;$config_resize['width'] = 640;$this->image_lib->initialize($config_resize);$this->image_lib->resize();$data["file_name_url"] = base_url() . $user_upload_path . $file_name;$data["file_name_thumb_url"] = base_url() . $user_upload_path . $file_name_thumb;
Notes:
- index function: load the view upload_example to display the upload form.
do_upload function: save uploaded file to web server, resize the file and display result.
- userfile: is the file input name we created in the upload form.
- user_upload_path: is the location on web server to save uploaded files. It must be writable.
- maintain_ratio = TRUE: to maintain aspect ratio
- master_dim = height: the height is used as the hard value
- height and width resized image’s height and maintain ratio.
CodeIgniter View to display resized image
<!DOCTYPE html><html lang="en"><head> <meta charset="utf-8"> <title>CodeIgniter Upload And ReSize Image Maintain Ratio</title></head><body><?php if(isset($upload_error)) { echo $upload_error; } else { ?> <strong>Thumbnail:</strong> <p><img src="<?php echo $file_name_thumb_url;?>" /></p> <strong>Original:</strong> <p><img src="<?php echo $file_name_url;?>" /></p> <?php }?></body></html>
Links
<form action="up.php" method="post" enctype="multipart/form-data"><input type="file" name="files" multiple id="files"/></form>
This is up.php
list($width, $height) = getimagesize($_FILES['files']['tmp_name']);//i gave sample ratio 2.5 and 0.4 you can adjust yourselfif(abs($width / $height) >= 2.5 || abs($width / $height) <= 0.4) {echo 'Image ratio is invalid';exit ;}