Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GD Image Resizer comments 1

Status
Not open for further replies.

jasc2k

Programmer
Nov 2, 2005
113
GB
Hi there,
Sorry I am still a newb at PHP at would like someone to just have a look at my image resize function that I have built inside an uploading class using several tutorials and brain cells. Though the script does work after seeing more examples I wonder if I have over done this little project.

Basically the function needs to determine if the image passed to it is smaller than the parameters set it does nothing otherwise it will resize or rescale (probably easier if I get straight to the code!)

To call the funtion:
Code:
					$this->resize_image($this->path . $this->file["name"], 50, 50, null, true, 100, $this->path."s_".$this->file['raw_name'].$this->file["extention"]);

Then the functions the first will find the file extension to work with then the actual resize function:
Code:
	function file_extension($filename){
	
		$extension = substr( $filename, ( strrpos($filename, '.') + 1 ) ) ;
		$extension = strtolower( $extension ) ;
		
		return $extension;
	}
	
	function resize_image($image_name, $max_width = null, $max_height = null, $scale = null, $relscale = true, $quality = 100, $thumb_name){
	
		$img = null;
		$ext = $this->file_extension($image_name);

		if ($ext == 'jpg' || $ext == 'jpeg') {
		    $img = @imagecreatefromjpeg($image_name);
		} else if ($ext == 'png') {
		    $img = @imagecreatefrompng($image_name);
		} else if ($ext == 'gif') {
		    $img = @imagecreatefromgif($image_name);
		}
		
		// If an image was successfully loaded, test the image for size
		if ($img) {
		    // Get image size and scale ratio
		    list($oldwidth, $oldheight) = getimagesize($image_name);
			
			if($max_width >= $oldwidth && $max_height >= $oldheight){ // This should stop upscaling
					$width = $oldwidth;
					$height = $oldheight;
			} else {
				if($relscale == true && ($max_width || $max_height)){ // Supply bounds, scale w/o loss to ratio
					if($oldheight > $oldwidth || !$max_width){ 
						$sizefactor = (double) ($max_height / $oldheight);
						$width = (int) ($oldwidth * $sizefactor);
						$height = (int) ($oldheight * $sizefactor);
					} 
					else if($oldheight < $oldwidth || !$max_height){
						$sizefactor = (double) ($max_width / $oldwidth) ;
						$width = (int) ($oldwidth * $sizefactor);
						$height = (int) ($oldheight * $sizefactor);
					} else { // If the image has a ratio of 1, aka Height and Width ==, just do a generic resize
						$width = $max_width;
						$height = $max_height;
					}
				
				} elseif(  $max_width && $max_height && $relscale == false ){ // Max Width And Height are new dimensions, just do a generic resize  
					$width = $max_width;
					$height = $max_height;
				} elseif( $scale && !$max_width || !$max_height ){ // Scale Provided And No Max Width/Height
					$width = (int) ($oldwidth * ( $scale / 100 ));
					$height = (int) ($oldheight * ( $scale / 100 ));
				} else {
					return false; // No Dimensions Specified BUT if this function were to be accidentally called...
				}
			}
			
			$tmp_img = imagecreatetruecolor($width, $height);
			
		    // Copy and resize old image into new image
			imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $width, $height, $oldwidth, $oldheight);
		        
			imagedestroy($img);
			$img = $tmp_img;
			
			if($thumb_name == ""){
				$thumb_name = $image_name;
			}
		    
			if ($ext == 'png') {
			    imagepng($img,$thumb_name);
			} elseif($ext == 'gif'){
			    imagegif($img,$thumb_name);
			} else {
			    imagejpeg($img,$thumb_name,$quality);
			}     
		}
	}

I hope this makes some sense!
Thanks
 
Hi

While you are using the [tt]getimagesize()[/tt] function anyway, I would use that instead of playing with the file extension :
Code:
[b]list[/b][teal]([/teal][navy]$oldwidth[/navy][teal],[/teal] [navy]$oldheight[/navy][teal],[/teal] [navy]$imagetype[/navy][teal])[/teal] [teal]=[/teal] [COLOR=darkgoldenrod]getimagesize[/color][teal]([/teal][navy]$image_name[/navy][teal]);[/teal]

[b]switch[/b] [teal]([/teal][navy]$imagetype[/navy][teal])[/teal] [teal]{[/teal]
  [b]case[/b] IMAGETYPE_GIF[teal]:[/teal] [navy]$img[/navy] [teal]=[/teal] [navy]@imagecreatefromgif[/navy][teal]([/teal][navy]$image_name[/navy][teal]);[/teal] [b]break[/b][teal];[/teal]
  [b]case[/b] IMAGETYPE_JPEG[teal]:[/teal] [navy]$img[/navy] [teal]=[/teal] [navy]@imagecreatefromjpeg[/navy][teal]([/teal][navy]$image_name[/navy][teal]);[/teal] [b]break[/b][teal];[/teal]
  [b]case[/b] IMAGETYPE_PNG[teal]:[/teal] [navy]$img[/navy] [teal]=[/teal] [navy]@imagecreatefrompng[/navy][teal]([/teal][navy]$image_name[/navy][teal]);[/teal] [b]break[/b][teal];[/teal]
  [b]default[/b][teal]:[/teal] [b]die[/b][teal]([/teal][green][i]'I handle only GIF, JPEG and PNG images.'[/i][/green][teal]);[/teal]
[teal]}[/teal]

[gray]/* ... */[/gray]

[b]switch[/b] [teal]([/teal][navy]$imagetype[/navy][teal])[/teal] [teal]{[/teal]
  [b]case[/b] IMAGETYPE_GIF[teal]:[/teal] [COLOR=darkgoldenrod]imagegif[/color][teal]([/teal][navy]$img[/navy][teal],[/teal][navy]$thumb_name[/navy][teal]);[/teal] [b]break[/b][teal];[/teal]
  [b]case[/b] IMAGETYPE_JPEG[teal]:[/teal] [COLOR=darkgoldenrod]imagejpeg[/color][teal]([/teal][navy]$img[/navy][teal],[/teal][navy]$thumb_name[/navy][teal],[/teal][navy]$quality[/navy][teal]);[/teal] [b]break[/b][teal];[/teal]
  [b]case[/b] IMAGETYPE_PNG[teal]:[/teal] [COLOR=darkgoldenrod]imagepng[/color][teal]([/teal][navy]$img[/navy][teal],[/teal][navy]$thumb_name[/navy][teal]);[/teal] [b]break[/b][teal];[/teal]
[teal]}[/teal]
Note that the image may not be of the type indicated by its extension. Many programs will still handle them. Make your script be one of those.


Feherke.
 
supurb - another thumbs up seems you have all my answers :)
I am always nervous as I think I over complicate some of my code and there is so much to read as I keep adding!

Perhaps you would like to see some of what you have helped with in action?
http://www.theunit46.co.uk/7104/index.php

(Still a working project with no real goal yet! :))
 
almost forgot - thanks I appreciate yoour help again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top