sooty2006 Posted February 28, 2008 Share Posted February 28, 2008 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 More sharing options...
justsomeguy Posted February 28, 2008 Share Posted February 28, 2008 any help will be much helpfull thanksEloquent.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 More sharing options...
sooty2006 Posted February 28, 2008 Author Share Posted February 28, 2008 thanks alot mate you have solved my problem and it works !! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.