Php Image Display Script

I think robmiller may have one of his own, if he is willing to share :)

If not, I'll try to knock one up tonight, as this isn't an amazingly complex script but it can be finickity and I haven't got access to a parser atm. :)
 
You can make it pay attention to aspect ratio, if you like - that is, given an image 200px x 100px, and a constraint size of 100px x 100px, it would resize it to 100px x 50px as opposed to "squashing" it to 100px x 100px.

Old code, so it probably needs tidying up, but it works well enough:

Code:
function resize($if, $of = null, $quality = 65, $new_w = 200, $new_h = 200) {
	
	if(eregi('(jpg|jpeg)$', $if)) {
		$type = 'jpg';
		$im = imagecreatefromjpeg($if);
	}
	elseif(eregi('png$', $if)){
		$type = 'png';
		$im = imagecreatefrompng($if);
	}
	elseif(eregi('gif$', $if)){
		$type = 'gif';
		$im = imagecreatefromgif($if);
	}
	
	$old_x = imagesx($im);
	$old_y = imagesy($im);
	
	if($old_x > $new_w || $old_y > $new_h) {
		
		if ($old_x > $old_y) {
			$thumb_w = $new_w;
			$thumb_h = $old_y * ($new_h / $old_x);
		}
		if ($old_x < $old_y) {
			$thumb_w = $old_x * ($new_w / $old_y);
			$thumb_h = $new_h;
		}
		if ($old_x == $old_y) {
			$thumb_w = $new_w;
			$thumb_h = $new_h;
		}
		
		$dst_img = imagecreatetruecolor($thumb_w, $thumb_h);
		imagecopyresampled($dst_img, $im, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y);
		
		$im = $dst_img;
		
	}
	
	if($of && !is_writable(dirname($of))) {
		die('Whoops - your album directory is not writable. Please fix this, or I won\'t be able to generate any thumbnails!');
	}
	
	if($type == 'jpg'){
		imagejpeg($im, $of, $quality); 
	} 
	elseif($type == 'png') {
		imagepng($im, $of); 
	}
	else {
		imagegif($im, $of);
	}
	
	@imagedestroy($dst_img); 
	@imagedestroy($im);
	
}
 
I can't get the to work. It fails on line 19. "$old_x = imagesx($im);" :confused:

Call to undefined function: imagesx() in C:\nuke\html\foto3.php on line 19.

Do I need some require's before I can run this?
 
Last edited:
I'm thinking its more to do with not getting a filename into it.

To simplify it for testing, I've removed the function like this and just populated the variables.

Code:
<?php
	
	$if = './1.jpg';
	$of = null;
	$quality = 65;
	$new_w = 200;
	$new_h = 200;
			
		
	if(eregi('(jpg|jpeg)$', $if)) {
		$type = 'jpg';
		$im = imagecreatefromjpeg($if);
	}
	elseif(eregi('png$', $if)){
		$type = 'png';
		$im = imagecreatefrompng($if);
	}
	elseif(eregi('gif$', $if)){
		$type = 'gif';
		$im = imagecreatefromgif($if);
	}
	
	$old_x = imagesx($im);
	$old_y = imagesy($im);
	
	if($old_x > $new_w || $old_y > $new_h) {
		
		if ($old_x > $old_y) {
			$thumb_w = $new_w;
			$thumb_h = $old_y * ($new_h / $old_x);
		}
		if ($old_x < $old_y) {
			$thumb_w = $old_x * ($new_w / $old_y);
			$thumb_h = $new_h;
		}
		if ($old_x == $old_y) {
			$thumb_w = $new_w;
			$thumb_h = $new_h;
		}
		
		$dst_img = imagecreatetruecolor($thumb_w, $thumb_h);
		imagecopyresampled($dst_img, $im, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_x, $old_y);
		
		$im = $dst_img;
		
	}
	
	if($of && !is_writable(dirname($of))) {
		die('Whoops - your album directory is not writable. Please fix this, or I won\'t be able to generate any thumbnails!');
	}
	
	if($type == 'jpg'){
		imagejpeg($im, $of, $quality); 
	} 
	elseif($type == 'png') {
		imagepng($im, $of); 
	}
	else {
		imagegif($im, $of);
	}
	
	@imagedestroy($dst_img); 
	@imagedestroy($im);
	

?>

I'm guessing, does your code actually create a smaller image. The code I've used just resize's it. I'd much rather persevere with the code you've posted if it actually shrinks the image server side.
 
Last edited:
Setting $of to null will output the image, and setting $of to a filename will save it to a file. Obviously you can set $if and $of to the same filename and it wil resize the actual image :)
 
Back
Top Bottom