So I've got everything set for blobs in MySQL. I have a script to serve up the image when it's called within an <img> tag. It works perfectly.

However, that's not enough. I want to be able to edit the image.

Currently, I have my script loadpic.php that grabs an image blob from the database and displays it. I want to be able to use the .jpg image functions in php, but each function requires me to provide an actual file name. The script that does this is called dispimg.php. Here's the code for creating an image from jpeg when I provide the loadpic.php script:

if($photoid)
{
$image = imagecreatefromjpeg("loadpic.php?photoid=$photoid");
header("Content-type: image/jpeg");
imagejpeg($image);
}

When I directly access this script by using dispimg.php?photoid=1, it should feed that into loadpic.php, get the .jpg image as a file, and then feed it into the imagecreatefromjpeg. I know that I'm creating an image from an image, but that's how the editing works with files that you already have. I need to open up a blob as a jpeg file so I can edit the image. So when I run that link I mentioned, I get the error:

Warning: imagecreatefromjpeg(loadpic.php?photoid=1): failed to open stream: No such file or directory

Yes, loadpic.php and dispimg.php are in the same directory. Has anyone tried this? How can it be accomplished? I know I could create a temporary file on the server, then load the image that way, but the whole point behind the blob is that I don't have to use files.

    You'll need to specify a URL
    so that it will be sent to the web server as an http request

    http://.../loadpic.php?photoid=$photoid

    The filesystem has no idea what ?photoid=42 is supposed to be, and will assume (if anything) that it's part of the file name.

    This is one reason why most people store binaries as files and store only paths to them in the database.

      Thanks! I did what you said and it worked perfectly. Now I can use all of those image commands to modify the blob. Thread resolved. Here is the code for both loadpic.php and dispimage.php:

      loadpic.php

      <?
      include("db-connect.php");
      if($photoid)
      {
      $dbQuery = "SELECT photoType, photoData FROM NewsPhotos WHERE photoId = $photoid";
      $result = mysql_query($dbQuery) or die("Couldn't get file list");
      $fileType = @mysql_result($result, 0, "photoType");
      $fileContent = @mysql_result($result, 0, "photoData");
      header("Content-Type: $fileType");
      echo($fileContent);
      }
      ?>
      

      dispimage.php

      <?
      //$image is the workable image variable...use it in any image command
      $image = imagecreatefromjpeg("http://www.mtulode.com/newsite/inc/loadpic.php?photoid=$photoid");
      header("Content-type: image/jpeg");
      imagejpeg($image);
      ?>
      
        Write a Reply...