thewhistler1
Technical User
Hello
Im trying to upload and resize an image and insert it into a MySQL database, but I am doing something wrong. It inserts a record but it is very small and not the image at all. When I check the error log is shows this:
PHP Warning: imagecopyresized(): supplied argument is not a valid Image resource
Here is my code, can someone tell me what I am doing wrong. Thanks in advance.
First I have a simple form to submit the file.
<form enctype="multipart/form-data" action="getpic.php" method="post">
<input type="file" name="picture" style="width:250px"><input type="hidden" name="MAX_FILE_SIZE" value="1024000"><br>
<input type='submit' value='Upload'>
</form>
If I remove the parts that try to resize the image then it will work but I need the image resized.
Im trying to upload and resize an image and insert it into a MySQL database, but I am doing something wrong. It inserts a record but it is very small and not the image at all. When I check the error log is shows this:
PHP Warning: imagecopyresized(): supplied argument is not a valid Image resource
Here is my code, can someone tell me what I am doing wrong. Thanks in advance.
First I have a simple form to submit the file.
<form enctype="multipart/form-data" action="getpic.php" method="post">
<input type="file" name="picture" style="width:250px"><input type="hidden" name="MAX_FILE_SIZE" value="1024000"><br>
<input type='submit' value='Upload'>
</form>
Code:
<?php
//Removed the connection to the database info
$fileName = file_get_contents($_FILES['picture']['tmp_name']);
list($width, $height) = getimagesize($_FILES['picture']['tmp_name']);
$fileName = mysql_real_escape_string($fileName);
$maxwidth = 400;
$diff = $maxwidth / $width;
$newwidth = $width * $diff;
$newheight = $height * $diff;
$thumb = imagecreatetruecolor($width, $height);
imagecopyresized($thumb, $fileName, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
$query = "INSERT INTO photos (listing_id, sequence, photo) VALUES ('1','1','$thumb')";
$result = mysql_query($query);
if (!$result) {
echo mysql_error();
} else {
echo "<br>File uploaded<br>";
}
?>