ckrudelux Posted July 16, 2009 Share Posted July 16, 2009 Need some help I want to resize images for example I have a image I want to upload that is 180px * 180px but I want it to be 80px * 80px then I save it to the server image directory. Link to comment Share on other sites More sharing options...
justsomeguy Posted July 16, 2009 Share Posted July 16, 2009 function resize_image($opts){ $src = isset($opts['source']) ? $opts['source'] : ''; $dest = isset($opts['dest']) ? $opts['dest'] : ''; $w = isset($opts['w']) ? intval($opts['w']) : 0; $h = isset($opts['h']) ? intval($opts['h']) : 0; if ($src == '') { return; } if ($w == 0 && $h == 0) { return; } if ($dest == '') $dest = $src; // resize in place // open the image $ext = strtolower(array_pop(explode('.', $src))); switch ($ext) { case 'jpg': case 'jpeg': $i = imagecreatefromjpeg($src); break; case 'gif': $i = imagecreatefromgif($src); break; case 'png': $i = imagecreatefrompng($src); break; default: return; } $new_w = imagesx($i); $new_h = imagesy($i); if (($w != 0 && $new_w < $w && $h == 0) || ($w == 0 && $h != 0 && $new_h < $h) || ($w != 0 && $new_w < $w && $h != 0 && $new_h < $h)) { // image is small enough if ($dest != $src) copy($src, $dest); return; } // determine new size if ($w != 0 && $new_w > $w) { $new_h = ($w / $new_w) * $new_h; $new_w = $w; } if ($h != 0 && $new_h > $h) { $new_w = ($h / $new_h) * $new_w; $new_h = $h; } // resize $new = imagecreatetruecolor($new_w, $new_h); imagecopyresampled($new, $i, 0, 0, 0, 0, $new_w, $new_h, imagesx($i), imagesy($i)); imagedestroy($i); // save the image switch ($ext) { case 'jpg': case 'jpeg': imagejpeg($new, $dest); break; case 'gif': imagegif($new, $dest); break; case 'png': imagepng($new, $dest); break; } imagedestroy($new);} ex: resize_image(array( 'source' => '/path/to/source.jpg', 'dest' => '/path/to/save.jpg', // optional, leave off to overwrite source 'w' => 80, 'h' => 80)); Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.