Jump to content

Recommended Posts

hello everyone

i am creating a php page where one can create a profile with picture

 

i need to crop the uploaded image before inserting it in the mysql database...can anyone give me the code for that....

I am able to save the picture directly in the database using blob... now i need to crop the image to a particular size before inserting it into the datsabase,as user will upload different size picture i need to save them in one particular dimension

 

 

 

THANKING YOU IN ADVANCE

 

function generate_Thumbnail($file) {
	$file = 'dirname/'$file;

	header('Content-type: image/jpeg');

	list($width, $height) = getimagesize($file);
	$modwidth = 120;
	$modheight = 90;

	$tn= imagecreatetruecolor($modwidth, $modheight);

	$extension = strtolower(substr($file, strrpos($file, ".") + 1));

	switch ($extension)
	{
		case 'jpg':
			$source = imagecreatefromjpeg($file);
			break;
		case 'jpeg':
			$source = imagecreatefromjpeg($file);
			break;
		case 'png':
			$source = imagecreatefrompng($file);
			break;
		case 'gif':
			$source = imagecreatefromgif($file);
			break;
		default:
			die("Image is of unsupported type.");
	}

	#$source = imagecreatefromjpeg($file);

	imagecopyresized($tn, $source, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);

	imagejpeg($tn);

}

 

You can use that to generate the thumbnail to insert it into that database, you will need to write the file to a tmp file somewhere then use file_get_contents/fopen to read the file into a variable then insert it into the database

I am able to store the image in a directory as well as to a database directly... what i was trying to do is croping the image and then inserting into the database,i am able to crop the image too and save it in a directory but i am not been able to link both the program  that is after croping the image and saving it into the directory i want to insert that file into the databse which i am not been able to do it,i know i am making a silly mistake but cant help it i am new in php,i am posting my both code can you please link them

 

CODE TO CREATE THE THUMBNAIL AND SAVED IN THE DIRECTORY /images

 

// Get the details of "imagefile"

$filename = $_FILES['imagefile']['name'];

$temporary_name = $_FILES['imagefile']['tmp_name'];

$mimetype = $_FILES['imagefile']['type'];

$filesize = $_FILES['imagefile']['size'];

 

//Open the image using the imagecreatefrom..() command based on the MIME type.

switch($mimetype) {

case "image/jpg":

case "image/jpeg":

$i = imagecreatefromjpeg($temporary_name);

break;

case "image/gif":

$i = imagecreatefromgif($temporary_name);

break;

case "image/png":

$i = imagecreatefrompng($temporary_name);

break;

}

 

//Delete the uploaded file

unlink($temporary_name);

 

//Save a copy of the original

imagejpeg($i,"images/uploadedfile.jpg",80);

 

//Specify the size of the thumbnail

$dest_x = 150;

$dest_y = 150;

 

//Is the original bigger than the thumbnail dimensions?

if (imagesx($i) > $dest_x or imagesy($i) > $dest_y) {

//Is the width of the original bigger than the height?

if (imagesx($i) >= imagesy($i)) {

$thumb_x = $dest_x;

$thumb_y = imagesy($i)*($dest_x/imagesx($i));

} else {

$thumb_x = imagesx($i)*($dest_y/imagesy($i));

$thumb_y = $dest_y;

}

} else {

//Using the original dimensions

$thumb_x = imagesx($i);

$thumb_y = imagesy($i);

}

 

//Generate a new image at the size of the thumbnail

$thumb = imagecreatetruecolor($thumb_x,$thumb_y);

 

//Copy the original image data to it using resampling

imagecopyresampled($thumb, $i ,0, 0, 0, 0, $thumb_x, $thumb_y, imagesx($i), imagesy($i));

 

//Save the thumbnail

imagejpeg($thumb, "images/thumbnail.jpg", 80);

 

 

CODE TO INSERT A FILE INTO MY DATABASE

 

if (isset($_FILES['image']) && $_FILES['image']['size'] > 0)

{

 

      // Temporary file name stored on the server

      $tmpName  = $_FILES['image']['tmp_name']; 

     

      // Read the file

      $fp      = fopen($tmpName, 'r');

      $data = fread($fp, filesize($tmpName));

      $data = addslashes($data);

      fclose($fp);

     

 

      // Create the query and insert

      // into our database.

      $query = "INSERT INTO tbl_images ";

      $query .= "(image) VALUES ('$data')";

      $results = mysql_query($query, $link);

     

      // Print results

      print "Thank you, your file has been uploaded.";

     

  }

else {

  print "No image selected/uploaded";

    }

}

 

 

BOTH THE CODE ARE WORKING FINE SEPERATLY

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.