Jump to content

Images not reducing in size:


roldahayes

Recommended Posts

Hi, I'm using the script below to that allows a user to upload a photo, which then appears on the page as a thumbnail, the image can be clicked and opens up in a lightbox image pop up.

 

All this works fine except the images are not resizing.  I.e if a a picture is 1024px wide, before upload - it stays that size on the server.  I need it to compress down to around 600px wide and have the KB of the file reduced.

 

I know that I should be using GD library to do this but not sure how to implement it.

 

The script I am using is here:

 

##############################################################################################
function upload($user_id,$fileid)
##############################################################################################
{
  global $db;
  $maxsize = $_POST['MAX_FILE_SIZE'];
  if(is_uploaded_file($_FILES['userfile']['tmp_name'][$fileid])) 
  {
        if($_FILES['userfile']['size'][$fileid] < $maxsize)
        {
          $imgData = addslashes(file_get_contents($_FILES['userfile']['tmp_name'][$fileid]));
          $size = getimagesize($_FILES['userfile']['tmp_name'][$fileid]);
          
          $sql_query = "SELECT * FROM user_images WHERE image_id = '999999999999999999'";
                 
          if(mysqli_num_rows(mysqli_query($db, $sql_query)) > 0)
          {
            $sql_query = "UPDATE user_images SET image_type = '{$size['mime']}', image = '{$imgData}', image_size = '{$size[3]}', image_name = '{$_FILES['userfile']['name'][$fileid]}' WHERE image_id = '$user_id'";
          }
          else
          {
            $sql_query = "INSERT INTO user_images ( image_type ,image, image_size, image_name) VALUES ('{$size['mime']}', '{$imgData}', '{$size[3]}', '{$_FILES['userfile']['name'][$fileid]}')";
          }
          if(!mysqli_query($db,$sql_query)) { echo '<div class="error">Unable to upload file</div>'; }
          else { $return = mysqli_insert_id($db); }
        }
        else 
        {
          echo
          '<div>File exceeds the Maximum File limit.  Skipping uploading of image.</div>
          <div>Maximum File limit is '.$maxsize.'</div>
          <div>File '.$_FILES['userfile']['name'][$fileid].' is '.$_FILES['userfile']['size'][$fileid].' bytes</div>
          <hr />';
        }
  }
  return $return;
}//END - upload()


##############################################################################################
function imageResize($width, $height, $target) 
##############################################################################################
{
  //protect function if image sizes not set (i.e. image doesn't exist in database)
  if((!isset($width)) || (!isset($height)))
  { $width = '1'; $height = '1'; }
  //if trying to resize an image that is much smaller than target...essentially do nothing and keep the original sizing
  elseif(($width < $target) && ($height < $target))
  { }
  else
  {
    //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 in html image tag format...
  return "width=\"$width\" height=\"$height\"";
}//END - imageResize


##############################################################################################

 

 

Link to comment
https://forums.phpfreaks.com/topic/216148-images-not-reducing-in-size/
Share on other sites

I've found this function on another website:

 

Would this sort of thing work ? (if i changed the names to what I already have?)

 

 // Resize the original image
    $imageResized = imagecreatetruecolor($new_width, $new_height);
    $imageTmp     = imagecreatefromjpeg ($originalImage);
    imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
?>

As final step we returns with the new image. The complete resize function looks like this:

<?php
function resizeImage($originalImage,$toWidth,$toHeight){
    
    // Get the original geometry and calculate scales
    list($width, $height) = getimagesize($originalImage);
    $xscale=$width/$toWidth;
    $yscale=$height/$toHeight;
    
    // Recalculate new size with default ratio
    if ($yscale>$xscale){
        $new_width = round($width * (1/$yscale));
        $new_height = round($height * (1/$yscale));
    }
    else {
        $new_width = round($width * (1/$xscale));
        $new_height = round($height * (1/$xscale));
    }

    // Resize the original image
    $imageResized = imagecreatetruecolor($new_width, $new_height);
    $imageTmp     = imagecreatefromjpeg ($originalImage);
    imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

    return $imageResized;
}

That should work, but you might want to check by file type also:

 

function resizeImage($originalImage,$toWidth,$toHeight){
    
    // Get the original geometry and calculate scales
    list($width, $height, $type) = getimagesize($originalImage);
    $xscale=$width/$toWidth;
    $yscale=$height/$toHeight;
    
    // Recalculate new size with default ratio
    if ($yscale>$xscale){
        $new_width = round($width * (1/$yscale));
        $new_height = round($height * (1/$yscale));
    }
    else {
        $new_width = round($width * (1/$xscale));
        $new_height = round($height * (1/$xscale));
    }

    // Resize the original image
    $imageResized = imagecreatetruecolor($new_width, $new_height);
switch ($type) {
	case "1": //gif image
		$imageTmp     = imagecreatefromgif($originalImage);
		break;
	case "2": //jpg image
		$imageTmp     = imagecreatefromjpeg ($originalImage);
		break;
	case "3": //png image
		$imageTmp     = imagecreatefrompng ($originalImage);
		break;
}
    imagecopyresampled($imageResized, $imageTmp, 0, 0, 0, 0, $new_width, $new_height, $width, $height);

    return $imageResized;
}

Hmmm, this caused errors with varible names on lots of other pages...

 

Does anyone have a way for me to actually just edit the (semi) working code to reduce the image sizes properly without changing any of the names that will cause the rest of the site to malfunction?

to be honest... I'm totally lost!

 

The script was written for me - but I soon realised that the images were not being reduced - just resized so they are taking up way too much space on the server and the pop ups are too big as well...

 

Ideally, I need to keep the script roughly the same as I really dont have the knowledge to change things around that much...

 

You can see an example of how the page looks at the moment, and see that the pops are really big.

 

http://www.viewmykitchen.com/viewprofile.php?id=13

 

 

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.