将缩放图像(缩略图)上载到数据库中 - PHP

时间:2014-01-26 20:13:45

标签: php image gd scaling

我正在制作一个简单的网络应用程序,允许商店的所有者将商品添加到他的库存中,并查看表格中所有商品及其信息(包括商品的图片)的列表。上传的项目图片通常很大。我设法缩小图片并将其输出到下面代码中的页面,但我想保存缩放图像,将其上传到我选择的目录。到目前为止,这是我的代码:

<?php

include'connect.php';

if( isset($_POST['item_name']) && isset($_POST['description']) && isset($_POST['price']) && isset($_POST['quantity']) && isset($_POST['manufacturer']) && isset($_FILES['pic']['name']) )
{
    $name = $_FILES['pic']['name'];
    $tmp_name = $_FILES['pic']['tmp_name'];

    if(!empty($_POST['item_name']) && !empty($_POST['description']) &&  !empty($_POST['price']) && !empty($_POST['quantity']) && !empty($_POST['manufacturer']) &&  !empty($name) )
    {   
        $item_name = $_POST['item_name'];
        $description = $_POST['description'];
        $price = $_POST['price'];
        $quantity = $_POST['quantity'];
        $manufacturer = (int) $_POST['manufacturer'];

        $image = $_POST['pic'];
        $image_size = getimagesize($image);
        $image_width = $image_size[0];
        $image_height = $image_size[1];

        $new_size = ($image_width + $image_height) / ($image_width * ($image_height / 45));
        $new_width = $image_width * $new_size;
        $new_height = $image_height * $new_size;

        $new_image = imagecreatetruecolor($new_width, $new_height);
        $old_image = imagecreatefromjpeg($image);

        imagecopyresized($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height );

        $location = 'Upload/';
        move_uploaded_file($tmp_name, $location.$name);

        $query = "INSERT INTO `item` VALUES ('$item_name', '$description','$price','$quantity','$name','$manufacturer', '')";

        if($query_run = mysql_query($query))
        {   echo 'Item successfully added.';    }


    }
}

?>

<form  action = 'additems.php' method = 'POST' enctype = 'multipart/form-data' >
    Name: <input type = 'text' name = 'item_name' ><br><br>
    Description: <input type = 'text' name = 'description' ><br><br>
    Price: <input type = 'text' name = 'price' ><br><br>
    Quantity: <input type = 'text' name = 'quantity' ><br><br>
    Manufacturer: <select name = 'manufacturer' >
                  <option value = '1'> Manufacturer 1 </option>
                  <option value = '2'> Manufacturer 2 </option>
                  <option value = '3'> Manufacturer 3 </option>
                  <option value = '4'> Manufacturer 4 </option>
                  </select>
                  <br><br>
    Picture: <input type = 'file' name = 'pic' > <br><br>

    <input type = 'submit' value = 'Submit' >

</form>

注意:$ query变量中的SQL语句是我存储正常大小的图像的方式。

0 个答案:

没有答案