Hi
I have this script that uploads a file, resizes it and saves it as a blob into a database, then displays it. Without the resizing it works fine, but as soon as I resize it the image is replaced by a black block. Any help will be appreciated.
<?php
error_reporting(E_NONE);
$conn = mysql_connect("localhost","user","database") OR DIE (mysql_error());
@mysql_select_db ("table", $conn) OR DIE (mysql_error());
if ($_FILES) {
$image_types = Array ("image/bmp",
"image/jpeg",
"image/pjpeg",
"image/gif",
"image/x-png");
$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"];
list($width, $height) = getimagesize($userfile);
$newwidth = 300;
$newheight = 200;
$resized = ImageCreateTrueColor($newwidth,$newheight);
$original = imagecreatefromjpeg($imgfile);
imagecopyresized($resized, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($resized);
if (in_array (strtolower ($file_type), $image_types)) {
$sql = "INSERT INTO image (image_type, image, image_size, image_name, image_date) ";
$sql.= "VALUES (";
$sql.= "'{$file_type}', '{$resized}', '{$file_size}', '{$file_name}', NOW())";
@mysql_query ($sql, $conn);
Header("Location:image.php?image=$file_name");
exit();
}
}
?>
<html>
<head>
<title>Upload Form</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>
</body>
</html>
I have this script that uploads a file, resizes it and saves it as a blob into a database, then displays it. Without the resizing it works fine, but as soon as I resize it the image is replaced by a black block. Any help will be appreciated.
<?php
error_reporting(E_NONE);
$conn = mysql_connect("localhost","user","database") OR DIE (mysql_error());
@mysql_select_db ("table", $conn) OR DIE (mysql_error());
if ($_FILES) {
$image_types = Array ("image/bmp",
"image/jpeg",
"image/pjpeg",
"image/gif",
"image/x-png");
$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"];
list($width, $height) = getimagesize($userfile);
$newwidth = 300;
$newheight = 200;
$resized = ImageCreateTrueColor($newwidth,$newheight);
$original = imagecreatefromjpeg($imgfile);
imagecopyresized($resized, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagejpeg($resized);
if (in_array (strtolower ($file_type), $image_types)) {
$sql = "INSERT INTO image (image_type, image, image_size, image_name, image_date) ";
$sql.= "VALUES (";
$sql.= "'{$file_type}', '{$resized}', '{$file_size}', '{$file_name}', NOW())";
@mysql_query ($sql, $conn);
Header("Location:image.php?image=$file_name");
exit();
}
}
?>
<html>
<head>
<title>Upload Form</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>
</body>
</html>