Jump to content

Image resize before copying to database


hallucinated

Recommended Posts

here is a little script to resize a POSTed image

    if(isset($_FILES['userfile'])) {
//check file type
        list($width, $height, $imtype, $attr) = getimagesize($_FILES['userfile']['tmp_name']);
	if ($imtype == 3) { // cheking image type                
	$ext="png"; }  // to use it later in HTTP headers  
	elseif ($imtype == 20) {             
	$ext="jpeg"; }
	elseif ($imtype == 2) {             
	$ext="jpg"; }
	elseif ($imtype == 1) {                
	$ext="gif"; }           
	else {               
	$msg = 'Unknown file format. Please select an image file.';    }

// This is the temporary file created by PHP 
$uploadedfile = $_FILES['userfile']['tmp_name'];

// Create an Image from it so we can do the resize
$src = imagecreatefromjpeg($uploadedfile);

// For our purposes, I have resized the image to be
// 230 pixels wide, and maintain the original aspect 
// ratio. This prevents the image from being "stretched"
// or "squashed". If you prefer some max width other than
// 230, simply change the $newwidth variable
$newwidth=230;
$newheight=($height/$width)*230;
$tmp=imagecreatetruecolor($newwidth,$newheight);

// this line actually does the image resizing, copying from the original
// image into the $tmp image
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); 

// here is where i attempted to assign the $tmp image to a variable, i guess that doesn't work
// $image = imagejpeg($tmp);


	// trying to upload the image to a database
	if (!isset($msg)) { // If there was no error
		$data = file_get_contents($tmp);
		$data = mysql_real_escape_string($tmp); // Preparing data to be used in MySQL query     
		mysql_query("UPDATE profile SET picture = '$data' WHERE userid = '$myid'") or die(mysql_error());
		$msg = 'Image updated successfully.';

	}
}

 

For some reason, file_get_contents($tmp); says that $tmp is non-existent. Any help?

Link to comment
Share on other sites

ob_start();
imagejpeg($tmp);
$image = ob_get_clean();



	if (!isset($msg)) { // If there was no error
		//$data = file_get_contents($tmp);
		//$data = mysql_real_escape_string($tmp); // Preparing data to be used in MySQL query     
		mysql_query("UPDATE profile SET picture = '$image' WHERE userid = '$myid'") or die(mysql_error());
		$msg = 'Image updated successfully.';

	}
}

 

results in: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

Link to comment
Share on other sites

Found this solution:

 

imagejpeg($tmp, $filename, 100);
$instr = fopen("$filename", "rb");  //need to move this to a safe directory
$image = addslashes(fread($instr,filesize("$filename")));





	if (!isset($msg)) { // If there was no error
		//$data = file_get_contents($tmp);
		//$data = mysql_real_escape_string($tmp); // Preparing data to be used in MySQL query     
		mysql_query("UPDATE profile SET picture = '$image' WHERE userid = '$myid'") or die(mysql_error());
		$msg = 'Image updated successfully.';

	}

Link to comment
Share on other sites

you could go as is, and assuming you never change $filename, the file will be overridden every run... you'll still have a file sitting in your directory (possibly open to the public), but it's certainly not the end of the world.

 

Also, this code worked for me..

 

ob_start();
imagejpeg($img);
$img = ob_get_contents();
ob_end_clean();

 

It should work for you after I made a modification. I'm still not quite sure why it did not work in the first place, since ob_get_clean() and the two functions I used here are essentially the same.

Link to comment
Share on other sites

No clue why it didn't work for me, that new code didn't work either.

 

And I would and I would want to avoid rewriting the file over and over, especially with multiple people accessing the site at once, the wrong picture my just get inserted into the database. Plus, it appears as if each uploaded retains its filename.

Link to comment
Share on other sites

In your original code it would be something like...

 

    if(isset($_FILES['userfile'])) {
//check file type
        list($width, $height, $imtype, $attr) = getimagesize($_FILES['userfile']['tmp_name']);
	if ($imtype == 3) { // cheking image type                
	$ext="png"; }  // to use it later in HTTP headers  
	elseif ($imtype == 20) {             
	$ext="jpeg"; }
	elseif ($imtype == 2) {             
	$ext="jpg"; }
	elseif ($imtype == 1) {                
	$ext="gif"; }           
	else {               
	$msg = 'Unknown file format. Please select an image file.';    }

// This is the temporary file created by PHP 
$uploadedfile = $_FILES['userfile']['tmp_name'];

// Create an Image from it so we can do the resize
$src = imagecreatefromjpeg($uploadedfile);

// For our purposes, I have resized the image to be
// 230 pixels wide, and maintain the original aspect 
// ratio. This prevents the image from being "stretched"
// or "squashed". If you prefer some max width other than
// 230, simply change the $newwidth variable
$newwidth=230;
$newheight=($height/$width)*230;
$tmp=imagecreatetruecolor($newwidth,$newheight);

// this line actually does the image resizing, copying from the original
// image into the $tmp image
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); 

ob_start();
imagejpeg($tmp);
$image = ob_get_contents();
ob_end_clean();


	// trying to upload the image to a database
	if (!isset($msg)) { // If there was no error
		$image = mysql_real_escape_string($image); // Preparing data to be used in MySQL query     
		mysql_query("UPDATE profile SET picture = '$image' WHERE userid = '$myid'") or die(mysql_error());
		$msg = 'Image updated successfully.';

	}
}

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.