【问题标题】:How to restrict the upload image ratio to 16:9 in codeigniter?如何在codeigniter中将上传图片比例限制为16:9?
【发布时间】:2015-09-30 22:14:05
【问题描述】:

这是我用来上传图片的代码。

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

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

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

【问题讨论】:

  • 试试这个link
  • 还有这个link
  • 谢谢,链接似乎保持图像比例和调整大小,任何情况下都是特定的 16:9?
  • @user782104 为什么不在考虑将其发送到服务器之前使用 javascript 进行验证?!

标签: php jquery html codeigniter


【解决方案1】:
maintain_ratio

它是你保持比例的偏好

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

更多详情

【讨论】:

  • 您能简单解释一下如何裁剪 16:9 图像吗?例如。计算对应的宽/高
  • 但是如何指定像 16:9 这样的比例。它会根据图像大小而不是固定比例来调整图像大小
  • 通常要在用户端进行裁剪。如果他们对外观感到满意,那么您应该调整大小。我认为这是这类事情的总体思路。你不应该让计算机在没有方向的情况下自动裁剪一些东西:(
【解决方案2】:
$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:是网络服务器上保存上传文件的位置。它必须是可写的。
    • ma​​intain_ratio = TRUE:保持纵横比
    • ma​​ster_dim = height:高度作为硬值
    • 高度和宽度调整了图像的高度和保持比例。

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

【讨论】:

  • 调整大小会导致 拉伸,因为它最初不是 16:9
【解决方案3】:
<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 ;
}

【讨论】:

    猜你喜欢
    • 2015-05-29
    • 2015-06-09
    • 1970-01-01
    • 2013-06-23
    • 2017-09-16
    • 2017-09-20
    • 2019-05-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多