Jump to content


Photo

resizing images


  • Please log in to reply
4 replies to this topic

#1 gerkintrigg

gerkintrigg
  • Members
  • PipPipPip
  • Magician
  • 828 posts
  • LocationBristol, UK
  • Age:37

Posted 09 August 2006 - 01:27 PM

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:

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;

}
Thanks in advance for any help you might be able to give.
Neil Trigger - http://www.ghostlypublishing.co.uk - Ghostly Publishing - Children's Fantasy Books

#2 shocker-z

shocker-z
  • Members
  • PipPipPip
  • Advanced Member
  • 864 posts
  • LocationNottingham

Posted 09 August 2006 - 01:38 PM

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
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

#3 gerkintrigg

gerkintrigg
  • Members
  • PipPipPip
  • Magician
  • 828 posts
  • LocationBristol, UK
  • Age:37

Posted 09 August 2006 - 02:28 PM

er... no. I tried it, but no cigar.
Neil Trigger - http://www.ghostlypublishing.co.uk - Ghostly Publishing - Children's Fantasy Books

#4 HeyRay2

HeyRay2
  • Members
  • PipPipPip
  • Advanced Member
  • 223 posts

Posted 09 August 2006 - 04:39 PM

Add this code:

	// 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); 
  	}

After this line:

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

Let us know if that works... ;)

#5 litebearer

litebearer
  • Members
  • PipPipPip
  • Advanced Member
  • 2,357 posts
  • Locationwhite lake michigan

Posted 09 August 2006 - 04:46 PM

Might be of some use...

http://www.nstoia.co...esize/index.php

Lite...

all the brothers were valiant!

[br][br]The truely intelligent people are not those who create the dots; rather they are they ones with the ability to connect the dots into a coherent picture




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users