Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restrict the upload image ratio to 16:9 in codeigniter?

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?

like image 232
user782104 Avatar asked Jul 12 '15 16:07

user782104


2 Answers

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
like image 55
Ijaz Ahmed Bhatti Avatar answered Nov 12 '22 16:11

Ijaz Ahmed Bhatti


$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:

  1. index function: load the view upload_example to display the upload form.
  2. 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

  1. Codeigniter Preferences
  2. 16:9 ratio width and height
  3. refer this article
like image 1
Abdulla Nilam Avatar answered Nov 12 '22 16:11

Abdulla Nilam