/**
 * Resize an image (GDLib)
 *
 * @access public
 * @param   string      Source Filename to resize
 * @param   string      Target file
 * @param   int         New width
 * @return  int         New height (can be autodetected)
 * @return  array       New image size
 */
function serendipity_resize_image_gd($infilename, $outfilename, $newwidth, $newheight = null)
{
    $func = serendipity_functions_gd($infilename);
    if (!is_array($func)) {
        return false;
    }
    $in = $func['load']($infilename);
    $width = imagesx($in);
    $height = imagesy($in);
    if (is_null($newheight)) {
        $newsizes = serendipity_calculate_aspect_size($width, $height, $newwidth, 'width');
        $newwidth = $newsizes[0];
        $newheight = $newsizes[1];
    }
    if (is_null($newwidth)) {
        $newsizes = serendipity_calculate_aspect_size($width, $height, $newheight, 'height');
        $newwidth = $newsizes[0];
        $newheight = $newsizes[1];
    }
    $out = imagecreatetruecolor($newwidth, $newheight);
    /* Attempt to copy transparency information, this really only works for PNG */
    if (function_exists('imagesavealpha')) {
        imagealphablending($out, false);
        imagesavealpha($out, true);
    }
    imagecopyresampled($out, $in, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    @umask(00);
    touch($outfilename);
    // safe_mode requirement
    $func['save']($out, $outfilename, $func['qual']);
    @chmod($outfilename, 0664);
    $out = null;
    $in = null;
    return array($newwidth, $newheight);
}
/**
 * Resize an image (GDLib)
 *
 * @access public
 * @param   string      Source Filename to resize
 * @param   string      Target file
 * @param   int         New width
 * @return  int         New height (can be autodetected)
 * @return  array       New image size
 */
function serendipity_resize_image_gd($infilename, $outfilename, $newwidth, $newheight = null)
{
    $func = serendipity_functions_gd($infilename);
    if (!is_array($func)) {
        return false;
    }
    try {
        // if an image exist that can not be loaded (invalid gif for example), the page shall still be rendered
        $in = $func['load']($infilename);
    } catch (Exception $e) {
        echo 'Could not create thumbnail: ', $e->getMessage(), "\n";
        return false;
    }
    $width = imagesx($in);
    $height = imagesy($in);
    if (is_null($newheight)) {
        $newsizes = serendipity_calculate_aspect_size($width, $height, $newwidth, 'width');
        $newwidth = $newsizes[0];
        $newheight = $newsizes[1];
    }
    if (is_null($newwidth)) {
        $newsizes = serendipity_calculate_aspect_size($width, $height, $newheight, 'height');
        $newwidth = $newsizes[0];
        $newheight = $newsizes[1];
    }
    $out = imagecreatetruecolor($newwidth, $newheight);
    /* Attempt to copy transparency information, this really only works for PNG */
    if (function_exists('imagesavealpha')) {
        imagealphablending($out, false);
        imagesavealpha($out, true);
    }
    imagecopyresampled($out, $in, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
    @umask(00);
    touch($outfilename);
    // safe_mode requirement
    $func['save']($out, $outfilename, $func['qual']);
    @chmod($outfilename, 0664);
    $out = null;
    $in = null;
    return array($newwidth, $newheight);
}