Image re-sizing

Discussion in 'PHP / Perl / Ruby on Rails' started by DesignWizard, Oct 28, 2008.

  1. DesignWizard CSNM Customer

    Does anyone know of any image re-sizing scripts that allow the user to upload an image which is then resized and stored on the server.

    Also need it to be proportional and have the ability to set max height/width sizes.

    Thanks
  2. Andrew Taylor CS New Media Staff

    You can do it fairly easily yourself, what I do is upload to a temp folder, resample and then move to the correct folder

    I can probably fish out something from the CMS if you need it
  3. Mark Voss CSNM Customer

    I usually use ASPUpload and ASPJpeg for jobs like this although I've been dabbling locally with a drop-in asp.net script which does the resizing just as well.

    If you've only a couple of images per page, you can allow users to upload quite big images and then resize them dynamically without increasing either server load or page load speed too much.
  4. Andrew Taylor CS New Media Staff

    PHP:


    function fileextension($Filename) {
    $ext1 explode("."$Filename);
    $ext3 count($ext1);
    $ext2 $ext3 1;
    $Ext $ext1[$ext2];
    return 
    $Ext;
    }


    // Where the file is going to be placed, temp directory for manipulation
    $target_path "../temp/";

    /* Add the original filename to our target path.
    Result is "uploads/filename.extension" */
    $target_path $target_path basename$_FILES['uploadedfile']['name']);
    $_FILES['uploadedfile']['tmp_name'];

    $target_path "../temp/";

    $target_path $target_path basename$_FILES['uploadedfile']['name']);

    if(
    move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {

    // Set a maximum height and width
    $width $Imgw;
    $height $Imgh;

    // Get new dimensions
    list($width_orig$height_orig) = getimagesize($target_path);

    $ratio_orig $width_orig/$height_orig;

    if (
    $width/$height $ratio_orig) {
       
    $width $height*$ratio_orig;
    } else {
       
    $height $width/$ratio_orig;
    }
    $Ext strtolower(fileextension($target_path));
    // Grab new image
    switch ($Ext) {
    case 
    "gif" :
    $image_t imagecreate($width$height);
    break;
    case 
    "jpg" :
    $image_t imagecreatetruecolor($width$height);
    break;
    case 
    "png" :
    $image_t imagecreatetruecolortransparent($width$height);

    break;
    }


    switch (
    $Ext) {
    case 
    "gif" :
    $imagelarge imagecreatefromgif($target_path);
    break;
    case 
    "jpg" :
    $imagelarge imagecreatefromjpeg($target_path);
    break;
    case 
    "png" :
    $imagelarge imagecreatefrompng($target_path);
    break;
    }
    // Create with new dimensions
    imagecopyresampled($image_t$imagelarge0000$width$height$width_orig$height_orig);
    $target_path5 "../images/";
    $newName2 $_FILES['uploadedfile']['name'];


    $water $target_path5 $newName;
    // Output
    switch ($Ext) {
    case 
    "gif" :
    imagegif($image_t$water75);
    break;
    case 
    "jpg" :
    imagejpeg($image_t$water75);
    break;
    case 
    "png" :
    imagepng($image_t$water0);
    break;
    }

    // Then delete temp file
          
    $imagedir "../temp/";
          
    unlink($imagedir $newName2);
          
    $Imageu = ( $_FILES['uploadedfile']['name'] );
    }
    else { echo 
    "Error Uploading Image"; }

    To get you started
  5. DesignWizard CSNM Customer

    Thanks Andrew.

    Will have a play and let you know how it goes!
  6. DesignWizard CSNM Customer

    It doesn't seen to upload the file into the temp folder at all. I have checked that folder permissions are set correctly but it just doesn't upload the file when submitted via html form.
  7. Andrew Taylor CS New Media Staff

    Can you post your code for the form and php?
  8. DesignWizard CSNM Customer

    HTML:
    <body>
    <form enctype='multipart/form-data'  name="imagesize" action="do.imageresize.php" method="POST">
    <input name="file" type="file" />
    <input type="submit" name="" value="submit" />
    </form>
    </body>
    
    </html>
    
    PHP:
    <?php
    function fileextension($Filename) {
    $ext1 explode("."$Filename);
    $ext3 count($ext1);
    $ext2 $ext3 1;
    $Ext $ext1[$ext2];
    return 
    $Ext;
    }


    // Where the file is going to be placed, temp directory for manipulation
    $target_path "temp/";

    /* Add the original filename to our target path.
    Result is "uploads/filename.extension" */
    $target_path $target_path basename$_FILES['uploadedfile']['name']);
    $_FILES['uploadedfile']['tmp_name'];

    $target_path "temp/";

    $target_path $target_path basename$_FILES['uploadedfile']['name']);

    if(
    move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {

    // Set a maximum height and width
    $width $Imgw;
    $height $Imgh;

    // Get new dimensions
    list($width_orig$height_orig) = getimagesize($target_path);

    $ratio_orig $width_orig/$height_orig;

    if (
    $width/$height $ratio_orig) {
       
    $width $height*$ratio_orig;
    } else {
       
    $height $width/$ratio_orig;
    }
    $Ext strtolower(fileextension($target_path));
    // Grab new image
    switch ($Ext) {
    case 
    "gif" :
    $image_t imagecreate($width$height);
    break;
    case 
    "jpg" :
    $image_t imagecreatetruecolor($width$height);
    break;
    case 
    "png" :
    $image_t imagecreatetruecolortransparent($width$height);

    break;
    }


    switch (
    $Ext) {
    case 
    "gif" :
    $imagelarge imagecreatefromgif($target_path);
    break;
    case 
    "jpg" :
    $imagelarge imagecreatefromjpeg($target_path);
    break;
    case 
    "png" :
    $imagelarge imagecreatefrompng($target_path);
    break;
    }
    // Create with new dimensions
    imagecopyresampled($image_t$imagelarge0000$width$height$width_orig$height_orig);
    $target_path5 "images/logos/";
    $newName2 $_FILES['uploadedfile']['name'];


    $water $target_path5 $newName;
    // Output
    switch ($Ext) {
    case 
    "gif" :
    imagegif($image_t$water75);
    break;
    case 
    "jpg" :
    imagejpeg($image_t$water75);
    break;
    case 
    "png" :
    imagepng($image_t$water0);
    break;
    }

    // Then delete temp file
          
    $imagedir "temp/";
          
    unlink($imagedir $newName2);
          
    $Imageu = ( $_FILES['uploadedfile']['name'] );
    }
    else { echo 
    "Error Uploading Image"; }


    ?>
  9. Andrew Taylor CS New Media Staff

    You need to change either the form or PHP code for the field names to match, it's called "file" on the form and "uploadedfile" on the script

    At the moment they are just ignoring each other
  10. Andrew Taylor CS New Media Staff

    You also need to define the image dimensions, either in the PHP, or pass them from the form and define them from that
  11. DesignWizard CSNM Customer

    Thanks. All sorted now!

Share This Page