Thread: Storing Binary
View Single Post
  #2 (permalink)  
Old 06-27-2006, 11:06 AM
Void's Avatar   
Void Void is offline
Programming Expert
 
Join Date: Jun 2006
Posts: 411
Credits: 8
Rep Power: 11
Void is on a distinguished road
Default Adding pictures to database (binary)

NeedHelp, I've done this before. The table needs to be a blob.
This is the code to insert into DB

add.php (Where the user inserts data to be submitted and browses for pictures)
HTML Code:
<form enctype="multipart/form-data" action="insert.php" method="POST" name=changer>
<input type="hidden" name="MAX_FILE_SIZE" value="102400">
<input type="file" name="photo" accept="image/jpeg">
<input type="Submit" value="Submit">
insert.php file
PHP Code:
<?php
// Connect to the DB somwhere up here

// Read our photo
$sPic="";
if (!empty(
$_FILES['photo']['name'])) {
    
$sPic addslashes(FormatImage($_FILES['photo']['name'], $_FILES['photo']['tmp_name']));
}

$query "INSERT INTO name (tb_name) VALUES ('$spic')"
$results mysql_query($query) or die ("Error " mysql_error());
mysql_close();

// This function formats the image and can resize if desired
function FormatImage($sPhotoFileName$sTempFileName) {
    
//$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 102400)
        {    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($oTempFilefileSize($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."
);
    }
    
    
$nWidth imagesx($oSourceImage); // get original source image width
    
$nHeight imagesy($oSourceImage); // and height
    
    // create small thumbnail
    
$nDestinationWidth $nWidth;
    
$nDestinationHeight $nHeight;
    
//$oDestinationImage = imagecreatetruecolor($nDestinationWidth, $nDestinationHeight);
    
$oDestinationImage imagecreate($nDestinationWidth$nDestinationHeight);
    
    
// below resizes the image
    
imagecopyresized(
            
$oDestinationImage$oSourceImage,
            
0000,
            
$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.
        
        // Return our image
        
return $sBinaryThumbnail;
}

?>
__________________
Void
Reply With Quote