I am working on a project in which i need to make thumbed images and maintain aspect ratio based on a set width and height. After searching the web i found examples that only helped me for one or the other need . So i combined both fucntionality into one, here is my function(this uses GD library to manipulate images):
Code:function make_thumb($src, $thumbdirectory,$thumbedfile, $desired_width,$desired_height) { /* read the source image */ $source_image = imagecreatefromjpeg($src); $width = imagesx($source_image); $height = imagesy($source_image); //calculate aspect ratio if ($width > $height) { $percentage = ($desired_width / $width); } else { $percentage = ($desired_height / $height); } $desired_width = round($width * $percentage); $desired_height = round($height * $percentage); /* create a new, "virtual" image */ $virtual_image = imagecreatetruecolor($desired_width, $desired_height); /* copy source image at a resized size */ imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height); //chk if destination exist and make it if not if(!is_dir($thumbdirectory)){ mkdir($thumbdirectory, 0755); chmod($thumbdirectory, 0755); /* create the physical thumbnail image to its destination */ imagejpeg($virtual_image, $thumbedfile); } else { chmod($thumbdirectory, 0755); /* create the physical thumbnail image to its destination */ imagejpeg($virtual_image, $thumbedfile); } }