function set_reasonable_image_size($image, $filename) { global $CONF; list($width, $height) = getimagesize($filename); if ($width <= $CONF['user_temp_avatar_max_width'] && $height <= $CONF['user_temp_avatar_max_height']) return $image; $size = resize_dimensions($CONF['user_temp_avatar_max_width'],$CONF['user_temp_avatar_max_height'],$width,$height); $fwidth = $size['width']; $fheight = $size['height']; $thumb = imagecreatetruecolor($fwidth, $fheight); setTransparency($thumb,$image); $index = imagecolorexact($thumb, 255, 255, 255); imagecolortransparent($thumb, $index); imagecopyresampled($thumb, $image, 0, 0, 0, 0, $fwidth, $fheight, $width, $height); imagepng($thumb,$filename); return $thumb; }
/** * resize_image * * resizes images to a given width and height. if * width is omitted height will be auto calculated * caching is also performed if cache is set to * true * * @params GDResource $image * @params int $width * @params int $height optional * @params bool $cache optional * @params string $path * @return GDResource */ function resize_image($image, $width, $height = false, $cache = false, $path = false) { $name = 'RESIZE' . $width . $height; if ($cache && cache_exists_image($name, $path)) { return cache_get_image($name, $path); } $old_width = imagesx($image); $old_height = imagesy($image); if ($height == false) { // calculate height list($width, $height) = resize_dimensions($width, $width, $old_width, $old_height); } // resize image $new_image = imagecreatetruecolor($width, $height); imagecopyresampled($new_image, $image, 0, 0, 0, 0, $width, $height, $old_width, $old_height); if ($cache) { // cache the image cache_image($name, $new_image, $path); } return $new_image; }