Jump to content

Conditional image resize on upload


rubik

Recommended Posts

Hey folks - I'm (obviously) new to the forum, but have been playing with PHP on and off for a few years, using it for what you could call merely "recreational" purposes.

Recently I created a website for my friends and family. The site has two pages: One which uses PHP to upload images from visitors' hard drives to a specific folder on the server, and one that displays thumbnails of all the images, with links to the full-sized ones (which open up in Javascript windows defined to be exactly the size of each individual picture). The upload script JUST uploads the images, and the gallery script takes all the images in the uploaded images folder and makes/displays the thumbnails, etc.

There are no complicated user accounts, no passwords, no asking any personal information...just the opportunity to share funny images with the relatively small group of people who know  of the website.

I encounted one problem: When my friends attempt to upload images straight from their digital camera (as in, .JPG images that haven't been resized or anything--just imported from a memory card), the image successfully uploads, and the thumbnail successfully creates, but naturally when a 4-megapixel camera takes a picture, the image is thousands of thousands of pixels in dimension. Therefore when a visitor clicks the thumbnail for that particular image, the Javascript window that pops up is MASSIVE and only a fraction of the full picture takes up the whole screen (thus making it impossible to conveniently view the picture).

After that lengthy introduction, what I want is simple: When a visitor uploads an image that's more than 1024x768 pixels or so, I'd like the upload script to detect the size of the image, resize it to a width of 1024 and change the height accordingly to maintain the aspect ratio, and then save [b]that[/b] image to the uploaded images folder for the gallery script to deal with.

Here is my uploading code...I'd be grateful beyond words if someone could give me something to add somewhere in here to implement that resizing function.

------------------------------------------------------

<?php
//edit this
$_max_file_size = '5242880'; //file size in bytes.
$upload_dir = "images/"; //upload folder..chmod to 777
$_i = "3";                //number of files to upload at one time
//end edit

echo "Maximum file size : " . $_max_file_size/1000 . "KB<br>";
echo "<form enctype='multipart/form-data' action='?do=upload' method='post'>";
echo "<input type='hidden' name='MAX_FILE_SIZE' value='" . $_max_file_size , "'>";
echo "Choose your file(s): <br>";
//show number of files to select
For($i=0; $i <= $_i-1;$i++)
{
echo "<input name='file" . $i . "' type='file'><br>";
}
echo "<input type='submit' value='Upload File'>";
echo "</form>";

if($_GET['do'] == 'upload')
{
//upload all the fields until done
For($i=0; $i <= $_i-1; $i++)
{
//create a random number
$_random = rand(1, 1000000);
//file with the upload folder
$target_path = $upload_dir . $_random . basename($_FILES['file' . $i]['name']);
//actual file name with the random number
$_file_name = $_random . basename($_FILES['file' . $i]['name']);

//do not upload the 'left blank' fields
if(basename($_FILES['file' . $i]['name']) != '')
{

if(move_uploaded_file($_FILES['file' . $i]['tmp_name'], $target_path))
{
    //uploaded successfuly
    $_uploaded=1;
}
else
{
    //error uploading
    $_error=1;
}
}
else
{
$_check=$_check+1;
}

}

//file(s) did upload
if($_uploaded == '1')
{
$_uploaded=0;
echo "The file(s) have been uploaded.<br>";
}
//file uploaded?
if($_error == '1')
{
$_error=0;
echo "There was an error uploading some of the file(s). Please try again! The file may have been too large. The maximum file size is " . $_max_file_size/1000 . "KB<br>";
}
//user selected a file?
if($_check == $_i)
{
$_check=0;
echo "Select a file first then click 'Upload File'<br>";
}
}
?>

------------------------------------------------------
Link to comment
https://forums.phpfreaks.com/topic/33998-conditional-image-resize-on-upload/
Share on other sites

Something like this should help :)

[code]
<?php
function resize_jpg($file, $newsize){
// Get the current image size
$image_data = getimagesize($file);
// Current width
$current_width = $image_data[0];
// Current height
$current_height = $image_data[1];

// Resize the image
if ($current_height > $current_width) {
// Calculate the proper dimensions
$width = ($newsize / $current_height) * $current_width;
$height = $newsize;
} else {
// Calculate the proper dimensions
$new_width = ($new_size / $current_width) * $current_height;
$new_height = $new_size;
}

// Create a new image with the new sizes
$new_image = ImageCreateTrueColor($new_width, $new_height);
$image = ImageCreateFromJpeg($file);
imagecopyResampled ($new_image, $image, 0, 0, 0, 0, $new_width, $new_height,
  $image_data[0], $image_data[1]);
return $new_image;
}
?>
[/code]

Scot McConnaughay
  • 2 weeks later...
  • 2 weeks later...
OK I have been having some troubles with this -- scottmcc gave me this new code, but I don't know how to call it within my current script...can [b]anyone[/b] help?

I just want it to detect if the image is more than 800x600 or so, and then resize it to that before uploading.

Any help?
This should do the trick.  You'll need to set $file and $newsize.
[code]
<?php
// Get the current image size
$image_data = getimagesize($file);
// Current width
$current_width = $image_data[0];
// Current height
$current_height = $image_data[1];

if($current_width>800||$current_height>600)
{
resize_jpg($file, $newsize);
}
//UPLOAD FILE
?>
[/code]
thanks so much for the help, but as it says to the left, i am what you call a "noobie." :)

could you please give me an example of the two scripts integrated?

[code]
<?php
function resize_jpg($file, $newsize){
// Get the current image size
$image_data = getimagesize($file);
// Current width
$current_width = $image_data[0];
// Current height
$current_height = $image_data[1];

// Resize the image
if ($current_height > $current_width) {
// Calculate the proper dimensions
$width = ($newsize / $current_height) * $current_width;
$height = $newsize;
} else {
// Calculate the proper dimensions
$new_width = ($new_size / $current_width) * $current_height;
$new_height = $new_size;
}

// Create a new image with the new sizes
$new_image = ImageCreateTrueColor($new_width, $new_height);
$image = ImageCreateFromJpeg($file);
imagecopyResampled ($new_image, $image, 0, 0, 0, 0, $new_width, $new_height,
  $image_data[0], $image_data[1]);
return $new_image;
}
?>
[/code]

and

[code]
<?php
// Get the current image size
$image_data = getimagesize($file);
// Current width
$current_width = $image_data[0];
// Current height
$current_height = $image_data[1];

if($current_width>800||$current_height>600)
{
resize_jpg($file, $newsize);
}
//UPLOAD FILE
?>
[/code]

I want the new size to be 800x600, and the image to be uploaded to the folder [i]images/[/i].

I really appreciate it.
In your upload file you have at the moment do this:
[code]
<?php
//ANY HEADERS BITS(SESSIONS ETC)

function resize_jpg($file, $newsize){
// Get the current image size
$image_data = getimagesize($file);
// Current width
$current_width = $image_data[0];
// Current height
$current_height = $image_data[1];

// Resize the image
if ($current_height > $current_width) {
// Calculate the proper dimensions
$width = ($newsize / $current_height) * $current_width;
$height = $newsize;
} else {
// Calculate the proper dimensions
$new_width = ($new_size / $current_width) * $current_height;
$new_height = $new_size;
}

// Create a new image with the new sizes
$new_image = ImageCreateTrueColor($new_width, $new_height);
$image = ImageCreateFromJpeg($file);
imagecopyResampled ($new_image, $image, 0, 0, 0, 0, $new_width, $new_height,
  $image_data[0], $image_data[1]);
return $new_image;
}

// Get the current image size
$image_data = getimagesize($file);
// Current width
$current_width = $image_data[0];
// Current height
$current_height = $image_data[1];

$newsize=800
$file=yourpic.php //YOU NEED TO DYNAMICALLY CREATE THIS.
if($current_width>800||$current_height>600)
{
resize_jpg($file, $newsize);
}
//WHATEVER CODE YOU CURRENTLY HAVE TO UPLOAD THE IMAGE.
?>
[/code]
Hope it makes sense.
cagecrawler, thanks for being so patient with me :)

for the $file variable, i understand i have to have that set to the dynamically created name for the individual image -- each image is assigned a random number and that's placed before the actual file name.

[code]
<?php
//create a random number
$_random = rand(1, 1000000);
//file with the upload folder
$target_path = $upload_dir . $_random . basename($_FILES['file' . $i]['name']);
//actual file name with the random number
$_file_name = $_random . basename($_FILES['file' . $i]['name']);
?>
[/code]

so that somehow needs to be put into $file?

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.