Black Block after resize?

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
Catz
Forum Newbie
Posts: 8
Joined: Wed Dec 15, 2004 4:08 pm

Black Block after resize?

Post 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]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Catz
Forum Newbie
Posts: 8
Joined: Wed Dec 15, 2004 4:08 pm

Post by Catz »

Hi

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

Thnx
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Post Reply