2015-07-12 99 views
13

这是我用来上传图片的代码。如何限制codeigniter中的上传图像比例为16:9?

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

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

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

+1

尝试此[链接](http://stackoverflow.com/questions/3255773/php-crop-image-to-fix-width-and -height-without-loss-dimension-ratio) – Bugfixer

+1

and this [link](http://stackoverflow.com/questions/7319304/resizing-and-cropping-image-with-gd-while-retaining-aspect-ratio) – Bugfixer

+0

谢谢,链接似乎保持图像比例和调整大小,任何情况下特定的16:9? – user782104

回答

5
maintain_ratio 

它的首选项,你保持比

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

详情 https://ellislab.com/codeigniter/user-guide/libraries/image_lib.html Resize image according to width only & crop Extra Height , Codeigniter Resizing and cropping in Codeigniter

+0

你会介意简单地解释如何裁剪16:9图像吗?例如。计算相应的宽度/高度 – user782104

+1

但是如何指定比例如16:9,它将根据图像大小调整图像的大小,而不是固定比例 – Bugfixer

+0

是的。应该裁剪而不是调整大小 – user782104

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 =高度:高度被用作硬值
    • 高度和宽度尺寸调整的图像的高度和维持率。

笨视图中显示调整后的图像

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

调整大小会导致**拉伸**,因为它不是原来的16:9 –

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