Jump to content

Archived

This topic is now archived and is closed to further replies.

gerkintrigg

resizing images

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.

Share this post


Link to post
Share on other sites
if (($height > 20) or ($width > 400)) {
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
}


Would that not do the job??

Regards
Liam

Share this post


Link to post
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... ;)

Share this post


Link to post
Share on other sites
Might be of some use...

http://www.nstoia.com/toh/technical/imageresize/index.php

Lite...

Share this post


Link to post
Share on other sites

×

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.