Page 1 of 1

Black Block after resize?

Posted: Tue Aug 23, 2005 8:58 am
by Catz
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.

Code: Select all

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

feyd | Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Tue Aug 23, 2005 9:48 am
by feyd

Posted: Tue Aug 23, 2005 10:15 am
by Catz
Hi

I've made the change but still get the same problem:)

Thnx

Posted: Tue Aug 23, 2005 1:22 pm
by feyd
looking further, it appears you're inserting the resource handle, not the image generated.

If you used output buffering to capture the image being output by imagejpeg(), you would then have access to the image stream, instead of just a resource.