Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Mar 2006
    Posts
    200
    Thanks
    27
    Thanked 2 Times in 2 Posts

    upload to server and image resize problem...

    Hello!

    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)
    PHP Code:
    <?
    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
    mysql_select_db($database_connBooks)
    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/';

    $title=$_POST['title'];

    if(isset(
    $_POST['upload']))
    {
        
    $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";
            exit;
        }
        
        if(!
    get_magic_quotes_gpc())
        {
            
    $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

    PHP Code:
    <?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
    //imagejpeg($image);

    function open_image ($file) {
            
    # JPEG:
            
    $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$image0000$new_width$new_height$width$height);

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

    ?>
    REALLY appreciate your help...

    Thanks!

    Nicky

  • #2
    New to the CF scene
    Join Date
    Aug 2006
    Location
    Germany, Heilbronn
    Posts
    6
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    $image open_image('bookimages/'$fileName''); 
    You have to concatinate the two strings like this:

    PHP Code:
    $image open_image('bookimages/'.$fileName); 
    or better:

    PHP Code:
    $image open_image($uploadDir.$fileName); 

  • #3
    Regular Coder
    Join Date
    Mar 2006
    Posts
    200
    Thanks
    27
    Thanked 2 Times in 2 Posts
    Thank you. I'll have a look and get back to you.

    Appreciate your help.

    Nicky


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •