Image

Imagenight_red wrote in Imagephp

Listens: mixed by dj scot project (D I G I T A L L Y - I M P O R T E D - Hard House, Tech House, UK Hard House... DI is in da house!)

Optimization

Here is the problem that I am having. These two scripts run on the server and it creates a larger I mean a lot larger swap file usage, but there is no way that I know of to improve the mysql usage since it is already as low as I can get it.

What I want is a way to improve this code for better speed and less resources.


, rather
 * than scriptname.php?id=. This is to prevent various boards from rejecting
 * the image display URL because it contains variables and could be used in a
 * cross-site scripting (XSS) attack.
*/
function get_id_from_url () {
    $string = $_SERVER['REQUEST_URI'];
    $search_begin = $_SERVER['SCRIPT_NAME'].'/';
    $search_begin_pos = strpos ($string, $search_begin);
    
    if ($search_begin_pos === false) {
    	return false;
    }
    
    $id = substr ($string, ($search_begin_pos + strlen ($search_begin)));
    $id = intval ($id);

    if ($id >= 1) {
        return $id;
    } else {
        return false;
    }
}

?>



This next section is the update code where can I make this better?


close_db();
        exit ('You really suck.');
    }
} else {
    $db->close_db();
    exit ('You suck.');
}

//Here's where the body of the script happens.
//First, check to make sure that user ID exists and correct password
//has been specified for that ID. If authentication succeeds, store
//user's song in DB.
if (authenticate ($id, $pass) && store ($id, $song_1)) {
    $songdata = array('song' => $song_1);

$image_save_filename = $config['images'].$id.".png";

if (!$songdata) {
    $song = 'OH NOS! TEH ERROR!';
} else {
    // $song = $songdata['time']." - Currently playing:\r\n".$songdata['song'];
    $song = $songdata['song'];
}

$text_size = imagettfbbox ($config['font_size'], 0, $config['font_file'], $song); //size, angle, name, string
$tmpl = $config['canvas_name'].$config['canvas_ext'];
$img_size = getimagesize ($tmpl);

if (song_too_long ($text_size[4], $img_size[0], $config['padding'])) {
    //large is still too small, start shrinking string by removing characters and adding
    //elipsis to indicate truncation
    $elipsis = '...';
    $elipsis_length = imagettfbbox ($config['font_size'], 0, $config['font_file'], $elipsis);

    while (($text_size[4] + $elipsis_length[4]) >= ($img_size[0] - $config['padding'])) {
        //while song name is longer than (image width + buffer of 10 pixels), make song 
        //one character shorter
        $song = substr ($song, 0, -1);
        $text_size = imagettfbbox ($config['font_size'], 0, $config['font_file'], $song);
    }
                
    $song .= $elipsis;
    $text_size = imagettfbbox ($config['font_size'], 0, $config['font_file'], $song);
}

$horiz_indent = ($config['padding'] / 2);
$input = imagecreatefrompng ($tmpl);
    
$color = imagecolorclosest ($input, $config['font_color_r'], $config['font_color_g'], $config['font_color_b']);

imagettftext ($input, $config['font_size'], 0, $horiz_indent, $config['text_vertical_offset'], $color, $config['font_file'], $song);
imagepng ($input, $image_save_filename);
imagedestroy($input);

/* song_too_long ()
 *
 * Function returns boolean value. True if (width in pixels of
 * given string + padding) is greater than width in pixels of the canvas,
 * false if not.
*/
function song_too_long ($text_size, $img_size, $padding) {
    if ($text_size >= ($img_size - $padding)) {
        return true;
    } else {
        return false;
    }
}
    echo 'Woohoo! Success!';
    $db->close_db();
} else {
    $db->close_db();
    exit ('Your suckiness sets a new standard in the field of sucking.');
}

/* authenticate ()
 *
 * Check that account for ID specified by user exists; if it does, check that correct
 * password has been specified for that account.
*/
function authenticate ($id, $pass) {
    global $config, $db;

    $user = $db->queryOneRecord ('SELECT pass FROM '.$config['db_table'].' WHERE id='.$id.';');
    
    if ($user) {
        if ($pass == $user['pass']) {
            return true;
        } else {
            return false;
        }
    } else {
        return false;
    }
}

/* store ()
 *
 * Store songname in DB for given user ID.
*/
function store($id, $song_1) {
    global $config, $db;

    if (!isset ($_SERVER['HTTP_USER_AGENT']) ||
    $_SERVER['HTTP_USER_AGENT'] == '') {
        $_SERVER['HTTP_USER_AGENT'] = 'Unknown';
    }

    if (
    $db->query ("UPDATE ".$config['db_table']." ".
    "SET song='".whaddslash ($song_1).
    "',ip='".whaddslash ($_SERVER['REMOTE_ADDR']).
    "',time=".time().
    ",uagent='".whaddslash ($_SERVER['HTTP_USER_AGENT']).
    "',updates=(updates+1)".
    " WHERE id=".$id.";")) {
        return true;
    } else {
        return false;
    }
}
?>