Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Black block after image resize?

Status
Not open for further replies.

Kriekie

Technical User
Nov 21, 2004
10
ZA
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>
 
Firstly, you can't resize the BMP.
Secondly, resizing is more complicated that how it looks like.
You can try this, but this does not solve the entire problem.

Code:
$resized = ImageCreateTrueColor($newwidth,$newheight);
switch ($file_type)
{
 case 1: case 2: $original = imagecreatefromjpeg ($_FILES["userfile"]["tmp_name"]); break;
 case 3: $original = imagecreatefromgif($_FILES["userfile"]["tmp_name"]); break;
 case 4: $original = imagecreatefrompng($_FILES["userfile"]["tmp_name"]); break;
}

if ($original)
{
   imagecopyresampled($resized, $original, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

switch ($file_type)
{
 case 1: case 2: imagejpeg ($resized); break;
 case 3: imagegif ($resized); break;
 case 4: imagepng ($resized); break;
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top