anujphp Posted November 21, 2008 Share Posted November 21, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/133616-solved-crop-image-before-inserting-into-mysql/ Share on other sites More sharing options...
Loldongs Posted November 21, 2008 Share Posted November 21, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/133616-solved-crop-image-before-inserting-into-mysql/#findComment-695721 Share on other sites More sharing options...
genericnumber1 Posted November 21, 2008 Share Posted November 21, 2008 I'd suggest just saving the image in a directory and linking it to an entry in the database... storing images in a database is just messy. Quote Link to comment https://forums.phpfreaks.com/topic/133616-solved-crop-image-before-inserting-into-mysql/#findComment-695730 Share on other sites More sharing options...
anujphp Posted November 21, 2008 Author Share Posted November 21, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/133616-solved-crop-image-before-inserting-into-mysql/#findComment-695790 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.