I have searched a million pages today, without any solutions.

I can add images to MySql, Pull them from the database and show them successfully. My problem is that they are all in different width and height sizes.

What I then tried to do is to set the size BEFORE I add it to MYSQL, this is where my problem lies. The code below is for the resize, which works fine if I specify a specific image. In my case, the user will load the image via input type: file method, which only returns the image name and not the full path. With the code below, I need to get a file path, or a temp path, which is what I can not seem to manage.

Any ideas on this?

Code for resize, working fine with specific named files, how to get the "uploaded" path name and set its value to "$image_path? -

<?php

$image_path = "Main-bg.jpg";

list($width, $height, $type, $attr)= getimagesize($image_path); 

//specify what percentage you are resizing to
$percent_resizing = 20;

$new_width = round((($percent_resizing/100)*$width));
$new_height = round((($percent_resizing/100)*$height));

echo '<img src="'.$image_path.'" height="'.$new_height.'" width="'.$new_width.'">';

?>

This code is to upload the pictures etc. I think I need to add the resize code here somewhere, not sure where, obviously AFTER I got a path -

Test.php -

<?php
$conn = mysql_connect("localhost", "MyUsername", "xxxxxxxxx")
  OR DIE (mysql_error());
@mysql_select_db ("images", $conn) OR DIE (mysql_error());
// Do this process if user has browse the
// file and click the submit button
if ($_FILES) {
  $image_types = Array ("image/bmp",
                        "image/jpeg",
                        "image/pjpeg",
                        "image/gif",
                        "image/x-png");
  if (is_uploaded_file ($_FILES["userfile"]["tmp_name"])) {
    $userfile  = addslashes (fread
                 (fopen ($_FILES["userfile"]["tmp_name"], "r"),
                 filesize ($_FILES["userfile"]["tmp_name"])));
    $file_name = $_FILES["userfile"]["name"];
    $file_size = $_FILES["userfile"]["size"];
    $file_type = $_FILES["userfile"]["type"];
 
    if (in_array (strtolower ($file_type), $image_types)) {
      $sql = "INSERT INTO image"
             . "(id, image_type, image, image_size, image_name, image_date) ";
      $sql.= "VALUES (";
      $sql.= "'', '{$file_type}', '{$userfile}', '{$file_size}', "
             . "'{$file_name}', NOW())";
      @mysql_query ($sql, $conn);
      Header("Location:".$_SERVER["PHP_SELF"]);
      exit();
    }
  }
}
 
// Do this process of user has click
// a file name to view or remove
if ($_GET) {
  $iid = $_GET["iid"];
  $act = $_GET["act"];
  switch ($act) {
    case 'rem':
      $sql = "DELETE FROM image WHERE id=$iid";
      @mysql_query ($sql, $conn);
      Header("Location:./Test.php");
      exit();
      break;
    default:
      print "<img src=\"image.php?iid=$iid\">";
      break;
  }
}
 
?>
<html>
<head>
<title>Storing Images in DB</title>
</head>
<body>
<form method="post" enctype="multipart/form-data">
Select Image File:
<input type="file" name="userfile"  size="40">
<input type="submit" value="submit">
</form>
<?php
  $sql = "SELECT * FROM image ORDER BY image_date ASC";
  $result = mysql_query ($sql, $conn);
  if (mysql_num_rows($result)>0) {
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
      
	  //if (!isset($i))
	  //$i = "";
	  //else
	  //{
	  $i;
      //if (!isset($str))
	  	//If not isset -> set with dumy value 
		//$str = "";		 
			//else
			//{
	  $str .= $i.". ";
      //$str .= "<a href='image.php?iid=".$row["id"]."'>"
           //. $row["image_name"]."</a> ";
		   
	$str .= "<img src='image.php?iid=".$row["id"]."'>";
	//. $row["image_name"]."</> ";
      //$str .= "[".$row["image_date"]."] ";
      //$str .= "[".$row["image_size"]."] ";
      //$str .= "[<a href='index.php?act=rem&iid=".$row["id"]
           //. "'>Remove</a>]<br>";
			//}
    //}
	}
    print $str;
  }
?>
</body>
</html>

Image.php code -

<?php
// database connection
$conn = mysql_connect("localhost", "MyUserName", "xxxxxxx")
  OR DIE (mysql_error());
@mysql_select_db ("store", $conn) OR DIE (mysql_error());
$sql    = "SELECT * FROM image WHERE id=".$_GET["iid"];
$result = mysql_query ($sql, $conn);
if (mysql_num_rows ($result)>0) {
  $row = @mysql_fetch_array ($result);
  $image_type = $row["path"];
  $image = $row["image"];
  Header ("Content-type: $image_type");
  print $image;
}
?>

Recommended Answers

All 3 Replies

Thanks Kraai, but still my problem exist. I know how to resize etc, what I do not know, is how to get the path to set it to "path"...

The returned path from all browsers is something like C:/fakepath..., where fake path does not help me a lot.:)

function create_thumbnail( $source_file, $destination_file, $max_dimension,$sc_quality)
{
    list($img_width,$img_height) = getimagesize($source_file); // Get the original dimensions

In the above code, I need to get the $source_file, which seems to be impossible. Without the source file, I can not get the sizes to resize the image.

Hi Andre

Your problem made me study some more. Can the problem be that you do not specify a folder other than the "temp" after the user uploaded the image to store it permanent? Can it also be that in your form script, you have no "form action" ?

Example for the form script specify the action:

<form action="test.php" method="post"
enctype="multipart/form-data">

Example of storing (saving) the "temp" uploaded file in a directory:

{
      move_uploaded_file($_FILES["file"]["tmp_name"],
      "upload/" . $_FILES["file"]["name"]);
      echo "Stored in: " . "upload/" . $_FILES["file"]["name"];
      }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.