首页 > 解决方案 > 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 新手,也是这里的新手。

标签: phpmysqldatabase

解决方案


推荐阅读