function addAvathar() {
$imgtype=$_FILES['userfile']['name'];
$imext=explode('.',$imgtype);
$ext=end($imext);
if(($ext!='gif')&&($ext!='jpeg')&&($ext!='png')&&($ext!='jpg'))
{
echo '<script type="text/javascript">document.getElementById("popup_upload").style.display = "none";alert("upload correct file type");</script>';
exit();
}
else{
$config['upload_path'] = './avatar/';
$config['allowed_types'] = 'gif|jpeg|png|jpg';
$config['max_size'] = '1024mb';
$config['max_width'] = '3000';
$config['max_height'] = '3000';
$this->load->library('upload', $config);
if (!$this->upload->do_upload()) {
$ve['data'] = $this->upload->display_errors();
} else {
$this->upload->data();
}
if ($this->input->post("submit")) {
$title = $this->input->post('title');
$fInfo = $this->upload->data();
if ($fInfo['image_width'] > 600) {
$src = './avatar/' . $fInfo['file_name'];
$percent = 0.5;
$width = $fInfo['image_width'];
$height = $fInfo['image_height'];
$newwidth = $width * $percent;
$newheight = $height * $percent;
// Load
$thumb = ImageCreateTrueColor($newwidth, $newheight);
$extention = $fInfo['file_ext'];
// create new jpeg image based on the target sizes
switch ($extention) {
case 'jpg':
$img_r = imagecreatefromjpeg($src);
break;
case 'jpeg':
$img_r = imagecreatefromjpeg($src);
break;
case 'png':
$img_r = imagecreatefrompng($src);
break;
case 'gif':
$img_r = imagecreatefromgif($src);
break;
default:
$img_r = imagecreatefromjpeg($src);
}
// Resize
imagecopyresized($thumb, $img_r, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($thumb, $src, 90);
}
$ban = array('id' => '', 'title' => $title);
$this->db->insert('bannar', $ban);
echo '<script type="text/javascript">window.top.window.show_popup_crop("../../avatar/' . $fInfo['file_name'] . '")</script>';
} else {
$this->load->view('crop', array('edt' => $edit['result'], 'result' => $data['result']));
}
}
}
答案 0 :(得分:0)
在PHP脚本中,如果您使用任何HTML标记意味着您可以在PHP脚本中运行javascript,如下面的代码。
echo "<div id='popup_upload'></div>";
echo "<script type='text/javascript'>document.getElementById('popup_upload').style.display = 'none';alert('upload correct file type');</script>";
然后只有脚本可以在HTML行中找到提到的id。更好的是,您可以在PHP中返回错误消息并将其显示在前端HTML文件中。