我的控制器功能是
public function add() {
$blog_title = $this->input->post("qes_blog_title");
$description = htmlentities($_POST['qes_blog_description']);
$edit_status = $this->input->post("edit");
$filename = $blog_title . date('ymdhis');
if ($this->session->userdata('user_type') == "SA") {
$dirname = (dirname(dirname(dirname(__FILE__)))) .
'/uploads/blog/admin';
$usertype = "1";
} else {
$dirname = (dirname(dirname(dirname(__FILE__)))) . '/uploads/blog/'
. $this->session->userdata('user_id');
$usertype = "0";
}
if (!file_exists($dirname)) {
mkdir($dirname, 0777, true);
}
$config['upload_path'] = $dirname;
$config['file_name'] = $filename;
$config['allowed_types'] = 'jpeg|jpg|png';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload("qes_blog_image")) {
//print_r($this->upload->display_errors());
$data = array(
'qes_blog_title' => $blog_title,
'qes_blog_description' => $description,
'qes_vendor_id' => $this->session->userdata('user_id'),
'qes_user_type' => $usertype,
'qes_created_at' => date('Y-m-d H:i:s')
);
} else {
if ($edit_status) {
$condition = array(
'qes_vendor_blog_id' => $edit_status
);
$categoryDetails = $this->common_model->get_row($condition, $this->tablename);
if ($categoryDetails->qes_category_image != NULL) {
if ($this->session->userdata('user_type') == "SA") {
unlink((dirname(dirname(dirname(__FILE__)))) . '/uploads/blog/admin/' . $categoryDetails->qes_blog_image);
} else {
unlink((dirname(dirname(dirname(__FILE__)))) . '/uploads/blog/' . $categoryDetails->qes_vendor_id . "/" . $categoryDetails->qes_blog_image);
}
}
}
$uploqes_data = $this->upload->data();
$uploqes_filename = $uploqes_data['file_name'];
$data = array(
'qes_blog_title' => $blog_title,
'qes_blog_description' => $description,
'qes_vendor_id' => $this->session->userdata('user_id'),
'qes_user_type' => $usertype,
'qes_blog_image' => $uploqes_filename,
'qes_created_at' => date('Y-m-d H:i:s')
);
}
$condition = array(
'qes_blog_title' => $blog_title
);
$blog_details = $this->common_model->get_row($condition, $this->tablename);
if ($edit_status) {
if (count($blog_details) > 0) {
if ($blog_details->qes_blog_title == $blog_title) {
$condition = array(
'qes_vendor_blog_id' => $edit_status
);
$result = $this->common_model->update_row($data, $condition, $this->tablename);
$status = 'Blog "<b>' . $blog_title . '</b>" Successfully Updated';
} else {
$htm = "0";
$result = "0";
$status = "0";
}
} else {
$condition = array(
'qes_vendor_blog_id' => $edit_status
);
$result = $this->common_model->update_row($data, $condition, $this->tablename);
$status = 'Blog "<b>' . $blog_title . '</b>" Successfully Updated';
}
} else {
if (count($blog_details) > 0) {
if ($blog_details->qes_blog_title == $blog_title) {
$htm = "0";
$result = "0";
$status = "0";
} else {
$result = $this->common_model->insert_data($this->tablename, $data);
$status = 'Blog "<b>' . $blog_title . '</b>" Successfully Created';
}
} else {
$result = $this->common_model->insert_data($this->tablename, $data);
$status = 'Blog "<b>' . $blog_title . '</b>" Successfully Created';
}
}
if ($result) {
$htm = $result;
$this->session->set_flashdata('success_msg', $status);
} else {
$htm = '0';
$this->session->set_flashdata('success_msg', 'Error While Creating Parent');
}
echo $htm;
}
我是Codeigniter的初学者。
如何在上传时限制图像的大小。只能上传特定尺寸的图像。以上是添加博客的控制器功能。如何在上面的代码中指定大小。如何保持像#34;只上传指定大小的图像&#34;在Codeigniter中上传图片时?
答案 0 :(得分:0)
请参考此代码,这可能对您有所帮助。谢谢!
$file_name = '';
$error = '';
if ($_FILES['image']['name'] != '') {
$config['upload_path'] = 'uploads/blog/main/';
$config['allowed_types'] = 'jpg|png|jpeg';
$this->load->library('upload', $config);
if (!$this->upload->do_upload('image')) {
$error = $this->upload->display_errors();
} else {
$data = array('upload_data' => $this->upload->data());
$file_name = $data['upload_data']['file_name'];
$config['source_image'] = 'uploads/blog/main/' . $file_name;
$config['new_image'] = 'uploads/blog/thumb/' . $file_name;
$config['width'] = 172;
$config['height'] = 122;
$config['maintain_ratio'] = FALSE;
$this->image_lib->initialize($config);
$this->image_lib->resize();
$this->image_lib->clear();
if ($data['upload_data']['image_height'] > $this->main_height || $data['upload_data']['image_width'] > $this->main_width) {
$config['image_library'] = 'gd2';
$config['source_image'] = 'uploads/blog/main/' . $file_name;
$config['new_image'] = 'uploads/blog/main/' . $file_name;
$config['width'] = $this->main_width;
$config['height'] = $this->main_height;
$config['maintain_ratio'] = FALSE;
$this->image_lib->initialize($config);
$this->image_lib->resize();
}
}
}
答案 1 :(得分:0)
修改强>
如果您想将图片尺寸限制为特定width
和height
,请同时设置{{1}的最小和最大 }和width
到配置中的相同值。例如。要将图像大小限制为1024x768,您可以在配置中设置首选项,如下所示
height
如果您使用$config['min_width'] = '1024'; // restrict width to 1024px
$config['min_height'] = '768'; // restrict height to 768px
$config['max_width'] = '1024'; // restrict width to 1024px
$config['max_height'] = '768'; // restrict height to 768px
$this->load->library('upload', $config); // pass the config when loading the library
,请将配置传递给autoload
方法。
initialize
旁注:
我看到你在加载库时传递了$this->upload->initialize($config);
,然后也进行了初始化。这是多余的。如果您使用config
功能,则会使用initialize
方法。
答案 2 :(得分:0)
而不是复制粘贴原始代码,让我为您在php中使用图像上传执行的每个操作提供示例。这样你就可以让我们以下任何一个来实现你的结果。
<强> 1。检查是否收到图像。使用中,强>
if(isset($_FILES["image"])){
// image received
} else {
// image not received
}
<强> 2。检查图像是否正确上传。使用中,强>
if ($_FILES['image']['error'] !== UPLOAD_ERR_OK) {
// error
die("Upload failed with error code " . $_FILES['image']['error']);
} else {
// no error
}
第3。检查图像是否格式有效。使用中,强>
$info = getimagesize($_FILES['image']['tmp_name']);
if ($info === FALSE) {
//Unable to determine image type of uploaded file
}
if (($info[2] !== IMAGETYPE_GIF) && ($info[2] !== IMAGETYPE_JPEG) && ($info[2] !== IMAGETYPE_PNG)) {
// File received is not in valid format
}
<强> 4。要获得图像的大小,请使用上述方法以及
$width = $info[0];
$height = $info[1];
4.1检查图像是否为方形,
if($width!=$height) {
//Image is not square
}
4.1检查最小像素图像宽度
if($width<50) { // Image width should be atleast 50 pixel }
您可以使用条件语句来接收您要查找的所需图像大小。
要上传图片(不一定需要采用以下格式,请根据需要进行修改),
$ imagename =($ _ FILES [&#34;图像&#34;] [&#34; tmp_name的值&#34;]);
$ filename = $ _FILES [&#39; image&#39;] [&#39; name&#39;];
$ temp = explode(&#34;。&#34;,$ _ FILES [&#34; image&#34;] [&#34; name&#34;]);
$ filename = rand(1,99999)。&#39;。&#39; .end($ temp);
move_uploaded_file($ _ FILES [&#34; image&#34;] [&#34; tmp_name&#34;],&#34; ../ your_path /&#34;。$ filename);