crop image before inserting into mysql
Moderator: General Moderators
crop image before inserting into mysql
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
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
- novice4eva
- Forum Contributor
- Posts: 327
- Joined: Thu Mar 29, 2007 3:48 am
- Location: Nepal
Re: crop image before inserting into mysql
I think you would want to resize it
, for that there is a good post by onion2k viewtopic.php?f=28&t=41743, but if you were really talking about crop then imagecopy may be the function of your interest 
Re: crop image before inserting into mysql
thanks for the link it helped me a lot but still unable to solve my issue
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
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
- novice4eva
- Forum Contributor
- Posts: 327
- Joined: Thu Mar 29, 2007 3:48 am
- Location: Nepal
Re: crop image before inserting into mysql
You image is already in $thumb variable.
you can do a simple insert:
and as for inserting into directory, you must have already achieved it.
Code: Select all
imagejpeg($thumb, "images/thumbnail.jpg", 80);Code: Select all
$query = "INSERT INTO tbl_images(image) VALUES ('$thumb')";
Re: crop image before inserting into mysql
i followed your suggestion but it does not work...it saves resource id #5 in the table instead of photo
- novice4eva
- Forum Contributor
- Posts: 327
- Joined: Thu Mar 29, 2007 3:48 am
- Location: Nepal
Re: crop image before inserting into mysql
Can we see your code.
Re: crop image before inserting into mysql
SURE...the code i have already given earlier and i am posting the code which i did after following the suggestion
THE HTML FORM
<form action="imagethumbnail.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="200000">
<input type="file" name="imagefile">
<input type="submit" name="upload" value="Upload Image">
</form>
THE PHP PAGE
<?php
// 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);
$username = "root";
$password = "vertrigo";
$host = "localhost";
$database = "test";
// Make the connect to MySQL or die
// and display an error.
$link = mysql_connect($host, $username, $password);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
// Select your database
mysql_select_db ($database);
// Create the query and insert
// into our database.
$query = "INSERT INTO tbl_images ";
$query .= "(image) VALUES ('$thumb')";
$results = mysql_query($query, $link);
// Print results
print "Thank you, your file has been uploaded.";
?>
THE HTML FORM
<form action="imagethumbnail.php" method="POST" enctype="multipart/form-data">
<input type="hidden" name="MAX_FILE_SIZE" value="200000">
<input type="file" name="imagefile">
<input type="submit" name="upload" value="Upload Image">
</form>
THE PHP PAGE
<?php
// 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);
$username = "root";
$password = "vertrigo";
$host = "localhost";
$database = "test";
// Make the connect to MySQL or die
// and display an error.
$link = mysql_connect($host, $username, $password);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
// Select your database
mysql_select_db ($database);
// Create the query and insert
// into our database.
$query = "INSERT INTO tbl_images ";
$query .= "(image) VALUES ('$thumb')";
$results = mysql_query($query, $link);
// Print results
print "Thank you, your file has been uploaded.";
?>
- novice4eva
- Forum Contributor
- Posts: 327
- Joined: Thu Mar 29, 2007 3:48 am
- Location: Nepal
Re: crop image before inserting into mysql
Hummm that got me wondering too
. Anyways now i have a solution.
You can also use the file_get_contents() function to read the stored image file and save it in database, whichever way you prefer.
PS: while posting your code, could you please use the code tags. Just click the "Code" button (located below the subject).

Code: Select all
//Save the thumbnail
imagejpeg($thumb, "thumbnail.jpg", 80);//THIS SAVES IT IN FILE
ob_start();
imagejpeg($thumb, NULL, 80);//IF SECOND PARAMETER SET TO NULL, OUTPUTS TO THE BROWSER
$contents = ob_get_contents();//SAVE $content IN DATABASE
// end capture
ob_end_clean();
PS: while posting your code, could you please use the code tags. Just click the "Code" button (located below the subject).
Re: crop image before inserting into mysql
I will follow your suggestion while posting code..
i did this also its same...the problem has not been rectified..
Thank for taking so much time for me....
i did this also its same...the problem has not been rectified..
Thank for taking so much time for me....
- novice4eva
- Forum Contributor
- Posts: 327
- Joined: Thu Mar 29, 2007 3:48 am
- Location: Nepal
Re: crop image before inserting into mysql
Hummm that's strange, i had tried that code in my localhost and it worked perfectly well!! Maybe the problem lies somewhere else...could you enable the error with these code
and see where the culprit lies
Code: Select all
ini_set('display_errors', 1);
ini_set('error_reporting', E_ALL);
Re: crop image before inserting into mysql
I am not getting any error but the image is not stored
- novice4eva
- Forum Contributor
- Posts: 327
- Joined: Thu Mar 29, 2007 3:48 am
- Location: Nepal
Re: crop image before inserting into mysql
freaking image hahaha ok i am outa tricks!! try echoing the contents of the output buffer, remember to remove the headers while doing so.
and check for the magic quote (get_magic_quotes_gpc)too, i think it might be the culprit ...humm if it is off try addslashes() with the $content......
Code: Select all
ob_start();
imagejpeg($thumb, NULL, 80);//IF SECOND PARAMETER SET TO NULL, OUTPUTS TO THE BROWSER
$contents = ob_get_contents();//SAVE $content IN DATABASE
ob_end_clean();
echo $content;
Re: crop image before inserting into mysql
thanks everybody
mainly novice...now the code is working it was some problem with the apache..i have re installed apache and its working....THANKS A LOT ONCE AGAIN
mainly novice...now the code is working it was some problem with the apache..i have re installed apache and its working....THANKS A LOT ONCE AGAIN