Jump to content


Photo

Resizing An Image Please Help :(


  • Please log in to reply
2 replies to this topic

#1 battleroyalex

battleroyalex
  • Members
  • Pip
  • Newbie
  • 6 posts

Posted 25 February 2006 - 11:08 PM

I currently have an image uploading system but I need to add a line of code into it to resize the images width to 600 and delete the old image.

Current Form:
<form enctype="multipart/form-data" action="uploader.php" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
Choose a file to upload: <input name="uploadedfile" type="file" /><br />
<input type="submit" value="Upload File" />
</form>

Current Uploader.php:
<?php
// Where the file is going to be placed
$target_path = "uploads/";

$target_path = $target_path . basename( $_FILES['uploadedfile']['name']);

if(move_uploaded_file($_FILES['uploadedfile']['tmp_name'], $target_path)) {
    echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded";
} else{
    echo "There was an error uploading the file, please try again!";
}

?>

Its a very simple script im really new at this can someone fix it up to resize the images to 600 width :(?

#2 battleroyalex

battleroyalex
  • Members
  • Pip
  • Newbie
  • 6 posts

Posted 26 February 2006 - 06:01 AM

come on guys no help at all?

#3 litebearer

litebearer
  • Members
  • PipPipPip
  • Advanced Member
  • 2,357 posts
  • Locationwhite lake michigan

Posted 26 February 2006 - 03:59 PM

Here is a script I use for our family album site. Frequently my banjo playing kin upload pictures that are greater than 800 x 600, so I run this program each night to resample them. It converts gifs and pngs to jpg and deletes the old file.

I am sure you could adapt this to meet your needs. Also with a little tweaking it could create and save thumbnail versions.
<?PHP

###########################################
#
# script to take all the images in a folder and resample them.  it will loop through all the images 
# in a folder and check to see if width is > 800 and/or height > 600 . If the image meets
# either of those criteria, the script will resample the image to fall within the guidelines 
#
############################################

// set path to image folder

$image_folder = "pics";

if ($handle = opendir($image_folder))
{
    while (false !== ($file = readdir($handle)))
    { 
        if ($file != "." && $file != ".." )
        { 
            $image_name = $image_folder . '/' . $file;
            $image_type = strstr($image_name, '.');

            switch($image_type) {
                case '.jpg':
                    $src = imagecreatefromjpeg($image_name);
                    break;
                case '.png':
                    $src = imagecreatefrompng($image_name);
                    $totlen = strlen($image_name) - 3;
                    $newname = substr($image_name, 0, $totlen) . "jpg";
                    unlink($image_name);
                    $image_name = $newname;
                    break;
                case '.gif':
                    $src = imagecreatefromgif($image_name);
                    $totlen = strlen($image_name) - 3;
                    $newname = substr($image_name, 0, $totlen) . "jpg";
                    unlink($image_name);
                    $image_name = $newname;

                    break;
                default:
                    echo "Error Invalid Image Type: " . $image_name . " x x x " . $image_type;
                    die;
                    break;
            }

            $width = imagesx($src); 
            $height = imagesy($src); 

            // check that width is 800 or less
            if ($width > 800)
            {
                $ratio_w = 800/$width;
            } else {
                $ratio_w = 1;
            }

            // check if height is 600 or less
            if ($height > 600)
            {
                $ratio_h = 600/$height;
            } else {
                $ratio_h = 1;
            }

            // determine which ratio creates the smaller image
            if ($ratio_w <= $ratio_h)
            {
                $aspect_ratio = $ratio_w;
            } else {
                $aspect_ratio = $ratio_h;
            }

            // new resizing code
            $new_w = abs($width * $aspect_ratio);
            $new_h = abs($height * $aspect_ratio);
            $size = $new_h;

            $aspect_ratio = $height/$width; 

            //start resizing 
            if ($height <= $size) 
            { 
                $new_w = $width; 
                $new_h = $height; 
            } else { 
                $new_h = $size; 
                $new_w = abs($new_h / $aspect_ratio); 
            } 


            $img = imagecreatetruecolor ($new_w,$new_h); 

            //save image 
            imagecopyresampled ($img,$src,0,0,0,0,$new_w,$new_h,$width,$height); 

            imagejpeg($img, $image_name, 90); 
            imagedestroy($img); 

        } 
    }
    closedir($handle); 
}

?>

Hope its of some help.

Lite...

all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users