Hi, I have a successfully working system at the moment where a user can choose a file and upload it to my MYSQL database as a BLOB, but before the image gets saved as a BLOB I want to resize it.

I have been giving this a try by using some tutorials but I cannot seem to get it working correctly. I know most people would suggest not saving the image as a BLOB and doing it through file directories but this is not how I want my system to work.

Here is my current code:

header("Location: profile.php?view={$_GET['view']}");
include_once 'usersession.php';

if (isset($_GET['view'])) $view = sanitizeString($_GET['view']); //get user details
else $view = $user;

//process to submit the text and the image
if (isset($_FILES['image']) && $_FILES['image']['size'] > 0 && $_FILES['image']['size'] < 400000 && isset($_POST['text']))
        echo "process both";
    	$text = sanitizeString($_POST['text']);
	$text = preg_replace('/\s\s+/', ' ', $text);

        $tmpName  = $_FILES['image']['tmp_name'];
        // Read the file
        $fp      = fopen($tmpName, 'r');
        $data = fread($fp, filesize($tmpName));
        $data = addslashes($data);
        $fileName = $_FILES['imageFile']['name'];
        $tmpName  = $_FILES['imageFile']['tmp_name'];
        $fileSize = $_FILES['imageFile']['size'];
        $fileType = $_FILES['imageFile']['type'];

        if ($fileName)
            $fp      = fopen($tmpName, 'r+');
            $content = fread($fp, filesize($tmpName)); //reads $fp, to end of file length
            // get originalsize of image
            $im = imagecreatefromstring($content);
            $width = imagesx($im);
            $height = imagesy($im);

            // Set thumbnail-height to 180 pixels
            $imgh = 180;
            // calculate thumbnail-height from given width to maintain aspect ratio
            $imgw = $width / $height * $imgh;
            // create new image using thumbnail-size
            // copy original image to thumbnail
            imagecopyresampled($thumb,$im,0,0,0,0,$imgw,$imgh,ImageSX($im),ImageSY($im)); //makes thumb

            imagejpeg($thumb, "test.jpg", 80);  //imagejpeg($resampled, $fileName, $quality);
            $instr = fopen("test.jpg","rb");  //need to move this to a safe directory
            $image = addslashes(fread($instr,filesize("test.jpg")));
            queryMysql("UPDATE user SET about='$text', avatar='$image' WHERE user='$user'");

        queryMysql("UPDATE user SET about='$text', avatar='$data' WHERE user='$user'");

As you can see there is a large amount commented out, this is my attempt by following a tutorial but it does not work, the image does not get saved into the database and no errors are given to explain why.

I hope someone will be able to help, Thanks.

Recommended Answers

All 2 Replies

Member Avatar
queryMysql("UPDATE user SET about='$text', avatar='$data' WHERE user='$user'");

looks like you're calling an user-defined function (queryMysql).

Does this function exist? IF not, just use the usual mysql_query(...sql...).

Yea that function exists in the included file the function is as so:

function queryMysql($query)
	$result = mysql_query($query) or die(mysql_error());
	return $result;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.