Jump to content

Load DB image to GD


mentalist

Recommended Posts

My main issue is how to read image data from a database into GD using imagecreatefromstring()or similar.

 

 

 

I have an upload mechanism in place which stores images (and files) into a database entry, and allows viewing (not shown).

 

1. File upload handling and DB storage function:

function do_upload($db,$tag,$uid,$plugin,$path,$redirect=true,$fn=""){
if(isset($_POST[$tag]) && $_FILES['userfile']['size'] > 0){
// GET UPLOAD INFO
$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];
// READ TEMP FILE
$fp = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);
// DETAILS
if($fn!=""){ $fileName=$fn; }
if(!get_magic_quotes_gpc()){ $fileName = addslashes($fileName); }
$fullpath=$plugin."/".$path."/".$fileName;
$ref=md5($uid.$fileName.time());

// STORE TO DB
$tn=$db->prefix."datastore"; // id,uid,plugin,ref,name,path,fullpath,type,size,data,visible,status,force
$a=$db->table_get($tn,"fullpath='".$fullpath."'");
if(count($a)>0){
// UPDATE ENTRY
$db->table_update($tn,"type='".$fileType."', size='".$fileSize."', data='".$content."'","id=".$a[0]['id']);
}else{
// CREATE ENTRY
$db->table_insert($tn, "'',".$uid.",'".$plugin."','".$ref."','".$fileName."', '".$path."', '".$fullpath."', '".$fileType."', '".$fileSize."', '".$content."',1,1,0");
}

if($redirect){
// REDIRECT TO CLEAR
header("Location: ".rx_path_self());
exit();
}
}
}

 

Next I try to feed the image data into GD:

 

$a=$db->table_get($tn,"fullpath='tmp/tmp/img.png'");
if(count($a)>0){
$e=$a[0];
$img2 = imagecreatefromstring(stripslashes($e['data']));

 

I've tried it with and without stripslashes, decode64, etc...

 

How to read stored data into GD?

Link to comment
Share on other sites

Are you sure your path information is returned as you expect it?  Also, whenever I need to generate an image file, I'm using something like this:

if ($fext=="jpg" || $fext=="jpeg") {
  	$grfx_obj = imagecreatefromjpeg($fpath);
 } elseif ($fext=="png") {
  	$grfx_obj = imagecreatefrompng($fpath);
  	imagesavealpha($grfx_obj, true); //preserve background transparency
  	imagealphablending($grfx_obj, false); //don't blend grapic pixels with background
 } else {
  	throw new Exception('GD imagecreate* failed.');
 }

I've used imagecreatefromstring before but I recall it was buggy or there was some obscure quark, where fromjpeg or frompng worked better for my situation.

Link to comment
Share on other sites

Yes.  I see you are reading the file into $content and then storing the binary in the DB.  Couple things, is the data type for the "data" field "blob"?  It may be that the binary is getting corrupted or cut off in the process.  Also, there are file size limitations with 2M being the default size for most distros.  Are your files over that limit?

 

I find it much simpler (and more common) to move the temporary file to a permanent directory and in the process give the file a programmatic name.  Then in my database, I'll store the file path, original file name and update the record with the programmatic name and file extension.   

Edited by rwhite35
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.