调整用户在php和mysqli上传的图像

时间:2015-08-15 19:52:52

标签: php mysqli

我在mysqli数据库中进行了简单的上传和存储图片。我可以上传图像并将其存储在数据库中,但在显示时,图片将以实际尺寸显示,而且太大。有人可以帮我添加一些编码来调整用户上传的图片吗?只需调整大小以便查看。我不知道如何编码。这是我对index.php的编码。

<html>
<head>
    <title>Upload an image</title>
</head>
<body>

<form action="index.php" method="POST" enctype="multipart/form-data">
    File:
    <input type="file" name="image" /> <input type="submit" value="Upload">
</form>

<?php

$db = "databaseimage";

//connect to database
$con = mysqli_connect("127.0.0.1", "root", "MyNewPass") or die (mysqli_error());
mysqli_select_db($con, $db) or die (mysqli_error($db));

//file properties
//tmp_name is a temporary file to store image
// Turn off error reporting
error_reporting(0);
$file = $_FILES['image']['tmp_name'];

if (!isset($file))
    echo "Please select an image.";
else
{
    //addslashes to prevent any mysql injection
    $image = addslashes(file_get_contents($_FILES['image']['tmp_name']));
    $image_name = addslashes($_FILES['image']['name']);
    // getimagesize will get the image size from the temporary files. this will return false value if it is not image.
    $image_size = getimagesize($_FILES['image']['tmp_name']);

    if ($image_size==FALSE)
        echo "That's not an image.";
    else
    {
        if (!$insert = mysqli_query ($con,"INSERT INTO store VALUES ('', '$image_name', '$image')"))
            echo "Problem uploading image!";
        else
        {
            $lastid = mysqli_insert_id($con);
            // id=1 will become a unique id for image. but we are using $lastid so that it can become like 1 or 2 or 3 and so on refer to image id stored in database
            echo "Image uploaded.<p />Your image:<p /><img src=get.php?id=$lastid>";
        }
    }
}

?>

</body>
</html>

以下是获取图片get.php

的编码
<?php

$db = "databaseimage";

//connect to database
$con = mysqli_connect("127.0.0.1", "root", "MyNewPass") or die (mysqli_error());
mysqli_select_db($con, $db) or die (mysqli_error($db));

$id = addslashes ($_REQUEST['id']);

//image given out to the user
$image = mysqli_query($con, "SELECT * FROM store WHERE id=$id"); //solved
$image = mysqli_fetch_assoc($image); //solved
$image = $image['image'];

// type of file changed to an image
header ("Content-type: image/jpeg");

echo $image;

?>

1 个答案:

答案 0 :(得分:0)

  $image_name=$_FILES["file"]["name"];
if ((($_FILES["file"]["type"] == "image/gif")
|| ($_FILES["file"]["type"] == "image/jpeg")
|| ($_FILES["file"]["type"] == "image/png")
|| ($_FILES["file"]["type"] == "image/pjpeg"))
&& ($_FILES["file"]["size"] < 800000))
  {
  if ($_FILES["file"]["error"] > 0)
    {
    echo "Return Code: " . $_FILES["file"]["error"] . "<br />";
    }
  else
    {
      $path="../images/products/";
      $filename = stripslashes($_FILES['file']['name']);
      $big_img_path = "../images";
      $small_img_path = "../images";
      $small2_img_path= "../images";

    if (file_exists($path . $_FILES["file"]["name"]))
      {
      echo $_FILES["file"]["name"] . " already exists. ";
      }
    else
      {
      $extension = getExtension($filename);  // get file name
      $extension = strtolower($extension);  // get extension
      $image_name= date('Ymd').'.'.$extension; // change name of doc as order id
      move_uploaded_file($_FILES["file"]["tmp_name"],$path.$image_name);
      GenerateThumbFile($path.$image_name,$big_img_path.$image_name,200,150,$path,$big_img_path);
      GenerateThumbFile($path.$image_name,$small_img_path.$image_name,150,100,$path,$small_img_path);
      GenerateThumbFile($path.$image_name,$small2_img_path.$image_name,60,60,$path,$small2_img_path);

     echo "<h3 style='color:#008000'>upload is successfull</h3>";

      }
    }
  }
else
  {
     echo "Invalid file";
  }