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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

php upload & resize not inserting the correct thing in DB

Status
Not open for further replies.

thewhistler1

Technical User
Jan 20, 2008
35
US
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>

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>";

} 

?>
If I remove the parts that try to resize the image then it will work but I need the image resized.
 
you are not using the gd functions properly. they use images rather than file names. read up in the manual.

this code resizes the uploaded image and stores the thumbnail in the database. this is a bad idea. it is better by far to store files in a filesystem and metadata in a database. the original file is discarded.

this code also will upscale images. this is generally considered pointless as the quality loss is great.

Code:
list($width, $height, $type) = getimagesize($_FILES['picture']['tmp_name']);

//calculate dimensions
$maxwidth = 400;
$diff = $maxwidth / $width;    
$newwidth = $width * $diff;
$newheight = $height * $diff;

//create new canvas
$thumb = imagecreatetruecolor($newwidth, $newheight);
$c = 'imagecreatefrom'.$type ($_FILES['picture']['tmp_name']);
$o = 'image'.$type;

$old = $c($_FILES['picture']['tmp_name']);
imagecopyresampled($thumb, $old, 0,0,0,0,$newwidth, $newheight, $width, $height);
$new = mysql_escape_string($o($thumb));
imagedestroy($c);
imagedestroy($thumb);
$query = "INSERT INTO photos (listing_id, sequence, photo) VALUES ('1','1','$new')";
$result = mysql_query($query) ? '<br/>File Uploaded<br/>' : mysql_error();
echo $result;
 
this code resizes the uploaded image and stores the thumbnail in the database. this is a bad idea. it is better by far to store files in a filesystem and metadata in a database
If you run a web farm (i.e. more than 2 servers) storing in a file system can be a little tricky and you would need to share the directory somehow (easy on windows) or you might distribute the images to each machine which also has its problems. Storing the images in the database would priovide you with distributed storage with security as well as the indexing so is actualy a good idea.
So, as with most things in this game, you have to carefully condier what it is you are actualy trying to do
 
jpadie

It doesn't seem to like the following line, it gave an error.

$c = 'imagecreatefrom'.$type ($_FILES['picture']['tmp_name']);

PHP Fatal error: Function name must be a string

Any ideas on what we did wrong?

Thanks
 
apologies

Code:
$c = 'imagecreatefrom'.$type;
 
That wont work either as $type returns a number.
1 = gif
2 = jpeg
3 = png

so tried changing it to this

//create new canvas
$thumb = imagecreatetruecolor($newwidth, $newheight);
if ($image_type == 2){
$c = 'imagecreatefromjpeg';
$o = 'imagejpeg';
} elseif ($image_type == 1){
$c = 'imagecreatefromgif';
$o = 'imagegif';
} elseif ($image_type == 3){
$c = 'imagecreatefrompng';
$o = 'imagepng';
}

but it still isnt working, now I get this error:
PHP Fatal error: Function name must be a string

on this line:
$old = $c($_FILES['picture']['tmp_name']);

Below is the whole code at this point. Any one have an idea on how to get this to work?

Code:
list($width, $height, $type) = getimagesize($_FILES['picture']['tmp_name']);

//calculate dimensions
$maxwidth = 400;
$diff = $maxwidth / $width;    
$newwidth = $width * $diff;
$newheight = $height * $diff;

//create new canvas
$thumb = imagecreatetruecolor($newwidth, $newheight);
if ($image_type == 2){
$c = 'imagecreatefromjpeg';
$o = 'imagejpeg';
} elseif ($image_type == 1){
$c = 'imagecreatefromgif';
$o = 'imagegif';	
} elseif ($image_type == 3){
$c = 'imagecreatefrompng';
$o = 'imagepng'; 
}

$old = $c($_FILES['picture']['tmp_name']);
imagecopyresampled($thumb, $old, 0,0,0,0,$newwidth, $newheight, $width, $height);
$new = mysql_escape_string($o($thumb));
imagedestroy($c);
imagedestroy($thumb);
$query = "INSERT INTO photos (listing_id, sequence, photo) VALUES ('1','1','$new')";
$result = mysql_query($query);

if (!$result) {
echo mysql_error();
} else { 
echo "<br>File uploaded<br>";
}
 
change this line
Code:
list($width, $height, $type) = getimagesize($_FILES['picture']['tmp_name']);

to
Code:
list($width, $height, $image_type) = getimagesize($_FILES['picture']['tmp_name']);
 
OK, I can't seem to get this to work.
If I was to take your initial suggestion jpadie and put the files in a folder and just have links to the database how would I change this code to be able to resize the images before placing them in the folder?

Thanks
 
i was not thinking straight. imageXX outputs to the browser or the filesystem, not to a string. you will have to do the following

Code:
list($width, $height, $image_type) = getimagesize($_FILES['picture']['tmp_name']);

//calculate dimensions
$maxwidth = 400;
$diff = $maxwidth / $width;    
$newwidth = $width * $diff;
$newheight = $height * $diff;

//create new canvas
$thumb = imagecreatetruecolor($newwidth, $newheight);
if ($image_type == 2){
$c = 'imagecreatefromjpeg';
$o = 'imagejpeg';
} elseif ($image_type == 1){
$c = 'imagecreatefromgif';
$o = 'imagegif';    
} elseif ($image_type == 3){
$c = 'imagecreatefrompng';
$o = 'imagepng'; 
}

$old = $c($_FILES['picture']['tmp_name']);
imagecopyresampled($thumb, $old, 0,0,0,0,$newwidth, $newheight, $width, $height);
$fName = tempnam(sys_get_temp_dir(), 'tmp_image');
$f = $o($thumb, $fName);
$new = mysql_escape_string(file_get_contents($fName));
unlink($fName);
imagedestroy($c);
imagedestroy($thumb);
$query = "INSERT INTO photos (listing_id, sequence, photo) VALUES ('1','1','$new')";
$result = mysql_query($query);

if (!$result) {
	echo mysql_error();
} else { 
	echo "<br>File uploaded<br>";
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top