0

I have successfully added the original image into my imgs/ folder and also onto the server. But I'm wanting to add the thumbnail into the database. I've added it into the imgs/ folder but can't seem to find away to insert it into the database.

This is the final bit of code that is used to crop the img and insert it to the folder.

I need to insert it into the database also so I can call on it for the $_SESSION User and the Users friend as I have profiles.

if (isset($_POST["upload_thumbnail"]) && strlen($large_photo_exists)>0) {
    //Get the new coordinates to crop the image.
    $x1 = $_POST["x1"];
    $y1 = $_POST["y1"];
    $x2 = $_POST["x2"];
    $y2 = $_POST["y2"];
    $w = $_POST["w"];
    $h = $_POST["h"];
    //Scale the image to the thumb_width set above
    $scale = $thumb_width/$w;
    $cropped = resizeThumbnailImage($thumb_image_location, $large_image_location,$w,$h,$x1,$y1,$scale);
    //Reload the page again to view the thumbnail
    header("location:".$_SERVER["PHP_SELF"]);
    exit();
}
if(isset($_GET['a'])){
if ($_GET['a']=="delete"){
if (file_exists($large_image_location)) {
        unlink($large_image_location);
    }
    if (file_exists($thumb_image_location)) {
        unlink($thumb_image_location);

        $creator_id     =   $_SESSION['id'];
            $sql = "UPDATE users SET user_pic_small='".$img."' WHERE id=$creator_id";
            $sql2 = "INSERT INTO userphotos(photo_ownerid,photo_ispublic, photo_name, photo_caption, photo_imagedata) VALUES ($creator_id,1,'Profile Picture','Profile Picture','$img')";

            // insert the image
            if(!mysql_query($sql)) {
                echo "Fail. It broke.";
            }else{
            $c=mysql_query($sql2);

                echo "<script> parent.alert('Image Uploaded','',1000);</script>";
            }

    }
}
}

Hope someone can help. Thanks.

dave
  • 1,009
  • 5
  • 15
  • 26

4 Answers4

2

If you want to add in your database the path of thumbnail ($thumb_image_location), just add the code that inserts the path before unlink().

If you want to store the whole image into database, you need the column to be MEDIUMBLOB type, then, before unlink() read the code of the file that contains the image, for example with:

$img = file_get_contents($thumb_image_location);

Then, INSERT data stored in $img into your database.

CoursesWeb
  • 4,179
  • 3
  • 21
  • 27
  • It has to be of `BLOB` type, `SMALL`, `MEDIUM` or `LARGE` depends on the size of the thumbnails. – Romain Jul 10 '12 at 11:38
  • 1
    He might have to apply `addslashes()` to the $img variable such as `$img = addslashes($img);` before storing into the DB. – Andreas W. Wylach Jul 10 '12 at 11:43
  • Like this, this is how my main image looks when preparing it for INSERTION. $img =addslashes (file_get_contents($thumb_image_location)); – dave Jul 10 '12 at 11:50
  • Looks OK to me. See my answer, just posted it before you made your update to the code, I used the `read()` fct. You can easily adapt it. – Andreas W. Wylach Jul 10 '12 at 11:54
  • Thank you everyone. My thumbnail is now successfully in my database aswell as my folder. A simple $img =addslashes(file_get_contents($thumb_image_location)) <- Thanks to Courses Web and Andreas for your combined useful answers. And thank you to everyone else for their time and input. – dave Jul 10 '12 at 12:01
1

Ideally, you don't want to be adding the thumbnail itself to the database, just a reference (filepath) to the file. So, while I don't know what your database looks like, you need to go through the following steps:

  1. Create a field in your table called 'thumbnail' or similar. This will hold the name which the thumbnail file is saved as.
  2. Add the filepath to the database immediately after you crop the large image (ie between the lines '$cropped = ...' and 'header("location....' in your code)
  3. Whenever a user or user's friend is logged in, check this field and pull any thumbnail images referenced in the table.

And that is basically it.

Community
  • 1
  • 1
hellsgate
  • 5,905
  • 5
  • 32
  • 47
  • Yeah its just the filepath. Sorry I didn't specify that clearly. Trying out the few answers now. :) – dave Jul 10 '12 at 11:42
0

If you want to store only the path of the Image into Database ,than it's fine to insert only the path and serve it with HTML.

Otherwise if you want to store the raw data of the image into Database than you have to encode the Image into base64 String .

Sending/Displaying a base64 encoded Image - Here is how you encode and image at base64.

And store this huge string into a Blob Field Type into databse.

Community
  • 1
  • 1
Rosmarine Popcorn
  • 10,761
  • 11
  • 59
  • 89
0

You can use this:

// Read the file 
$fp = fopen($file, 'r');
$data = fread($fp, filesize($file));
$data = addslashes($data);
fclose($fp);

// Create the query and insert into our database.
// image is an BLOB field type
$query = "INSERT INTO tbl_images ";
$query .= "(image) VALUES ('$data')";
$results = mysql_query($query, $link);
Andreas W. Wylach
  • 723
  • 2
  • 10
  • 31