Jump to content

Resizing An Image Please Help :(


battleroyalex

Recommended Posts

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:
[code]<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>[/code]

Current Uploader.php:
<?php
[code]
// 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!";
}

?> [/code]

Its a very simple script im really new at this can someone fix it up to resize the images to 600 width :(?
Link to comment
https://forums.phpfreaks.com/topic/3602-resizing-an-image-please-help/
Share on other sites

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.
[code]
<?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);
}

?>[/code]

Hope its of some help.

Lite...

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.