Page 1 of 1

crop image before inserting into mysql

Posted: Thu Nov 20, 2008 11:10 pm
by anujphp
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

Re: crop image before inserting into mysql

Posted: Thu Nov 20, 2008 11:58 pm
by novice4eva
I think you would want to resize it :dubious: , 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 :mrgreen:

Re: crop image before inserting into mysql

Posted: Fri Nov 21, 2008 5:06 pm
by anujphp
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

Re: crop image before inserting into mysql

Posted: Sat Nov 22, 2008 10:40 pm
by novice4eva
You image is already in $thumb variable.

Code: Select all

imagejpeg($thumb, "images/thumbnail.jpg", 80);
you can do a simple insert:

Code: Select all

 
$query = "INSERT INTO tbl_images(image) VALUES ('$thumb')";
 
and as for inserting into directory, you must have already achieved it.

Re: crop image before inserting into mysql

Posted: Sun Nov 23, 2008 3:41 am
by anujphp
i followed your suggestion but it does not work...it saves resource id #5 in the table instead of photo

Re: crop image before inserting into mysql

Posted: Sun Nov 23, 2008 4:47 am
by novice4eva
Can we see your code.

Re: crop image before inserting into mysql

Posted: Sun Nov 23, 2008 7:26 pm
by anujphp
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.";



?>

Re: crop image before inserting into mysql

Posted: Sun Nov 23, 2008 10:23 pm
by novice4eva
Hummm that got me wondering too :wink: . Anyways now i have a solution.

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();
 
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).
:drunk:

Re: crop image before inserting into mysql

Posted: Sat Nov 29, 2008 2:38 am
by anujphp
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....

Re: crop image before inserting into mysql

Posted: Sat Nov 29, 2008 10:46 pm
by novice4eva
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

Code: Select all

 
    ini_set('display_errors', 1);
    ini_set('error_reporting', E_ALL);
 
and see where the culprit lies

Re: crop image before inserting into mysql

Posted: Mon Dec 08, 2008 2:28 am
by anujphp
I am not getting any error but the image is not stored

Re: crop image before inserting into mysql

Posted: Tue Dec 09, 2008 7:11 am
by novice4eva
freaking image hahaha ok i am outa tricks!! try echoing the contents of the output buffer, remember to remove the headers while doing so.

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;
 
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......

Re: crop image before inserting into mysql

Posted: Sun Dec 14, 2008 11:49 pm
by anujphp
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