Re: insert/retrieve image into database
| From: | Jeroen Olthof | Date: | Fri, 18 Aug 2000 01:05:12 +0000 |
| Subject: | Re: insert/retrieve image into database | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-12386@lists.php.net to get a copy of this message | ||
Currently I'm working on a site that have to handle a lot of images uploaded
by users.
so I did some testing.
carefully consider storing images data in a db
if the db table where images are store becomes large it could brak down the
speed of your
query's where te images data fields are part of . (like SELECT * FROM
Images_tbl WHERE id = '8683')
and ofcours you db will just grow fast
I choose for writing the images to a file with the php fopen/fwrite
functions making te name of the fill like
timestamp_id (timestamp is for easy selecting old files (like
20000818130240_12.jpg)
if u use the <input type="file" name="Picture_data" ..... form object
make sure your form tags looks like
<form name="test" action="some_url"
enctype="multipart/form-data">
te get the right data from the picture to add it in the db or file is
$Data = fread(fopen($Picture_data,"r"), filesize($Picture_data));
the following fuction below will returns a array with info about the picture
(works with , jpg,gif and png)
$Picture_info = elqGetImageFormat($Data);
here is the elqGetImageFormat.php file I found in this newsgroups a wile ago
<?php
/* Function: array elqGetImageFormat(string $dataFromImageFile)
elqGetImageFormat is a php3 script with functionality similar to that
of php3's internal function GetImageSize(string $fileName).
The differences are as follows:
- elqGetImageFormat takes the data from an image file as argument
instead of the file name. Ie. you can use the function on
data allready read from an image file, imagedata read from
a blob field in a database, or similar.
- elqGetImageFormat does NOT retrieve any JPEG/JFIF APP marker info,
thus a call to
elqGetImageData(string $dataFromImageFile, array [imageinfo])
will generate an error.
For further documentation on use of this function, consult your
regular php3 reference on the GetImageSize function.
Copyright notice:
"This piece of code is copyright Oyvind Hallsteinsen, 2000.
The code is to be seen as public domain, use it for whatever
it's worth. The code is provided "as is", and comes with
NO WARRANTY whatsoever.
Please let this note follow your modified versions."
Code originated from: http://www.elq.org
- Oyvind Hallsteinsen aka Gosub / ELq - gosub@elq.org
*/
/* Defining the JFIF markers we'll be using */
define(M_SOF0, "\xc0"); /* Start Of Frame N */
define(M_SOF1, "\xc1"); /* N indicates which
compression process */
define(M_SOF2, "\xc2"); /* Only SOF0-SOF2 are now in
common use */
define(M_SOF3, "\xc3");
define(M_SOF5, "\xc5"); /* NB: codes C4 and CC are NOT
SOF markers */
define(M_SOF6, "\xc6");
define(M_SOF7, "\xc7");
define(M_SOF9, "\xc9");
define(M_SOF10, "\xca");
define(M_SOF11, "\xcb");
define(M_SOF13, "\xcd");
define(M_SOF14, "\xce");
define(M_SOF15, "\xcf");
define(M_EOI, "\xd9"); /* End Of Image (end of
datastream) */
define(M_SOS, "\xda"); /* Start Of Scan - image data start
*/
function elqRead1Byte(&$imgData, &$imgPos) { /* Get 1 byte from data */
$c = ord($imgData[$imgPos++]);
return $c;
}
function elqRead2Bytes(&$imgData, &$imgPos) { /* Get 2 bytes from data */
$c1 = (ord($imgData[$imgPos++])<<8);
$c2 = ord($imgData[$imgPos++]);
return ($c1 + $c2);
}
function elqNextMarker(&$imgData, &$imgPos) { /* Find next marker segment
and return marker code */
$c = elqRead1Byte($imgData, $imgPos);
while ($c != 255) {
$c = elqRead1Byte($imgData, $imgPos);
}
do {
$c = elqRead1Byte($imgData, $imgPos);
} while ($c == 255);
return $c;
}
function elqProcessSof(&$imgData, &$imgPos) { /* Grab width/height from SOF
segment */
$imgPos += 3;
$imgInfo[1] = elqRead2Bytes($imgData, $imgPos);
$imgInfo[0] = elqRead2Bytes($imgData, $imgPos);
return $imgInfo;
}
function elqSkipSegment(&$imgData, &$imgPos) { /* Skip entire marker
segment (uninteresting) */
$length = elqRead2Bytes($imgData, &$imgPos);
$length -= 2;
$imgPos += $length;
}
function elqJpegInfo(&$imgData) {
$imgPos = 2;
while($imgPos < strlen($imgData)) { /* Go through data and make sure
loop exits even if jfif is corrupt */
$marker = elqNextMarker($imgData, $imgPos);
switch (chr($marker)) {
case M_SOF0:
case M_SOF1:
case M_SOF2:
case M_SOF3:
case M_SOF5:
case M_SOF6:
case M_SOF7:
case M_SOF9:
case M_SOF10:
case M_SOF11:
case M_SOF12:
case M_SOF13:
case M_SOF14:
case M_SOF15:
$imgInfo = elqProcessSof($imgData, $imgPos);
return $imgInfo;
case M_EOI:
case M_SOS:
return FALSE; /* End loop in case we find one of these markers
*/
default:
elqSkipSegment($imgData, $imgPos); /* We're not interested in other
markers */
break;
}
}
}
function elqGenSizeArg($width, $height) { /* Generate Width/height args for
img tag */
return "width=\"" . $width . "\" height=\"" . $height .
"\"";
}
function elqGetImageFormat(&$imgData) { /* Determine file type,
extract/generate info */
file://bin
$gif_sig = "\x47\x49\x46";
$jpeg_sig = "\xff\xd8\xff";
$png_sig = "\x89\x50\x4e\x47\x0d\x0a\x1a\x0a";
file://hex
$gif_sigh = bin2hex($gif_sig);
$jpeg_sigh = bin2hex($jpeg_sig);
$png_sigh = bin2hex($png_sig);
if(strcmp($gif_sig, substr($imgData, 0, 3)) == 0) {
$imgInfo[0] = ord($imgData[6]) + (ord($imgData[7])<<8);
$imgInfo[1] = ord($imgData[8]) + (ord($imgData[9])<<8);
$imgInfo[2] = "gif";
} else if(strcmp($jpeg_sig, substr($imgData, 0, 3)) == 0) {
$imgInfo = elqJpegInfo($imgData);
if($imgInfo) $imgInfo[2] = "jpg";
else $imgInfo = FALSE;
} else if(strcmp($png_sig, substr($imgData, 0, 8)) == 0) {
$imgInfo[0] = (ord($imgData[16])<<24) + (ord($imgData[17])<<16) +
(ord($imgData[18])<<8) + ord($imgData[19]);
$imgInfo[1] = (ord($imgData[20])<<24) + (ord($imgData[21])<<16) +
(ord($imgData[22])<<8) + ord($imgData[23]);
$imgInfo[2] = "png";
} elseif(strcmp($gif_sigh, substr($imgData, 0, 3)) == 0) {
$imgInfo[0] = ord($imgData[6]) + (ord($imgData[7])<<8);
$imgInfo[1] = ord($imgData[8]) + (ord($imgData[9])<<8);
$imgInfo[2] = "gif";
} else if(strcmp($jpeg_sigh, substr($imgData, 0, 3)) == 0) {
$imgInfo = elqJpegInfo($imgData);
if($imgInfo) $imgInfo[2] = "jpg";
else $imgInfo = FALSE;
} else if(strcmp($png_sigh, substr($imgData, 0, 8)) == 0) {
$imgInfo[0] = (ord($imgData[16])<<24) + (ord($imgData[17])<<16) +
(ord($imgData[18])<<8) + ord($imgData[19]);
$imgInfo[1] = (ord($imgData[20])<<24) + (ord($imgData[21])<<16) +
(ord($imgData[22])<<8) + ord($imgData[23]);
$imgInfo[2] = "png";
} else $imgInfo = FALSE;
if($imgInfo) { /* In case everything went well, generate
width/height img argument */
$imgInfo[3] = elqGenSizeArg($imgInfo[0], $imgInfo[1]);
}
return $imgInfo;
}
?>
good luck
kind regards
Jeroen Olthof
From holland
> hi,
>
> i am having some slight problem with inserting binary stuff into my
> database via odbc function calls... i am trying to store a bunch of
> images, and i cannot figure out how to make Php return a variable with all
> the information in it such that i can store into a database using INSERT.
>
> essentially, the process is that a user would upload an image, and the
> image goes into the database to be retrieved later when necessary...
>
> can someone point me to where i can look for this? i'm completely baffled
> and out of ideas... :P
>
> ----------------
> remco chang
> bountyquest.com
>
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net
>