Jump to content

resizing images


gerkintrigg

Recommended Posts

Hi everyone!
I'd like to find out how to change the current script I'm using. It resizes images fine, but rather than setting them to absolute figures, I'd like to work out a way whereby I can set maximum figures.

So for example, rather than saying "it MUST be 400 pixels wide by 20 Px high" I'd like to say: "it can be a maximum of 400 px wide by 20 px high, but the proportions must stay the same.

This is the script I have so far:

[code]function makeimage($filename,$newfilename,$path,$newwidth,$newheight) {

//SEARCHES IMAGE NAME STRING TO SELECT EXTENSION (EVERYTHING AFTER . )
$image_type = strstr($filename, '.');

//SWITCHES THE IMAGE CREATE FUNCTION BASED ON FILE EXTENSION
switch($image_type) {
case '.jpg':
$source = imagecreatefromjpeg($filename);
break;
case '.png':
$source = imagecreatefrompng($filename);
break;
case '.gif':
$source = imagecreatefromgif($filename);
break;
default:
echo("Error Invalid Image Type");
die;
break;
}

//CREATES THE NAME OF THE SAVED FILE
$file = $newfilename . $filename;

//CREATES THE PATH TO THE SAVED FILE
$fullpath = $path . $file;

//FINDS SIZE OF THE OLD FILE
list($width, $height) = getimagesize($filename);

//CREATES IMAGE WITH NEW SIZES
$thumb = imagecreatetruecolor($newwidth, $newheight);

//RESIZES OLD IMAGE TO NEW SIZES
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

//SAVES IMAGE AND SETS QUALITY || NUMERICAL VALUE = QUALITY ON SCALE OF 1-100
imagejpeg($thumb, $fullpath, 60);

//CREATING FILENAME TO WRITE TO DATABSE
$filepath = $fullpath;

//RETURNS FULL FILEPATH OF IMAGE ENDS FUNCTION
return $filepath;

}[/code]Thanks in advance for any help you might be able to give.
Link to comment
Share on other sites

Add this code:

[code=php:0]
// Set the maximum width and height
$max_width = 400;
$max_height = 20;

// Check if either dimension is over the maximum
if (($height > $max_height) || ($width > $max_width))
  {
  // Set a size factor variable, depending on which
  // dimension is greater
if ($height > $width)
    {
      $sizefactor = (double) ($max_height / $height);
    }
    else
    {
      $sizefactor = (double) ($max_width / $width) ;
    }

    $newwidth = (int) ($image_width * $sizefactor);
  $newheight = (int) ($image_height * $sizefactor);
  }
[/code]

After this line:

[code=php:0]
//FINDS SIZE OF THE OLD FILE
list($width, $height) = getimagesize($filename);
[/code]

Let us know if that works... ;)
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.