exhaler Posted February 10, 2010 Share Posted February 10, 2010 Hi, i'm currently using this script to resize and crop images but it doesn't work proportionally, also how can i keep the transparency. function ResizeProportionalImage($nw, $nh, $source, $stype, $dest="") { $size = getimagesize($source); $w = $size[0]; $h = $size[1]; switch ($stype) { case IMAGETYPE_GIF: $simg = imagecreatefromgif($source); break; case IMAGETYPE_JPEG: $simg = imagecreatefromjpeg($source); break; case IMAGETYPE_PNG: $simg = imagecreatefrompng($source); break; } $dimg = imagecreatetruecolor($nw, $nh); $wm = $w/$nw; $hm = $h/$nh; $h_height = $nh/2; $w_height = $nw/2; if($w > $h) { /*$adjusted_width = $w / $hm; $half_width = $adjusted_width / 2; $int_width = $half_width - $w_height; imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);*/ imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h); } else if(($w < $h) || ($w == $h)) { $adjusted_height = $h / $wm; $half_height = $adjusted_height / 2; $int_height = $half_height - $h_height; imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h); } else { imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h); } if($dest != "") imagejpeg($dimg,$dest,100); else return $dimg; } if someone has a better way of resizing and cropping image proportionally please post it thanks Quote Link to comment Share on other sites More sharing options...
Deoctor Posted February 10, 2010 Share Posted February 10, 2010 try using this one <?php resize("./", "http://images.wsdot.wa.gov/nwflow/flowmaps/sysvert.gif", "538", "./"); function resize($cur_dir, $cur_file, $newwidth, $output_dir) { $dir_name = $cur_dir; $olddir = getcwd(); $dir = opendir($dir_name); $filename = $cur_file; $format='image/gif'; if(preg_match("/.jpg/i", "$filename")) { $format = 'image/jpeg'; } if (preg_match("/.gif/i", "$filename")) { $format = 'image/gif'; } if(preg_match("/.png/i", "$filename")) { $format = 'image/png'; } if($format!='') { list($width, $height) = getimagesize($filename); $newheight=$height*$newwidth/$width; switch($format) { case 'image/jpeg': $source = imagecreatefromjpeg($filename); break; case 'image/gif'; $source = imagecreatefromgif($filename); break; case 'image/png': $source = imagecreatefrompng($filename); break; } $dimg = imagecreatetruecolor(640,538); imagealphablending($dimg, false); $source = @imagecreatefromgif("$filename"); imagecopyresized($dimg, $source, 0,0,0,438, $newwidth, $newheight, $width, $height); $filename="$output_dir/test1.gif"; @imagegif($dimg,$filename); } } ?> Quote Link to comment Share on other sites More sharing options...
exhaler Posted February 10, 2010 Author Share Posted February 10, 2010 thx ym_chaitu, but its not resizing proportionally and handling transparency Quote Link to comment Share on other sites More sharing options...
Deoctor Posted February 10, 2010 Share Posted February 10, 2010 for proportional resizing u need to use an external application like image magick or some thing.. http://php.net/manual/en/book.imagick.php Quote Link to comment Share on other sites More sharing options...
jl5501 Posted February 10, 2010 Share Posted February 10, 2010 I would suggest you look at phpthumb http://phpthumb.sourceforge.net/ Quote Link to comment Share on other sites More sharing options...
Deoctor Posted February 10, 2010 Share Posted February 10, 2010 u can even check this script http://www.nashruddin.com/Resize_Image_to_Different_Aspect_Ratio_on_the_fly or else try with this script <?php header ("Content-type: image/jpeg"); /* JPEG / PNG Image Resizer Parameters (passed via URL): img = path / url of jpeg or png image file percent = if this is defined, image is resized by it's value in percent (i.e. 50 to divide by 50 percent) w = image width h = image height constrain = if this is parameter is passed and w and h are set to a size value then the size of the resulting image is constrained by whichever dimension is smaller Requires the PHP GD Extension Outputs the resulting image in JPEG Format By: Michael John G. Lopez - www.sydel.net Filename : imgsize.php Modified by: Nash - http://nashruddin.com Added: - displays default image if the requested image doesn't exist - hide image path from user */ $path = '/path/to/your/images'; // change to where your images reside $img = $path . '/' . $_GET['img']; if (file_exists($img)) { $percent = $_GET['percent']; $constrain = $_GET['constrain']; $w = $_GET['w']; $h = $_GET['h']; } else { $img = $path . '/default.jpg'; // change with your default image $percent = 100; } // get image size of img $x = @getimagesize($img); // image width $sw = $x[0]; // image height $sh = $x[1]; if ($percent > 0) { // calculate resized height and width if percent is defined $percent = $percent * 0.01; $w = $sw * $percent; $h = $sh * $percent; } else { if (isset ($w) AND !isset ($h)) { // autocompute height if only width is set $h = (100 / ($sw / $w)) * .01; $h = @round ($sh * $h); } elseif (isset ($h) AND !isset ($w)) { // autocompute width if only height is set $w = (100 / ($sh / $h)) * .01; $w = @round ($sw * $w); } elseif (isset ($h) AND isset ($w) AND isset ($constrain)) { // get the smaller resulting image dimension if both height // and width are set and $constrain is also set $hx = (100 / ($sw / $w)) * .01; $hx = @round ($sh * $hx); $wx = (100 / ($sh / $h)) * .01; $wx = @round ($sw * $wx); if ($hx < $h) { $h = (100 / ($sw / $w)) * .01; $h = @round ($sh * $h); } else { $w = (100 / ($sh / $h)) * .01; $w = @round ($sw * $w); } } } $im = @ImageCreateFromJPEG ($img) or // Read JPEG Image $im = @ImageCreateFromPNG ($img) or // or PNG Image $im = @ImageCreateFromGIF ($img) or // or GIF Image $im = false; // If image is not JPEG, PNG, or GIF if (!$im) { // We get errors from PHP's ImageCreate functions... // So let's echo back the contents of the actual image. readfile ($img); } else { // Create the resized image destination $thumb = @ImageCreateTrueColor ($w, $h); // Copy from image source, resize it, and paste to image destination @ImageCopyResampled ($thumb, $im, 0, 0, 0, 0, $w, $h, $sw, $sh); // Output resized image @ImageJPEG ($thumb); } ?> Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.