Jump to content

[SOLVED] crop image before inserting into mysql


anujphp

Recommended Posts

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.