Resize Image before Uploading with PHP
Using this example you are able to resize your image before uploading to the server. This is the very most usefull functionality to resize image before user save it to the server.
function resize($file, $imgpath, $width, $height){ /* Get original image x y*/ list($w, $h) = getimagesize($file['tmp_name']); /* calculate new image size with ratio */ $ratio = max($width/$w, $height/$h); $h = ceil($height / $ratio); $x = ($w - $width / $ratio) / 2; $w = ceil($width / $ratio); /* new file name */ $path = $imgpath; /* read binary data from image file */ $imgString = file_get_contents($file['tmp_name']); /* create image from string */ $image = imagecreatefromstring($imgString); $tmp = imagecreatetruecolor($width, $height); imagecopyresampled($tmp, $image, 0, 0, $x, 0, $width, $height, $w, $h); /* Save image */ switch ($file['type']) { case 'image/jpeg': imagejpeg($tmp, $path, 100); break; case 'image/png': imagepng($tmp, $path, 0); break; case 'image/gif': imagegif($tmp, $path); break; default: //exit; break; } return $path; /* cleanup memory */ imagedestroy($image); imagedestroy($tmp); }Now you can call this function anywhere in your site to resize original image to required size like below.
//$imgpath = "Where you want to save your image"; resize($_FILES["image"], $imgpath, 340, 340);
Please support us, Like us on Facebook.
Subscribe to:
Post Comments (Atom)
0 comments:
Post a Comment