php - PHP 图像更新、调整大小和重命名
问题描述
我制作了一个演示管理面板,在其中添加了编辑按钮以使用图像更新选项更新数据。这是我的 edit.php 页面,我从数据库中获取数据并显示在他们的字段中:
`<form class="form-horizontal" action='add-new-img.php' method='post' enctype="multipart/form-data">
<div class="box-body">
<div class="form-group row">
<label for="g_name" class="col-sm-2 col-form-label ">Gallery Name <sup class="text-red"> *</sup></label>
<div class="col-sm-8">
<select class="form-control" name="g_id">
<?php
while ($row = mysqli_fetch_assoc($res)) {
echo "<option value='$row[id],$row[g_name]'>$row[g_name]</option>";
}
?>
</select>
</div>
</div>
<div class="form-group row">
<label for="Image Name" class="col-sm-2 col-form-label ">Image Name <sup class="text-red"> *</sup></label>
<div class="col-sm-8">
<input type="text" class="form-control" name="img_name" required>
</div>
</div>
<div class="form-group row">
<label for="Image" class="col-sm-2 col-form-label ">Image <sup class="text-red"> *</sup></label>
<div class="col-sm-8">
<input type="file" name="new_image">
</div>
</div>
<div class="form-group row">
<label for="order_id" class="col-sm-2 col-form-label ">Order Number</label>
<div class="col-sm-8">
<input type="number" class="form-control" name="order_id" required>
</div>
</div>
<div class="form-group row">
<label for="Status" class="col-sm-2 col-form-label ">Status</label>
<div class="col-sm-8">
<select class="form-control" name="status">
<option value="YES">YES</option>
<option value="NO">NO</option>
</select>
</div>
</div>
</div>
<!-- /.box-body -->
<div class="box-footer">
<center>
<button type="Submit" class="btn btn-success ">Save</button>
</center>
</div>
<!-- /.box-footer -->
</form>`
这是我的 update.php 页面,我在其中使用新图像更新数据。但是当我不想更新图像时,只更新数据前图像在那个时候是活跃的或不活跃的,这段代码给出了错误。我注意到它总是在 if 部分继续。
<?php
$con = mysqli_connect("localhost", "root", "", "garnier", "3308");
extract($_POST);
if (isset($_FILES['new_image'])) {
$sql = "select * from images where id='$id'";
$res = mysqli_query($con, $sql);
$row = mysqli_fetch_assoc($res);
unlink("images/small/$row[image]");
unlink("images/medium/$row[image]");
unlink("images/large/$row[image]");
$imagename = $_FILES['new_image']['name'];
$source = $_FILES['new_image']['tmp_name'];
$target = "images/" . $imagename;
move_uploaded_file($source, $target);
$imagepath = $imagename;
$small = "images/small/" . $imagepath; //This is the new file you saving
$medium = "images/medium/" . $imagepath; //This is the new file you saving
$large = "images/large/" . $imagepath; //This is the new file you saving
$file = "images/" . $imagepath; //This is the original file
list($width, $height) = getimagesize($file);
$modwidth = 200;
$diff = $width / $modwidth;
$modheight = $height / $diff;
$tn = imagecreatetruecolor($modwidth, $modheight);
$image = imagecreatefromjpeg($file);
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
imagejpeg($tn, $small, 100);
$modwidth = 500;
$diff = $width / $modwidth;
$modheight = $height / $diff;
$tn = imagecreatetruecolor($modwidth, $modheight);
$image = imagecreatefromjpeg($file);
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
imagejpeg($tn, $medium, 100);
$modwidth = 800;
$diff = $width / $modwidth;
$modheight = $height / $diff;
$tn = imagecreatetruecolor($modwidth, $modheight);
$image = imagecreatefromjpeg($file);
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
imagejpeg($tn, $large, 100);
unlink($file); //Delete our uploaded file
}
$sqlu = "update `images` set `i_name`='$i_name' , `image`='$imagename', `order_id`='$order_id', `status`='$status' where `id`='$id'";
$resu = mysqli_query($con, $sqlu);
header("location:images.php?msg=record updated");
如何完美激活它?我是 php 新手,也是这里的新手。
解决方案
推荐阅读
- mysql - mysql:无法修改或更新日期值 0000-00-00 的列
- android - Ionic 3 如何知道 GPS 是否启用
- android - 首次运行应用程序时出错无法投射问题
- sql - 仅使用 Where 语句获取唯一条目?
- reactjs - 我将如何在自定义域上部署我的 React.js 更改?
- mysql - mysql 我可以按两个日期中最早的日期排序吗?
- java - SVG 文本上的清晰文本
- python - 从一个非常大的生成器中选择一个随机样本
- node.js - 如何显示节点 chid_process 命令的进度?
- javascript - 在更高级别表单的组件之间传递表单字段时遇到问题