Jump to content

Modify image upload and display script


Pig

Recommended Posts

Hi all

 

I have a script which uploads a file, renames it (unix time), adds .jpg file extension, then displays the image at a reduced resolution.

 

The problem is that after the image is uploaded, the the server sends the full image to browser which resizes the image. This is wasting bandwidth as the server downloads the entire image and then resizes to a reduced resolution.

 

Ideally, I need to modify the script to reduce the resolution first, then save it in the server before downloading to browser. This will reduce bandwidth wastage. However, I will still need the original image saved in the server. So perhaps the modified image should be saved in a different folder.

 

<?php

$n=$_FILES['user_file']['name'];
$type=$_FILES['user_file']['type'];
$size=$_FILES['user_file']['size'];

$fext=".jpg";

$time=time();
$n=$time;
$path="uploads\\". $n;
$path2="uploads\\". $n . $fext;

?>

<?php  if ($size<10590617 && ereg("image", $type))
    {
    move_uploaded_file($_FILES['user_file']['tmp_name'],
    $path2);


$root= "uploads/";
$path3=$root . $n . $fext;

       }
    
    
    ?>

// Please do not modify up to here! Pig

<?php
function imageResize($image, $target, $alt=NULL) {

//Gets the width and height of the image and outputs it as $theimage[0] (width) and $theimage[1] (height)
$theimage = getimagesize($image);
$width = $theimage[0];
$height = $theimage[1];


//takes the larger size of the width and height and applies the formula accordingly...this is so this script will work dynamically with any size image

if ($width > $height) {
$percentage = ($target / $width);
} else {
$percentage = ($target / $height);
}


//gets the new value and applies the percentage, then rounds the value
$width = round($width * $percentage);
$height = round($height * $percentage);


//Returns the new sizes inside an image tag so you can call it with "imageResize("uploads/$path3", "Some alt text", $target)"

echo "<img src=\"$image\" width=\"$width\" height=\"$height\" alt=\"$alt\">";
}
?>

<!- uses imageResize function instead of mixing up your PHP with an IMG tag -->

<?php imageResize("$path3", 250, "Your Image"); ?>

 

Thanks

 

I hope someone can help?

 

Pig

Link to comment
https://forums.phpfreaks.com/topic/173263-modify-image-upload-and-display-script/
Share on other sites

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.