dsio68964998 2015-08-15 19:52
浏览 12

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

I make a simple uploading and storing picture in mysqli database. I can upload image and store it in database but when displaying it, the picture will be displayed in actual size and it is too large. Can somebody help me out adding some coding to resize pictures uploaded by user? Just resize for view purpose. I don't know how to code it. Here is my coding for 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>

Here is my coding to get image 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;

?>
  • 写回答

2条回答 默认 最新

  • dsfsdfsd34324 2015-08-15 20:28
    关注

    As members have already mentioned to use the available libraries but since you are new and IF you want to show all images to a specific size you could do something like;

    <?php 
    echo '<img src="data:image/jpeg;base64,'.base64_encode( $image['image'] ).'" height= 100 width = 150/>';
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,
  • ¥15 我的代码无法在vc++中运行呀,错误很多