Been searching for days on how to do this.

It's irritating the crap out of me.

Eventually I just decided it was easier to re-upload the files and create the thumbnails at the same time from $_userfile instead of querying the database and making thumbnails from the BLOB image.

I'd still like to know how to do it, though.

    It is easier and cheaper to create and store the thumbnails when the original is stored.

    But one elegant solution is to write a [man]stream[/man] wrapper that turns a custom-designed URL for the image into a database request, collects the BLOB data and returns it - and similarly for saving. Then GD Lib can't tell the difference between a BLOB and an ordinary file.

      Alternatively, you could use [man]imagecreatefromstring/man after you retrieve the BLOB data from the DB. That would return an image resource identifier just as if you had opened the image from a file.

        You certainly could do that; it it would be quite a bit simpler than writing a stream wrapper.

        I got it into my head that the thumbnails were to be stored as BLOBs as well (to save regenerating them all every time they were displayed). For that you'd need a way to save the image data as a BLOB, too.

        That could be done by using output buffering to capture what imagejpeg() outputs instead of directing it to a file.

        But between that and imagecreatefromstring, it just looked a lopsided. Hence the "elegant" adjective. It's also good for encapsulation because exactly the same thumbnail-generation code would work for BLOBs and file-based images. 🙂

          Weedpacket;10911504 wrote:

          It is easier and cheaper to create and store the thumbnails when the original is stored.

          But one elegant solution is to write a [man]stream[/man] wrapper that turns a custom-designed URL for the image into a database request, collects the BLOB data and returns it - and similarly for saving. Then GD Lib can't tell the difference between a BLOB and an ordinary file.

          I started writing a response to this last night, but firefox decided to fail on me.

          :[

          I actually tried using this method, and the thumbnail images always appeared broken.

          I haven't had much experience with the GD Library, so it could be that I just didn't know what I was doing. ;

          I also used imagecreatefromstring and imagecreatefromjpeg, both items failed.

          The solution I'm using now.. is to just re-upload each file to a script that only makes the thumbnails.

          It's a pain in the ass though.

          Oh yeah,
          thanks for your replies.

            bradgrafelman;10911509 wrote:

            Alternatively, you could use [man]imagecreatefromstring/man after you retrieve the BLOB data from the DB. That would return an image resource identifier just as if you had opened the image from a file.

            Kept returning a blank file.

            Then I did some debugging, and found that the imagecreatefromstring() was returning "mysql resource #4" instead of actual image code.

              Spine Design;10911569 wrote:

              Then I did some debugging, and found that the imagecreatefromstring() was returning "mysql resource #4" instead of actual image code.

              Then apparently you didn't use [man]mysql_result/man or one of the mysql_fetch_*() commands to actually retrieve the BLOB data.

                bradgrafelman;10911573 wrote:

                Then apparently you didn't use [man]mysql_result/man or one of the mysql_fetch_*() commands to actually retrieve the BLOB data.

                I don't have the code I was using anymore, but this is one of the backups I made:

                <?php
                	ini_set("memory_limit","-1");
                
                // DB Connection
                require_once('includes/db.php');
                
                $bhid = $_REQUEST['bhid'];
                $q = "SELECT img 
                      FROM catalog 
                      WHERE bh_id = '$bhid'
                	  LIMIT 1";
                
                $result = mysql_query($q,$conn);
                if(mysql_num_rows($result)==1){
                	//header('Content-Type: image/jpeg');
                	$row = mysql_fetch_assoc($result);
                	$mysql_image = $row['img'];
                
                	$im = imagecreatefromstring($mysql_image);
                
                	//This will set our output to 45% of the original size
                	$size = 0.45;
                
                	// Setting the resize parameters
                	list($width, $height) = getimagesize($im);
                	echo $width; echo $height;
                	$modwidth = $width * $size;
                	$modheight = $height * $size;
                
                	// creating thumbnail
                	$tn = imagecreatetruecolor($modwidth, $modheight);
                	$source = imagecreatefromjpeg($im);
                
                	// Resizing image to thumbnail
                	imagecopyresized($tn, $source, 0, 0, 0, 0, $modwidth, $modheight, $width, $height);
                
                	// output jpeg
                	imagejpeg($tn); 
                } else {
                	echo "no result";
                }
                
                ?>
                

                This isn't complete, not sure if it would even work.

                          list($width, $height) = getimagesize($im); 

                  That will fail: [man]getimagesize[/man] expects a filename, not a GD resource. To get the width and height from one of those you need the [man]imagesx[/man] and [man]imagesy[/man] functions.

                    4 months later

                    could any one clarify above statements???i am new to php and i want to show the uploaded images such that when i click'em, a small thumbnail of the same is generated(which is created in real-time) which on click shows the full sized version of the image
                    thanks in advance

                      6 years later
                      aruns4u;10924450 wrote:

                      could any one clarify above statements???i am new to php and i want to show the uploaded images such that when i click'em, a small thumbnail of the same is generated(which is created in real-time) which on click shows the full sized version of the image
                      thanks in advance

                      I use this because my host provider has a file count limit and i like to keep the numbers down.

                      <?php
                      
                      // //WORKING EXAMPLE HERE: [url]http://tmgraphics.biz/test/base64TEST/[/url]
                      
                      /*
                      basically i am going to create a DB with 4 columns
                      - location of image
                      - date pof image
                      - blob thumb of image sm
                      - extra field for alt sizes
                      then i will have a sub routine that calls on a thumb based on the image full url location
                      sunroutine checks the date modified and either create, recreates or serves the base 64 data from the DB
                      */
                      
                      // //db pass log in once
                      
                      mysql_connect("localhost", "USERNAME", "PASS") or die(mysql_error());
                      mysql_select_db("DATABASE") or die(mysql_error());
                      
                      function random_string($length)
                      	{
                      	$key = '';
                      	$keys = array_merge(range(0, 9) , range('a', 'z'));
                      	for ($i = 0; $i < $length; $i++)
                      		{
                      		$key.= $keys[array_rand($keys) ];
                      		}
                      
                      return $key;
                      }
                      
                      // /////START thumbDB FUNCTION
                      
                      function dbthumb($loc, $sz)
                      	{
                      	$fullpath = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['SCRIPT_NAME'];
                      	$fullpath = explode(basename($_SERVER['SCRIPT_NAME']) , $fullpath);
                      	$fullpath = $fullpath[0] . $loc;
                      	$create_new = false;
                      	$curr_mod_date = filemtime($loc);
                      	$curr_mod_date = date("l dS \of F Y h:i:s A", $curr_mod_date);
                      
                      // //checks exist thumb in DB base on location
                      
                      $result = mysql_query("SELECT loc, mod_date,  basedata, addinfo FROM thumbDB WHERE `loc` = '$fullpath'");
                      if (!$result)
                      	{
                      	echo 'Could not run query: ' . mysql_error();
                      	exit;
                      	}
                      
                      $row = mysql_fetch_row($result);
                      if ($row != "")
                      	{
                      	$base64 = $row[2];
                      
                      	// /thumb as base64
                      
                      	}
                        else
                      	{
                      	$create_new = true;
                      	}
                      
                      if ($row[1] != $curr_mod_date || $create_new = true)
                      	{
                      
                      	// // create or recreate thumb  //imagick
                      
                      	$output = "thumb_" . random_string(13) . ".png";
                      
                      	// random string can be any number
                      
                      	exec("convert -thumbnail x300 $loc $output");
                      	$imagedata = file_get_contents($output);
                      	$base64 = base64_encode($imagedata);
                      
                      	// /store in db
                      
                      	unlink($output);
                      	$addinfo = "";
                      	mysql_query("INSERT INTO thumbDB (loc, mod_date,  basedata, addinfo) VALUES ('$fullpath', '$curr_mod_date', '$base64', '$addinfo' ) ON DUPLICATE KEY UPDATE
                       loc = '$fullpath', basedata = '$base64', mod_date = '$curr_mod_date', addinfo = '$addinfo'") or die(mysql_error());
                      		}
                      
                      // // serves thumb as base 64
                      
                      return "<img  src=\"data:image/jpg;base64," . $base64 . "\" " . $sz . "  >";
                      }
                      
                      // /////END thumbDB FUNCTION
                      // ///USAGE
                      
                      $loc = "brand.jpg";
                      $sz = "height = \"100\"";
                      
                      // height = \"100\" width = \"100\"  ///this can be anything dealing with size /// this is just display size / and can range from 10-300 max ///thue thumb is created at 300
                      
                      echo "<a href = \"" . $loc . "\">" . dbthumb($loc, $sz) . "</a>";
                      ?>

                      ...your welcome

                        Write a Reply...