Before I clarify, I have gone through everything regarding this issue both on this site, and php.net it self and others...

What I want to do:
After taking the image contents from mysql, I want to then create a temporary file (if needed) before resizing the current image to 150 x or x 150 or something.

So to recap:
1. Get the image contents
2. Create Temp Image (If needed)
3. Resize image from mysql
4. Delete temp image (if needed)
5. Output image

I need it to also accomadate all image types (jpg,jpeg,gif,bmp,png).

I just don't want the user to download the whole image from mysql unless they want to view it, instead the server side will do all work and spit out the images as actual thumbnails not just resized images which would need to be fully downloaded.

Thanks,

-Phil

    Okay, first get the value into a variable. Then, use fopen() and fputs() to put text to the file:
    $tn = fopen("temp","w+b");
    fputs($tn,$imagedata);
    fclose($tn);

    From here, you can either use GD or ImageMagick. Since you want to use GIFs, you'll have to use the latter (GD no longer has GIF support). Since I don't know how to use ImageMagick, you'll have to look that up. I know I've seen someone post the code to resize images using it.

      I've tried this:

      $sConn = mysql_connect($dbServer, $dbUser, $dbPass)
        or die("Couldn't connect to database server");
      
      $dConn = mysql_select_db($dbDatabase, $sConn)
        or die("Couldn't connect to database $dbDatabase");
      
      function RandChars( $passwordLength ) {
      
       $passwordChars = '0123456789'
                       .'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
                       .'abcdefghijklmnopqrstuvwxyz';
      
       $password = "";
      
       for ($index = 1; $index <= $passwordLength; $index++) {
      
         // pick random number
         $randomNumber = rand(1,strlen($passwordChars));
      
         $password .= substr($passwordChars,$randomNumber-1,1);
      
      
       }
       return $password;
      
      }
      // Database connection variables
      
      $dbQuery  = "SELECT blobType, blobData ";
      $dbQuery .= "FROM $user_name ";
      $dbQuery .= "WHERE blobFileName = '$filename'";
      
      $result = mysql_query($dbQuery) or message_die(GENERAL_ERROR, "Couldn't get file list.  ".mysql_error());
      
      $data = @mysql_result($result, 0, "blobData");
      $type = @mysql_result($result, 0, "blobType");
      
      $exten = substr($filename, -3);
      if ($exten != "jpg" AND $exten != "gif" AND $exten != "png" AND $exten != "bmp") {
      $exten = substr($filename, -4);
      }
      $randname =  RandChars('6');
      $imagename = './~tmp/'.$randname.".".$exten;
      $handle = fopen($imagename, 'w+');
      fwrite($handle, $data);
      fclose($handle);
      exec('mogrify -size 120x120 '.$imagename.' -resize 120x120 +profile "*"'); 
      $resizedImage = fopen($imagename, "r");
      $resizedImage = fread($resizedImage, filesize($imagename));
      unlink($imagename);
      header("Content-type: $type");
      echo $resizedImage;
      

      Whats wrong with it?? It does absolutely nothing lol, it loads the image from mysql perfectly, makes a files perfectly, but the MOGRIFY does not work (i've also tried the usr/bin/mogrify)..so it just outputs the image as it would normally without thumbnailing it...

        serving binaries from blobs works great in theory, but in practice it is unadvisable.

        unless you can figure out how to stream and capture the binaries from mysql, you will have to write the thumbnail to the server each time it's accessed, then direct the browser to that file. this takes alot of server resources! picture your hard drive churning butter all day.

        ideally, you should use the old-fashioned method of keeping the images, and their thumbnails on in a directory and allowing your server software to handle them. that way caching can be used, and your hard drive won't be as busy.

        i suggest you:
        1. have the user upload the image (provided that is the source)
        2. make a thumbnail of that image using server-side 3rd party softwre
        3. copy the images to a permanent folder (and possibly protect them using .htaccess or the like)
        4. give the user the url of the images

        one common reason for using blobs is to protect images from being accessed by unauthorized clients. if this i the case, there are many other safe and fast alternatives.

          I didn't ask to be lectured on the reasons i shouldn't use mysql for binaries lol.
          I run a free hosting service on another site, which takes advantage of my near unlimited mysql space.
          So far I have had NO problems with hosting files in the mysql, images download and view perfectly, and other files download perfect. Anyways to make a long story short...my problem isn't with mysql, its with MOGRIFY not doing what I told it too, I've generated an image into a tmp dir, and i want to scale it then get it back into php, delete the image and show the contents.

          Thanks,

          -Phil

            Write a Reply...