mentalist Posted June 17, 2015 Share Posted June 17, 2015 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? Quote Link to comment https://forums.phpfreaks.com/topic/296885-load-db-image-to-gd/ Share on other sites More sharing options...
rwhite35 Posted June 17, 2015 Share Posted June 17, 2015 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. Quote Link to comment https://forums.phpfreaks.com/topic/296885-load-db-image-to-gd/#findComment-1514208 Share on other sites More sharing options...
mentalist Posted June 17, 2015 Author Share Posted June 17, 2015 You seem to be loading the files using a filename and not with saved data? Quote Link to comment https://forums.phpfreaks.com/topic/296885-load-db-image-to-gd/#findComment-1514210 Share on other sites More sharing options...
rwhite35 Posted June 17, 2015 Share Posted June 17, 2015 (edited) 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 June 17, 2015 by rwhite35 Quote Link to comment https://forums.phpfreaks.com/topic/296885-load-db-image-to-gd/#findComment-1514213 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.