Jump to content

[SOLVED] Create thumbnail/ store in MySQL problem


dbillings

Recommended Posts

I'm trying to store an image file in mysql after I resize the image to a value equal or below .300 mb. The data always ends up being 14 bytes I'm not sure whats going wrong here.

 

<?php
######################################
### Script - uploadimages.php
### Author - Billings
### Created - 12/14/2007
######################################



if(isset($_REQUEST['delete'])){

$id= $_REQUEST['id'];

include("mysql_connect.php");

$query= "DELETE FROM trinity_images WHERE id='$id'";
$result= mysql_query($query)or die();

if($result){

echo "Image deleted.";

}

}

if(isset($_POST['upload']) && $_FILES['userfile']['size'] > 0) {

ini_set('memory_limit', '200m');	

$fileName = $_FILES['userfile']['name'];
$tmpName  = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

if($fileSize > 314573) { // Resize image if it's over .300 mb's.

	if(!get_magic_quotes_gpc()) {
    		$fileName = addslashes($fileName);
	}

	$percent = 314573 / $fileSize;

	echo "Percent: ".$percent;
	echo "<br />Filesize: ".$fileSize;


	$newfilesize = $fileSize * $percent;

	echo "<br />New filesize: ".$newfilesize;


	list($width, $height) = getimagesize($tmpName);

	echo "<br />Width: ".$width;
	echo "<br />Height: ".$height;	

	$newwidth = floor($width * $percent);
	$newheight = floor($height * $percent);

	echo "<br />New width: ".$newwidth;
	echo "<br />New height: ".$newheight;

	$newimage = imagecreatetruecolor($newwidth, $newheight);

	$source = imagecreatefromjpeg($tmpName);		

	imagecopyresized($newimage, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);



}			

if($filetype == "image/pjpeg" || "image/gif" || "image/png"){



	include("mysql_connect.php");


	$query = "INSERT INTO trinity_images (name, type, size, data ) ".
	"VALUES ('$fileName' , '$fileType' , '$newfilesize', '$newimage')";

	$data = mysql_query($query) or die('Error: '.mysql_error()); 

	mysql_close();

	if($data) {

		echo "<br />File $fileName uploaded<br />";

	}else{

		echo "<br />File $fileName failed to upload<br />";
	}
}else{

	echo "<br />File type ".$filetype." not allowed. Must be JPEG,GIF or PNG.<br />";

} 

}
?>


<fieldset><legend>Upload Images</legend>
<form method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<label id='label'>Select image:</label><input id='input' name="userfile" type="file" id="userfile"><br /> 
<input id='submit' name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>
</fieldset>

Link to comment
Share on other sites

Ok I'm running into problems again, I reworked my code and my problem is that when I use

 

imagecopyresized($thumb, $orig_image, 0, 0, 0, 0, $thumbw, $thumbh, $width, $height);

 

I try to upload $thumb to mysql and it is storing Resource id # 5 rather than the resource itself. How do I solve this?

 

Here's my new code:

 

<?php

if(isset($_REQUEST['upload'])) {

ini_set('memory_limit', '200m');

$filesize = $_FILES['userfile']['size'];
$filename = $_FILES['userfile']['name'];
$filetype = $_FILES['userfile']['type'];
$tmpname = $_FILES['userfile']['tmp_name'];
$desc = $_REQUEST['description'];

if($filetype == 'image/pjpeg') {

	$orig_image = imagecreatefromjpeg($tmpname);

	list($width, $height) = getimagesize($tmpname);

	if($filesize > 157286) { // Resize image if it's over 150 kb's.

		$percent = 157286 / $filesize;

		$newwidth = $width * $percent;
		$newheight = $height * $percent;

		$new_image = imagecreatetruecolor($newwidth, $newheight);

		imagecopyresized($new_image, $orig_image, 0, 0, 0, 0,$newwidth, $newheight, $width, $height);

	}

	// Generate thumbnail images.

	$thumbw = 50;

	$thumbh = $thumbw / $width * $height;

	$thumb = imagecreatetruecolor($thumbw, $thumbh);

	imagecopyresized($thumb, $orig_image, 0, 0, 0, 0, $thumbw, $thumbh, $width, $height);



	// create mysql query.

	include("mysql_connect.php");

	$query = "INSERT INTO trinity_images (name, description, size, image, thumb)
		  VALUES ('$filename','$desc','$filesize','$image','$thumb_image')";

	$result = mysql_query($query) or die(mysql_error());

	if($result) {

		echo "
<pre>
Filename: $filename
Filesize: ".$filesize * $percent."
Filetype: $filetype
Filestatus: Your file uploaded successfully!
</pre>";

	}else{

		echo "File failed to upload.";

	}

}else{

	echo "This program only supports the upload of jpeg files <br />
	      your image is a $filetype file.";

}



}

?>

<html>

<head>

</head>

<body>

<?php
echo $newfilename;
?>

<fieldset><legend>Upload Images</legend>
<form method="post" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="2000000">
<label id='label'>Select image:</label><input id='input' name="userfile" type="file" id="userfile"><br /> 
<label id='label'>Description:</label><br />
<textarea name='description' cols='30' rows='7'></textarea><br />
<input id='submit' name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>
</fieldset>



</body>

</html>

Link to comment
Share on other sites

Solution

 

<?php



if(isset($_REQUEST['upload'])) {



ini_set('memory_limit', '200m');

$filesize = $_FILES['userfile']['size'];
$filename = $_FILES['userfile']['name'];
$filetype = $_FILES['userfile']['type'];
$tmpname = $_FILES['userfile']['tmp_name'];
$desc = $_REQUEST['description'];

if($filetype == 'image/pjpeg') {

	$orig_image = imagecreatefromjpeg($tmpname);

	list($width, $height) = getimagesize($tmpname);

	if($filesize > 157286) { // Resize image if it's over 150 kb's.

		$percent = 157286 / $filesize;

		$newwidth = $width * $percent;
		$newheight = $height * $percent;

		$new_image = imagecreatetruecolor($newwidth, $newheight);

		imagecopyresized($new_image, $orig_image, 0, 0, 0, 0,$newwidth, $newheight, $width, $height);

	}

	// Generate thumbnail images.

	$thumbw = 50;

	$thumbh = $thumbw / $width * $height;

	$thumb = imagecreatetruecolor($thumbw, $thumbh);

	imagecopyresized($thumb, $orig_image, 0, 0, 0, 0, $thumbw, $thumbh, $width, $height);

	ob_start(); // Start capturing stdout.		

	imagejpeg($thumb); // As though output to browser.

	$binarythumb = ob_get_contents(); // the raw jpeg image data.

	$thumb = addslashes($binarythumb);

	imagejpeg($new_image);

	$binaryimage = ob_get_clean();

	$image = addslashes($binaryimage);

	// create mysql query.

	include("mysql_connect.php");

	$query = "INSERT INTO trinity_images (name, description, size, image, thumb)
		  VALUES ('$filename','$desc','$filesize','$image','$thumb')";

//	var_dump($query);

	$result = mysql_query($query) or die(mysql_error());



	if($result) {

		echo "
<pre>
Filename: $filename
Filesize: ".$filesize * $percent."
Filetype: $filetype
Filestatus: Your file uploaded successfully!
</pre>";

	}else{

		echo "File failed to upload.";

	}

}else{

	echo "This program only supports the upload of jpeg files <br />
	      your image is a $filetype file.";

}



}

?>

<html>

<head>

</head>

<body>

<?php
echo $newfilename;
?>

<fieldset><legend>Upload Images</legend>
<form method="post" enctype="multipart/form-data">
<input type="hidden" action="<?php $_SERVER['PHP_SELF']; ?>" name="MAX_FILE_SIZE" value="2000000">
<label id='label'>Select image:</label><input id='input' name="userfile" type="file" id="userfile"><br /> 
<label id='label'>Description:</label><br />
<textarea name='description' cols='30' rows='7'></textarea><br />
<input id='submit' name="upload" type="submit" class="box" id="upload" value=" Upload "></td>
</tr>
</table>
</form>
</fieldset>

</body>

</html>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.