Lots of CPU usage when...

Soldato
Joined
16 Oct 2007
Posts
7,480
Location
UK
creating a very simple image using php/gd?

i have a script that counts records in a database, and then outputs as an image

but if is to be shown on every page on a website, is this going to put a huge strain on the CPU?
 
I doubt the GD functions use much CPU, especially for a small image. Counting records in a database, I have no idea, I would guess it would be pretty efficient though.

You can just set the expiry time on the image so it's only loaded once and then cached in the users' browser until it expires.
Code:
header( 'Expires: ' . gmdate('D, d M Y H:i:s', time() + 60));
... for 1 min expiry time
 
Code:
<?php 
include("jobs_config.php"); 
mysql_connect($DBHost,$DBUser,$DBPass);  
mysql_select_db($DBName);  
$strQuery=" 
    SELECT count(".$DBprefix."ext_postings.id) as totaljobs, count( DISTINCT ".$DBprefix."ext_postings.employer) as totalemployers FROM ".$DBprefix."ext_employers , ".$DBprefix."ext_postings 
    WHERE  
    ".$DBprefix."ext_postings.employer =  ".$DBprefix."ext_employers.username 
    AND expires>".time()." AND ".$DBprefix."ext_postings.active='YES'"; 
$result=mysql_query($strQuery); 
$row=mysql_fetch_assoc($result); 
$image_text = $row['totaljobs']." jobs from  ".$row['totalemployers']." companies"; 
Header ("Content-type: image/png");  
$img_handle = imageCreateFromPNG("images/toptrans2.PNG"); 
$color = ImageColorAllocate ($img_handle, 100, 100, 100); 
ImageString ($img_handle, 3, 10, 9, $image_text, $color);  
ImagePng ($img_handle);  
ImageDestroy ($img_handle);  
?>



If you wouldn't mind mate!
I really have no idea about php, have just pieced together bits using the amazing abilities of OCUK members.
 
Code:
<?php 
include("jobs_config.php"); 
mysql_connect($DBHost,$DBUser,$DBPass);  
mysql_select_db($DBName);  
$strQuery=" 
    SELECT count(".$DBprefix."ext_postings.id) as totaljobs, count( DISTINCT ".$DBprefix."ext_postings.employer) as totalemployers FROM ".$DBprefix."ext_employers , ".$DBprefix."ext_postings 
    WHERE  
    ".$DBprefix."ext_postings.employer =  ".$DBprefix."ext_employers.username 
    AND expires>".time()." AND ".$DBprefix."ext_postings.active='YES'"; 
$result=mysql_query($strQuery); 
$row=mysql_fetch_assoc($result); 
$image_text = $row['totaljobs']." jobs from  ".$row['totalemployers']." companies"; 
Header ("Content-type: image/png");  
header( 'Expires: ' . gmdate('D, d M Y H:i:s', time() + 3600));
$img_handle = imageCreateFromPNG("images/toptrans2.PNG"); 
$color = ImageColorAllocate ($img_handle, 100, 100, 100); 
ImageString ($img_handle, 3, 10, 9, $image_text, $color);  
ImagePng ($img_handle);  
ImageDestroy ($img_handle);  
?>

3600 seconds = 1 hour
change it to whatever you want
 
Thank you!

From one Jim to another....
i don't suppose you could help me out with one last small problem?

I'm trying to change the font by using the imagettftext() function instead of imagestring.

but i mess it up every single time......................
 
Back
Top Bottom