Lost Password?


Go Back   CodeCall Programming Forum > Web Development Forum > Database & Database Programming

Database & Database Programming MySQL, Oracle, SQL, PL/SQL, ABAP, Smart Forms, and other databases and languages. A database is an organized body of related information used in many websites (including CC).

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-26-2006, 08:49 PM
NeedHelp NeedHelp is offline
Programming God
 
Join Date: May 2006
Posts: 527
Rep Power: 13
NeedHelp is on a distinguished road
Default Storing Binary

I have to create a program that stores images inside of a MySQL DB. I will be using PHP for this. I need to know how I can insert images as binary into MySQL and then display them again. I can't figure out how to do it using MySQL. I think the data is in there but then it doesn't display. I only see a broken link. Can anyone point me in the right direction?
__________________
I Need Help
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 06-27-2006, 11:06 AM
Void's Avatar   
Void Void is offline
Programming Expert
 
Join Date: Jun 2006
Posts: 411
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
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 06-27-2006, 11:13 AM
Void's Avatar   
Void Void is offline
Programming Expert
 
Join Date: Jun 2006
Posts: 411
Rep Power: 11
Void is on a distinguished road
Default Code to View image from a Database

This is the code to view an image that has been stored in a database. You have to use a seperate php and call it like an image.

Here is the HTML where it calls the php image
HTML Code:
<img src="viewpicture.php?id=<? print "$id"; ?>" />
^^ This can be stuck in any HTML page.

You can remove the ID from this if you only want to select the one image or whatever. Here is the viewpicture

viewpicture.php
PHP Code:
<?php
// Connect to DB up here

// Select our picture and display it
$query "SELECT * FROM db WHERE ID='$id'";

// Run our query and display the image
$results mysql_query($query);
$row mysql_fetch_array($results);
$sJpg $row[##] // Replace ## with the row number

// Send header info
header("Content-type: image/jpeg"); // act as a jpg file to browser

// Show it
print $sJpg;

?>
__________________
Void
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 06-27-2006, 05:09 PM
Jordan's Avatar   
Jordan Jordan is offline
Administrator
 
Join Date: Nov 2005
Location: Hendersonville, NC
Posts: 9,224
Last Blog:
Ext JS or Ext GWT
Rep Power: 20
Jordan is just really niceJordan is just really niceJordan is just really niceJordan is just really nice
Send a message via ICQ to Jordan Send a message via AIM to Jordan Send a message via MSN to Jordan
Default

Nice post void! I'm copying this to the PHP section as well.
__________________
CodeCall Blog | CodeCall Wiki | Shareware Site | Linux Forum | Write a Blog
The CodeCall Wiki is now fully integrated with vBulletin users! Check it out and add some new pages!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing to binary file paul. Pascal/Delphi 4 04-13-2007 09:23 PM
Binary, Decimal, Hex, the Manual way!! TcM Tutorials 22 01-10-2007 01:06 AM
Binary? NeedHelp Managed C++ 4 07-27-2006 03:59 PM
Storing Binary NeedHelp PHP Forum 5 07-05-2006 10:47 PM
Binary Conversion in VB roger Visual Basic Programming 0 06-01-2006 11:50 AM


All times are GMT -5. The time now is 12:48 PM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 98%

Ads