Hi Friends,
I want to upload a image file to my mysql database. But before that i want to resize it. I have used the following code. But what happens is that the image is successfully resized but the quality is not retained. Please someone guide me with some help.
The code goes here:
The php part
<?php
//***********************************************************************************************************
////////////////-------Photo upload, validation at server-------//////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
$sPhotoFileName = $_FILES['photo']['name']; // get client side file name
if ($sPhotoFileName) // file uploaded
{ $aFileNameParts = explode(".", $sPhotoFileName);
$sFileExtension = end($aFileNameParts); // part behind last dot
if ($sFileExtension != "jpg"
&& $sFileExtension != "JPEG"
&& $sFileExtension != "JPG")
{ die ("Choose a JPG for the photo");
}
$nPhotoSize = $_FILES['photo']['size']; // size of uploaded file
if ($nPhotoSize == 0)
{ die ("Sorry. The upload of $sPhotoFileName has failed.
Search a photo smaller than 100K, using the button.");
}
if ($nPhotoSize > 10240000000)
{ die ("Sorry.
The file $sPhotoFileName is larger than 100K.
Advice: reduce the photo using a drawing tool.");
}
// read photo
$sTempFileName = $_FILES['photo']['tmp_name']; // temporary file at server side
$oTempFile = fopen($sTempFileName, "r");
$sBinaryPhoto = fread($oTempFile, fileSize($sTempFileName));
// Try to read image
$nOldErrorReporting = error_reporting(E_ALL & ~(E_WARNING)); // ingore warnings
$oSourceImage = imagecreatefromstring($sBinaryPhoto); // try to create image
error_reporting($nOldErrorReporting);
if (!$oSourceImage) // error, image is not a valid jpg
{ die ("Sorry.
It was not possible to read photo $sPhotoFileName.
Choose another photo in JPG format.");
}
}
//////////////////////////////////////////////////////////////////////////////////////////
//////////////-------Photo upload, validation at server ends---/////////////////////////////
//***********************************************************************************************************
////////////////----------Create thumbnail--------------//////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
$nWidth = imagesx($oSourceImage); // get original source image width
$nHeight = imagesy($oSourceImage); // and height
// create small thumbnail
$nDestinationWidth = 80;
$nDestinationHeight = 60;
//$oDestinationImage = imagecreatetruecolor($nDestinationWidth, $nDestinationHeight);
$oDestinationImage = imagecreate($nDestinationWidth, $nDestinationHeight);
/*$oResult = imagecopyresampled(
$oDestinationImage, $oSourceImage,
0, 0, 0, 0,
$nDestinationWidth, $nDestinationHeight,
$nWidth, $nHeight); // resize the image
*/
imagecopyresized($oDestinationImage, $oSourceImage,0, 0, 0, 0,$nDestinationWidth, $nDestinationHeight,$nWidth, $nHeight); // resize the image
ob_start(); // Start capturing stdout.
imageJPEG($oDestinationImage); // As though output to browser.
$sBinaryThumbnail = ob_get_contents(); // the raw jpeg image data.
ob_end_clean(); // Dump the stdout so it does not screw other output.
//***********************************************************************************************************
////////////////------Store Thumbnail in database------//////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////
if($_POST['send'])
{
$sBinaryThumbnail = addslashes($sBinaryThumbnail);
$oDatabase = mysql_connect("localhost", "root", "karma");
mysql_select_db("upload", $oDatabase);
$sQuery = "insert into image (thumbnail) values ('$sBinaryThumbnail')";
echo $sQuery;
mysql_query($sQuery, $oDatabase);
}
?>
The html form
<form enctype="multipart/form-data" method="post" action="" name="form1">
<input type="hidden" name="MAX_FILE_SIZE" value="102400">
<input type="file" name="photo">
<input type="hidden" name="id" value="12345">
<input type=submit value="Send" name=send>
</form>
Here is the code where i have displayed the uploaded image. the quality becomes bad here.
<?
$oDatabase = mysql_connect("localhost", "root", "karma");
mysql_select_db("upload", $oDatabase);
$s="select thumbnail from image";
$r=mysql_query($s);
while($p=mysql_fetch_array($r))
{
$bytes = $p[0];
if($bytes == null)
{
$instr = fopen("images/d_silhouette.gif","rb");
$bytes = fread($instr,filesize("images/d_silhouette.gif"));
}
$data = base64_encode($bytes);
echo '<img src="data:image/jpeg;base64,', $data, '" border=0 />';
}
?>
Waiting for reply...