Here is the code I used for uploading an image.
$this->load->library('upload');
$ext = pathinfo($file_name, PATHINFO_EXTENSION);
$img_name = now() . "." . $ext;
$imgConfig['upload_path'] = $this->image_path;
$imgConfig['file_name'] = $img_name;
$imgConfig['max_size'] = '2048';
$imgConfig['allowed_types'] = 'jpg|png|bmp';
$imgConfig['overwrite'] = FALSE;
$this->upload->initialize($imgConfig);
if ($this->upload->do_upload("image_url")) {
$this->Playlist_model->update_playlist($insert_id, array("image_url" => $img_name));
}
And the frontend is simply a
<input type = 'file' >
The problem is , as the image upload should be an video thumbnail, what should I do to implement the upload? e.g using plugin in frontend to restrict/ crop (any recommendation)
Also, in server side how can I check?
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
- https://www.codeigniter.com/user_guide/libraries/image_lib.html
- Resize image according to width only & crop Extra Height , Codeigniter
- Resizing and cropping in Codeigniter
$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
- Codeigniter Preferences
- 16:9 ratio width and height
- refer this article
<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 yourself
if(abs($width / $height) >= 2.5 || abs($width / $height) <= 0.4) {
echo 'Image ratio is invalid';
exit ;
}