Jump to content

images size?


sooty2006

Recommended Posts

hey all,i have a small problem atm i am creating a website an users have profiles users can type in the link to the image in there profile but some pictures are too bigand the table goes all wrong !is there anyway that before the image is shown if its bigger than 200px X 200px it shrinks it any help will be much helpfull thanks

Link to comment
Share on other sites

any help will be much helpfull thanks
Eloquent.Here's a script you can use. This will resize an image that you give the path for, it should be a path the script can reach and can be either a local path or a URL. If it's a URL it needs to be an absolute URL. It takes parameters for the image filename, max height, and max width. You don't need to specify both the width and height, you can only specify one, but it will resize the image to make sure it is smaller then both the width and height you give, if you give them. First it will check for width and resize if the width is too big, then it will check height and resize again if the height is still too big. You would link to it like this:<img src="resize_img.php?w=100&h=100&image=http://www.w3schools.com/images/logo_new2.jpg">The image either needs to be JPG, GIF, or PNG.
<?php$image = "";$max_w = $max_h = 0;if (isset($_GET['image']))  $image = trim($_GET['image']);if ($image == "")  exit("no image");if (isset($_GET['w']))  $max_w = intval($_GET['w']);if (isset($_GET['h']))  $max_h = intval($_GET['h']);$ext = array_pop(explode(".", $image));switch(strtolower($ext)){  case 'jpg':  case 'jpeg':	$im = imagecreatefromjpeg($image);	break;  case 'gif':	$im = imagecreatefromgif($image);	break;  case 'png':	$im = imagecreatefrompng($image);	break;  default:	exit("unsupported type");}$w = imagesx($im);$h = imagesy($im);if ($max_w && $w > $max_w){  $ratio = $max_w / $w;  $new_h = round($ratio * $h);  $tmp = imagecreatetruecolor($max_w, $new_h);  imagecopyresampled($tmp, $im, 0, 0, 0, 0, $max_w, $new_h, $w, $h);  $im = $tmp;  unset($tmp);  $w = imagesx($im);  $h = imagesy($im);}if ($max_h && $h > $max_h){  $ratio = $max_h / $h;  $new_w = round($ratio * $w);  $tmp = imagecreatetruecolor($new_w, $max_h);  imagecopyresampled($tmp, $im, 0, 0, 0, 0, $new_w, $max_h, $w, $h);  $im = $tmp;  unset($tmp);}header('Content-type: image/png');imagepng($im);?>

Link to comment
Share on other sites

Archived

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

×
×
  • Create New...