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;
}
?>