Jump to content

please help resiing image


lingo5

Recommended Posts

hi,

this is my script to upload and image, change the file extension to lowercase and save the path to my db....to which I need to add a function to resize th image to 750px wide before saving it....please help

 

$uploadDir = '../uploads/';

if(isset($_POST['upload']))
{
fforeach ($_FILES as $file)
{
$fileName = strtolower($file['name']);
$tmpName = $file['tmp_name'];
$fileSize = $file['size'];
$fileType = $file['type'];

if($fileName==""){

$filePath = '../uploads/img/none.jpg';
}
else{

$filePath = $uploadDir . $fileName;
}

////assigning random name
//// get the file extension first
$ext = substr(strrchr($fileName, "."), 1); 

//// make the random file name
$randName = md5(rand() * time());

//// and now we have the unique file name for the upload file
$filePath = $uploadDir . $randName . '.' . $ext;

// Replace spaces with a '_'
$filePath = str_replace(" ", "_", $filePath);
$result = move_uploaded_file($tmpName, $filePath);


if(!get_magic_quotes_gpc())
{
$fileName = addslashes($fileName);
$filePath = addslashes($filePath);
}
$fileinsert[]=$filePath;
}
Edited by lingo5
Link to comment
Share on other sites

For this you'll have to use imagecopyresampled, which will allow you to copy and scale the image. Prior to this, however, you'll need to calculate the correct new dimensions of the scaled down version. Normally this is done by finding which of the dimensions has the greatest difference, in percent, compared to the maximum size for that particular side. This you figure out by taking the actual size and dividing it by the maximum size, for both dimensions. Then compare the results against each other, and select the biggest number out of the two.

Next you'll need to divide the actual dimensions on this number, to get the target dimensions for the new image. At which point you're ready to use the above function, to get a nicely scaled picture, with the same aspect ratios as the original. ;)

 

PS: If you only need it to be less than 750 pixels wide, you can just check if the width is bigger. If it is, calculate the difference ratio for the width, using the same method as described above, and use it straight away. No need to calculate the ratio for the height.

Edited by Christian F.
Link to comment
Share on other sites

1. Get the size of the image: getimagesize()

2. If image size < 750, do nothing - exit

3. Determine the percentage for the new size: 750 divided by current width

4. Determine the new height using current height times the percentage above

5. Use imagecopyresampled() to resize the image. Example #1 and the information above should be enough to get started.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.