📄 uploadimage.php
字号:
<?php
include('..\dbconnect.php');
$id=$_GET['id'];
$filename=$id."photo";
// 生成图片的宽度和高度
$resizewidth=400;
$resizeheight=400;
function ResizeImage($im,$maxwidth,$maxheight,$name){
$width = imagesx($im);
$height = imagesy($im);
if(($maxwidth && $width > $maxwidth) || ($maxheight && $height > $maxheight)){
if($maxwidth && $width > $maxwidth){
$widthratio = $maxwidth/$width;
$resizewidth=true;
}
if($maxheight && $height > $maxheight){
$heightratio = $maxheight/$height;
$resizeheight=true;
}
if($resizewidth && $resizeheight){
if($widthratio < $heightratio){
$ratio = $widthratio;
}else{
$ratio = $heightratio;
}
}elseif($resizewidth){
$ratio = $widthratio;
}elseif($resizeheight){
$ratio = $heightratio;
}
$newwidth = $width * $ratio;
$newheight = $height * $ratio;
if(function_exists("imagecopyresampled")){
$newim = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}else{
$newim = imagecreate($newwidth, $newheight);
imagecopyresized($newim, $im, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}
ImageJpeg ($newim,$name . ".jpg");
ImageDestroy ($newim);
}else{
ImageJpeg ($im,$name . ".jpg");
}
}
if($_FILES['image']['size']){
if($_FILES['image']['type'] == "image/pjpeg"){
$im = imagecreatefromjpeg($_FILES['image']['tmp_name']);
}
elseif($_FILES['image']['type'] == "image/x-png"){
$im = imagecreatefrompng($_FILES['image']['tmp_name']);
}
elseif($_FILES['image']['type'] == "image/gif"){
$im = imagecreatefromgif($_FILES['image']['tmp_name']);
}
if($im){
ResizeImage($im,$resizewidth,$resizeheight,$filename);
ImageDestroy ($im);
}
if ($submit) {
$sql_image="update products set photo_filename='".$filename."' where id ='$id'";
//echo $sql_image;
mysql_query($sql_image,$conn) or die("出错;".mysql_error());
header("Location:../product_view.php?id=$id");
}
}
?>
<script language="javascript">
function check() {
if (form.image.value=="") {
alert("你没有选择图片!")
return false
}
}
</script>
<form method="post" enctype="multipart/form-data" name="form" id="from" onSubmit=" return check()">
<p> 更新图片</p>
<p>
<input type="file" name="image" size="50" value="浏览">
</p>
<p>
<input name="submit" type="submit" id="submit" value="上传图片">
</p>
</form>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -