/** * Proprtionally crop and resize an image file * * @param string $format expect jpg, jpeg, gif, or png * @param string $filein path to read image file * @param string $fileout path to write converted image file * @param int $imagethumbsize_w width for conversion * @param int $imagethumbsize_h height for conversion * @param int $red RGB red value for imagecolorallocate() * @param int $green RGB green for imagecolorallocate() * @param int $blue RGB blue for imagecolorallocate() */ static function resize($format, $filein, $fileout, $imagethumbsize_w, $imagethumbsize_h, $red = 255, $green = 255, $blue = 255) { $format = strtolower($format); if ($format == 'jpg') $format = 'jpeg'; // fix stupid mistake if (!($format == 'jpeg' || $format == 'gif' || $format == 'png')) { throw new Exception('Invalid image format'); } $func = 'imagecreatefrom' . $format; $image = $func($filein); $width = $imagethumbsize_w; $height = $imagethumbsize_h; list($width_orig, $height_orig) = getimagesize($filein); if ($width_orig < $height_orig) { $height = ($imagethumbsize_w / $width_orig) * $height_orig; } else { $width = ($imagethumbsize_h / $height_orig) * $width_orig; } //if the width is smaller than supplied thumbnail size if ($width < $imagethumbsize_w) { $width = $imagethumbsize_w; $height = ($imagethumbsize_w/ $width_orig) * $height_orig; } //if the height is smaller than supplied thumbnail size if ($height < $imagethumbsize_h) { $height = $imagethumbsize_h; $width = ($imagethumbsize_h / $height_orig) * $width_orig; } $thumb = imagecreatetruecolor($width , $height); $bgcolor = imagecolorallocate($thumb, $red, $green, $blue); ImageFilledRectangle($thumb, 0, 0, $width, $height, $bgcolor); imagealphablending($thumb, true); imagecopyresampled($thumb, $image, 0, 0, 0, 0, $width, $height, $width_orig, $height_orig); $thumb2 = imagecreatetruecolor($imagethumbsize_w , $imagethumbsize_h); // true color for best quality $white = imagecolorallocate($thumb2, 255, 255, 255); ImageFilledRectangle($thumb2, 0, 0, $imagethumbsize_w , $imagethumbsize_h , $white); imagealphablending($thumb2, true); $w1 =($width/2) - ($imagethumbsize_w/2); $h1 = ($height/2) - ($imagethumbsize_h/2); imagecopyresampled($thumb2, $thumb, 0, 0, $w1, $h1, $imagethumbsize_w , $imagethumbsize_h ,$imagethumbsize_w, $imagethumbsize_h); $func = 'image' . $format; $func($thumb2, $fileout); }