#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    56
    Rep Power
    2

    Re-sizing images on upload


    Hi,

    I want to dynamically resize images on upload with the resize_class.

    I want to combine the following code with the upload code which I already have:

    PHP Code:
    <?php

        
    // *** Include the class
        
    include("resize-class.php");

        
    // *** 1) Initialise / load image
        
    $resizeObj = new resize('sample.jpg');

        
    // *** 2) Resize image (options: exact, portrait, landscape, auto, crop)
        
    $resizeObj -> resizeImage(200200'auto');

        
    // *** 3) Save image
        
    $resizeObj -> saveImage('sample-resized.jpg'100);

    ?>
    however, my images are saved into a folder while data (filename etc) is saved into a mysql database.

    This is my upload image:

    PHP Code:
    <?php
         
    //open database
    //    include("includes/db_connect.php");  requires select database  !!!
    require("includes/mysql_conn.php");
         
        
    // Check to see if the type of file uploaded is a valid image type .........................
    function is_valid_type($file)
    {
        
    // This is an array that holds all the valid image MIME types
        // These are the same for all file upload boxes
        
    $valid_types = array("image/jpg""image/jpeg""image/bmp""image/gif");

        
    // This is an array that holds all valid image extensions
        // These are the same for all file upload boxes
        
    $valid_exts = array('jpg''jpeg''bmp''gif');
        
        
    // This check is optional
        
    if(!in_array($file['type'], $valid_types))
            return 
    0;
            
        
    // Get the extension from the uploaded filename
        
    $upload_ext pathinfo($file['name'], PATHINFO_EXTENSION);
        
        
    // This check is essential for security
        
    if(!in_array($upload_ext$valid_exts))
            return 
    0;
        
        return 
    1;
    }  
         
        
    // This variable is the path to the image folder where all the images are going to be stored
        // Note that there is a trailing forward slash
        
    $TARGET_PATH "images/";
         
        
    // Get our POSTed variables
        
    $cat_id = (int) $_POST['cat_id'];
        
    $ctitle $_POST['ctitle'];
        
    $image $_FILES['image'];

    // Build our target path full string. This is where the file will be moved do
    // i.e. images/picture.jpg
    $target_path_1 $TARGET_PATH $image['name'];

        
    // Sanitize our inputs
        
    $cat_id = ($cat_id);
        
    $ctitle mysql_real_escape_string($ctitle);
        
    $image['name'] = mysql_real_escape_string($image['name']);
        
        
    // Make sure all the fields from the form have inputs
        
    if ( $cat_id == "" || $ctitle == "" || $image['name'] == "")
        {
    ?>
             All fields marked <font color="red">*</font> are required
        <?
            
    exit;
        }
         
    // Check to make sure that our file is actually an image
    // You check the file type instead of the extension because the extension can easily be faked
    if (!is_valid_type($image))
    {    
    ?>
             <font color="red">You must upload a jpeg, gif, or bmp</font>
        <?
             
    exit;
    }  

    // Here we check to see if a file with that name already exists
    // You could get past filename problems by appending a timestamp to the filename and then continuing
    if (file_exists($target_path_1))
    {
        
    ?>
             <font color="red">A file with that name already exists</font>
        <?

            
    exit;
    }  
         
    // Lets attempt to move the file from its temporary directory to its new home
    if (
        
    move_uploaded_file($image['tmp_name'], $target_path_1)
    )
    {
             
    // NOTE: This is where a lot of people make mistakes.
             // We are *not* putting the image into the database; we are putting a reference to the file's location on the server
             
    $sql "insert into collections (cat_id, ctitle, cfilename) values ('$cat_id', '$ctitle', '" $image['name'] . "')";
             
    $result mysql_query($sql) or die ("Could not insert data into DataBase: " mysql_error());

             exit;
    }
    else
    {
             
    // A common cause of file moving failures is because of bad permissions on the directory attempting to be written to
             // Make sure you chmod the directory to be writeable

           
    echo "Could not upload file. Check read/write persmissions on the directory";

             exit;
    }  
        
    ?>
    How should I go about it?
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,999
    Rep Power
    9397
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,972
    Rep Power
    375
    if you mean.. change it so that it is stored on database then don't.

    otherwise, well I echo what requinix said.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    56
    Rep Power
    2
    Originally Posted by paulh1983
    if you mean.. change it so that it is stored on database then don't.

    otherwise, well I echo what requinix said.
    I want to use the class I mentioned earlier for images saved into a folder but their data is saved in a database.
  8. #5
  9. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,999
    Rep Power
    9397
    You've described the situation well enough. What's the problem?
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,972
    Rep Power
    375
    Originally Posted by josephbupe
    I want to use the class I mentioned earlier for images saved into a folder but their data is saved in a database.
    when you say "their" data?

    well to me it sounds like you want to do opposite of what this class you are using is doing? correct

    in this case you should fiddle around with:

    // *** 3) Save image
    $resizeObj -> saveImage('sample-resized.jpg', 100); saveImage function so it does what you want it to do rather than what it does
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2012
    Posts
    56
    Rep Power
    2
    Originally Posted by paulh1983
    when you say "their" data?

    well to me it sounds like you want to do opposite of what this class you are using is doing? correct

    in this case you should fiddle around with:

    // *** 3) Save image
    $resizeObj -> saveImage('sample-resized.jpg', 100); saveImage function so it does what you want it to do rather than what it does
    Ok, what I want is to replace the "sample-resized.jpg" with the name of my image variable since my image is not embedded.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,972
    Rep Power
    375
    Originally Posted by josephbupe
    Ok, what I want is to replace the "sample-resized.jpg" with the name of my image variable since my image is not embedded.
    so what is stopping you replace that with your variable

IMN logo majestic logo threadwatch logo seochat tools logo