Getting First Image from Wordpress Post, Without Killing Your Server
Sunday May 03rd 2009, 5:59 am
Filed under:
Opinion
OK, so over at OnThisIsland we had a problem arise from the fact that our first-image-fetching function was killing our server. We had warnings flying in from our hosting company, and pages slowing down to unbelievable speeds. After a bit of investigation we realised that on each archive, home or search page, this function was called about 10 times, making it search through 10 different articles to find the image EACH time it loaded.
What we did was make it store a custom field automatically on its first load. (We also made it return a default image as we were using timthumb.php to resize images on the fly and didnt want to resize externally hosted images).
Here’s my version to get the first image from an article. Its partially based on the Wordpress Recipe and some ideas of my own. Hope you find it useful.
<?php
function getImage($post){
$externalimage = get_post_meta($post->ID, ‘external’, true);
$gthumb = get_post_meta($post->ID, ‘gthumb’, true);
if ($externalimage==”TRUE”){
$returnimg = “http://www.yoursite.com/default.png”;
} else {
if ($gthumb==”"){
$szPostContent = $post->post_content;
$szSearchPattern = ‘~<img [^\>]*\ />~’;
// Run preg_match_all to grab all the images
// and save the results in $aPics
preg_match_all( $szSearchPattern, $szPostContent, $aPics );
// Check to see if we have at least 1 image
$iNumberOfPics = count($aPics[0]);
if ( $iNumberOfPics > 0 ) {
for ( $i=0; $i < 1; $i++ ) {
$img = explode(“\”", $aPics[0][$i]);
$returnimg = $img[5];
// ==> Added to combat server performance.
add_post_meta($post->ID, ‘gthumb’, $returnimg, true);
};//end for
};//end if no. of pics more than zero
} else {
$returnimg = $gthumb;
}; // end if gthumb is set
}; //end of if externalimage
return $returnimg;
}//end function getImage
?>
Looking for Jane…
Ready to scare or impress? Or already in the Halloween spirit? Manchester’s halloween parties started off well into last week with the scarErasmus party at Tiger Tiger and several other parties throughout the weekend at clubs like the Attic, Sankeys and 5th Ave.
But halloween is only tomorrow and I’ve already seen some very impressive outfits throughout the city, from axe murderers to alleged ‘first time’ cross dressers. Although impulse dressers did party in horns and wings (some of which had the bodies to get away with only that) I was left impressed with the ammount of effort put into other costume ideas. I personally wore out a tarzan costume (being from South Africa I was aiming more for the African Warrior look but hey) and almost froze my cahones off, but it was well worth it, although several pieces of my costume did go missing, and if you are holding a piece of it, keep it, just let me know where they wound up
That brought me to my problem at hand, I never found any woman (or man :s ) dressed as tarzan’s other half, Jane. So if anyone see’s her, take a picture and send it to me. Lets see if there’s any daring young people who risked the cold for a good laugh.
First Month In Manchester
Well I’ve been here for one whole month and in my third week of Uni, so I figured I’d start up my blog again, since I’ve finished ’settling in’ or at least reached a level where I feel comfortable enough to get back into the wonderful world wide web.
“So how is Manchester after the army?” is probably the most asked question I get from people who realise my dog tags are more than just a passing fashion statement, and the answer is mostly a facial expression that could probably be tagged as ‘look around, what do YOU think?’
Manchester is a vibrant student city with two Uni’s, Manchester Metropolitan and the University of Manchester, being located right next to each other, which means that the people are almost always friendly and up for a good time. As for the night life, throughout the week its like a full blown battle between the promoters for claiming their night as the best student night out.
We have the Vodka Island guys pushing through on Mondays, the Headphone Disco on Tuesdays, Roller Disco on Wednesdays and the Erasmus Latin Crash Parties on every second Thursday, with Friday and Saturday being the nights for regular parties and a large number of gigs and concerts. However, these are just the most popular parties for the studying masses from my opinion, there are also some crazy hectic raves throughout the week at clubs like Sankeys and the Attic over the weekend.
Xmas inside
Monday December 25th 2006, 2:37 pm
Filed under:
Opinion
Well its been a strange day. Not the xmas i’m used to but hey. There was no family bonding. No getting gifts or seeing your loved one’s expressions after spending hours looking for that PERFECT gift.
Instead it consisted of normal guard duty and the occasional relaxation whilst waiting for your next duty to come up. Although the days precedings went on in a more chilled out fashion its still not what anyone pictures as their xmas day.
Being inside the army during xmas and xmas eve kinda makes me realise that there are others who whilst the world celebrates one holiday or another there are people somewhere on duty be it by choice or by not.
So my wishes go out to those who spent xmas on guard duty, on stand by at police stations, hospitals and firestations. Merry christmas to you.
so keep in mind on new years there are also going to be people working to ensure the safety of the public… Why not go past and thank them for what they are doing and give them your wishes.
As for my family, i’m sorry i couldn’t be there with you for the festive season esp after all that has happened in this last week. See you soon and last but not at all least… RIP thie!
Mobile expandability examined
Friday December 08th 2006, 9:05 pm
Filed under:
Opinion
Posting from mobile phone test. Just a quick post to see if this is even practical enough to be used as an everyday posting method… Using a Nokia N73 with Opera Mini web browser and so far so good.
Except i have a feeling that there is a limit on input text in opera.
maybe the future of all content management systems will contain a mobile update module, which will be lightweight, mobile screen resolution friendly and secure. With this in mind i’ll throw something together for the OTI cms.
(still no sign of the text input limit i experienced on the previous version of Opera Mini)
take care everyone, and i wish you a very merry festive season.