View Full Version : upload to server and image resize problem...

11-19-2006, 03:42 PM

Wondering if someone could help. I'm uploading an image to my server using php. This works fine...

I then want to resize that image, and save it in another directory...this also works fine by itself.

I now want to combine the 2, so a user can upload an image, and it is automatically resized. My image resize works fine with me hardcoding the image name into the document, but once I try to use a variable $fileName (passed from the file upload..) I'm getting this error.

Parse error: syntax error, unexpected T_VARIABLE in /home/sites/mysite.co.uk/public_html/gd.php on line 8

Here is my code...but I think the basic error is how I'm using the variable from the upload in the path of the gd.php file (the open_image path)..I'll give you it all anyways...

Upload to Server file (it includes the image resize file)

include 'Connections/connBooks.php';

// connect to the mysql server
$link = mysql_connect($hostname_connBooks, $username_connBooks, $password_connBooks)
or die ("Could not connect to mysql because ".mysql_error());

// select the database
or die ("Could not select database because ".mysql_error());

// you can change this to any directory you want
// as long as php can write to it
$uploadDir = '/home/sites/mysite.co.uk/public_html/bookimages/';


$fileName = $_FILES['userfile']['name'];
$tmpName = $_FILES['userfile']['tmp_name'];
$fileSize = $_FILES['userfile']['size'];
$fileType = $_FILES['userfile']['type'];

// the files will be saved in filePath
$filePath = $uploadDir . $fileName;

// move the files to the specified directory
$result = move_uploaded_file($tmpName, $filePath);
if (!$result) {
echo "Error uploading file";

$fileName = addslashes($fileName);
$filePath = addslashes($filePath);

$query = mysql_query("insert into books (id, type, title, author, nameimg, description, price, size, typeimg, path) values ('NULL', 'NULL', '$title', 'NULL', '$fileName', 'NULL', 'NULL', '$fileSize', '$fileType', '$filePath')")
or die("Could not insert data because ".mysql_error());

//mysql_query($query) or die('Error, query failed : ' . mysql_error());

// include 'config/closedb.php';

echo "<br>File uploaded<br>";
include 'gd.php';

include file to resize images...gd.php


include 'Connections/connBooks.php';
//the plan here was to use the value from $title to query the database and //get the value from there...but no matter what variable I put in...it still //errors!
$query_img = "SELECT nameimg FROM books WHERE title='$title'";

// Load image - $fileName SHOULD be passed from my upload file...this is the //bit that's erroring!!
$image = open_image('bookimages/'$fileName'');
if ($image === false) { die ('Unable to open image'); }

// Display image

function open_image ($file) {
$im = @imagecreatefromjpeg($file);
if ($im !== false) { return $im; }

# GIF:
$im = @imagecreatefromgif($file);
if ($im !== false) { return $im; }

# PNG:
$im = @imagecreatefrompng($file);
if ($im !== false) { return $im; }

return false;

// Get original width and height
$width = imagesx($image);
$height = imagesy($image);

// Set a new width, and calculate new height
$new_width = 150;
$new_height = $height * ($new_width/$width);

// Resample
$image_resized = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($image_resized, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

// Display resized image
//header('Content-type: image/jpeg');
//imagejpeg($image_resized, 'bookthumbs/$fileName');


REALLY appreciate your help...



11-19-2006, 10:00 PM
$image = open_image('bookimages/'$fileName'');

You have to concatinate the two strings like this:

$image = open_image('bookimages/'.$fileName);

or better:

$image = open_image($uploadDir.$fileName);


11-20-2006, 09:20 AM
Thank you. I'll have a look and get back to you.

Appreciate your help.

Nicky :)