12

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?

Batman
  • 541
  • 4
  • 25
user782104
  • 13,233
  • 55
  • 172
  • 312
  • 1
    Try this [link](http://stackoverflow.com/questions/3255773/php-crop-image-to-fix-width-and-height-without-losing-dimension-ratio) – Bugfixer Jul 15 '15 at 05:24
  • 1
    and this [link](http://stackoverflow.com/questions/7319304/resizing-and-cropping-image-with-gd-while-retaining-aspect-ratio) – Bugfixer Jul 15 '15 at 05:24
  • Thanks, the link seems to keep the image ratio and resize, any case for specific 16:9? – user782104 Jul 15 '15 at 07:47
  • @user782104 Why not just verifying that using javascript before even thinking to send it to the server ?! – akmozo Jul 18 '15 at 22:52

3 Answers3

6
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

Stack Programmer
  • 679
  • 6
  • 18
Ijaz Ahmed Bhatti
  • 736
  • 1
  • 7
  • 26
  • would you mind explain briefly about how to crop the 16:9 image? eg. calculate the corresponding width / height – user782104 Jul 15 '15 at 07:51
  • 1
    But how to specify ratio like 16:9.it will resize image according to image size not in ny fixed ratio – Bugfixer Jul 15 '15 at 07:52
  • 1
    to crop usually you want to do that on the users side. if they are satisfied with the look then you should resize.. I think that is the idea overall for this type of thing. You shouldn't allow the computer to crop something automatically without direction :( – Cayce K Jul 15 '15 at 12:10
1
$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
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
0
<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 ;
}