-1

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;

?>
Emerald
  • 864
  • 1
  • 14
  • 37
  • Resizing an image is easier said than done. You'll need to use one of a few libraries, and it is beyond the scope of StackOverflow to suggest you one. Please settle on one, try to get this to work, and ask a new question once you encounter a problem you cannot solve yourself. – Sumurai8 Aug 15 '15 at 19:55
  • Sorry I did view some tutorial but I just don't understand it. I am really new to this. – Emerald Aug 15 '15 at 20:02
  • Well, start [here](http://stackoverflow.com/questions/9650572/resize-image-php). You possibly need to install a library. – Sumurai8 Aug 15 '15 at 20:09
  • I'll try it out, thanks. – Emerald Aug 15 '15 at 20:20

2 Answers2

0

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/>';
?>
DirtyBit
  • 16,613
  • 4
  • 34
  • 55
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";
  }
ANSHUL GERA
  • 306
  • 3
  • 11