如何在codeigniter中将上传图片比率限制为16:9?

时间:2015-07-12 16:23:59

标签: php jquery html codeigniter

以下是我用于上传图片的代码。

$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));
}

前端只是一个

<input type = 'file' >

问题是,由于图片上传应该是视频缩略图,我该怎么做才能实现上传?例如在前端使用插件限制/裁剪(任何推荐)

另外,在服务器端如何查看?

3 个答案:

答案 0 :(得分:5)

maintain_ratio

首选项维持比例

Specifies whether to maintain the original aspect ratio when resizing or use hard values.

了解更多详情

答案 1 :(得分: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;

备注

  1. 索引功能:加载视图upload_example以显示上传表单。
  2. do_upload功能:将上传的文件保存到Web服务器,调整文件大小并显示结果。

    • userfile :是我们在上传表单中创建的文件输入名称。
    • user_upload_path :是Web服务器上保存上传文件的位置。它必须是可写的。
    • maintain_ratio = TRUE :保持宽高比
    • master_dim = height :高度用作硬值
    • 高度和宽度调整图片的高度和维护比例。
  3. CodeIgniter用于显示已调整大小的图像的视图

    <!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>
    

    链接

    1. Codeigniter Preferences
    2. 16:9 ratio width and height
    3. refer this article

答案 2 :(得分:0)

<form action="up.php" method="post" enctype="multipart/form-data">
<input type="file" name="files" multiple id="files"/>
</form>

这是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 ;
}