Okies, ive managed to create a better image resize function. The one i found before was pretty poor if not kinda similar. Anyways, hope this helps.
To call the function you need some basic details.
You need the files temporary name (from $_FILES), an upload directory, a filename (i suggest randomly creating them, ill post an article soon on that), and your desired width and heights (ive used 250).
NOTE: Height may vary slightly to ensure resizing is to scale.
$filetmpname = $_FILES['file']['tmp_name'];
$uploaddir = './images/';
$filename = 'yourfilename.jpg';
$width = 250;
$height = 250;
$resize = resizePic($filetmpname, $width, $height, $filename, $uploaddir);
The function looks like this:
function resizePic($filetmpname, $allowedwidth, $allowedheight, $filename, $uploaddir)
{
// Create an image for us to resize
$src = imagecreatefromjpeg($filetmpname);
//If image creation fails, return failse//
//This step is not actually really necessary, but I do it for//
//error provention//
if (!$src)
return false;
// Get dimensions of origional image
list($width,$height)=getimagesize($filetmpname);
//Maintains size ratio. Does not squash or squeeze image.//
$newwidth=$allowedwidth;
$newheight=($height/$width)*$allowedheight;
$tmp=imagecreatetruecolor($newwidth,$newheight);
//now, actually resize image using dimensions just worked out//
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
//generate filename using given values. Write image to disk from tmp location//
$filename = $uploaddir.$filename;
imagejpeg($tmp,$filename,100);
imagedestroy($src);
imagedestroy($tmp);
}
Hope this helps. Any improvements to it, let me know.
Posted by OLLIE at 12:52pm
No comments yet. Be the first to add one!