PHP to Pull Images

Status
Not open for further replies.

mediastar100

New member
Apr 26, 2008
1,443
12
0
Ok - part 2 of my speed ppc saga...

Say I want to pull images of celebs and display them based on the kw a user uses to land on my page. Is there some kind of PHP magic that can do this?

Will I need to collect and size all the images and have them stored somewhere the PHP can access them or can it scrape them from somewhere?

As ever - cheers guys,

Dave
 


cheers guys - im being a dumbass here...

Is there a few lines of code that I can use to display images that I have saved on a server?

Thanks

Dave
 
Here's the one I use ... it's real scientific

PHP:
<img src="http://www.mydomain.com/images/<?= $pic_location ?>" />

$pic_location is usually pulled from a db .. I guess you can round up all the files in a folder and do a regex to find the one with the keyword (celeb name), but that could consume some serious resources if there are many pictures in that folder.
 
I guess you can round up all the files in a folder and do a regex to find the one with the keyword (celeb name), but that could consume some serious resources if there are many pictures in that folder.

To go along with this idea would be to have many matches to a single keyword, or give out a random default image, kind of like a shortcut until you fill out the list completely. I don't think the resources would be a big deal until you grow the list to tens of thousands.
 
mediastar, did you even bother to learn php and html?

Not to be mean but damn.

To be constructive: there are many ways to do it. I would create an index in mysql of all the files on my server for that purpose, and use some simple querys to pull a related image url, but that's just me.
 
I like to use Flikr to do this sort of thing. At least then you can specify CC commerical license and not completely steal the image.

Here's some code to get started with:

Code:
    function get_image($keyword,$single=true) {
    
        $this->curl = new Curl();
        
        //Run search on flickr
        $page = $this->curl->get("http://www.flickr.com/search/?q=" . urlencode($keyword) ."&l=comm&ss=0&ct=0&w=all");        
        preg_match_all("@http://[^\s]+_m\.jpg@",$page,$matches);
        
        //Get more
        if(strstr($page,"&page=2")) {
        $page .= $this->curl->get("http://www.flickr.com/search/?q=" . urlencode($keyword) ."&l=comm&ss=0&ct=0&w=all&page=2");                
        preg_match_all("@http://[^\s]+_m\.jpg@",$page,$matches);        
        } else {
            //Split keyword
            $array = explode(" ",$keyword);        
            foreach($array AS $_keyword) {
                $page .= $this->curl->get("http://www.flickr.com/search/?q=" . urlencode($_keyword) ."&l=comm&ss=0&ct=0&w=all");                            
                
            }
            preg_match_all("@http://[^\s]+_m\.jpg@",$page,$matches);                    
        }
        
        
        if($single) {
            if(is_array($matches[0])) {
                return $matches[0][array_rand($matches[0])];
            }
        } else {
            return $matches[0];        
        }
        
        return false;    
    
    
    }
You can use it with the curl class I've posted elsewhere.

So, run for example
Code:
get_image("maria sharapova",false)
and you'll get an array like this:
Code:
Array
(
    [0] => http://farm1.static.flickr.com/79/239294613_545414de20_m.jpg
    [1] => http://farm1.static.flickr.com/87/240570247_ebbad47462_m.jpg
    [2] => http://farm1.static.flickr.com/96/239311838_22b35d5858_m.jpg
    [3] => http://farm2.static.flickr.com/1292/1281517753_99a3bf4ebf_m.jpg
etc
Just run through the array, $curl->get the image urls, and save the contents to your server, giving the images whatever name you want.

Do that with a variety of keywords and you'll have a nice collection soon enough.

As to retrieving the images, you have a variety of option as discussed. A quick and dirty method would be just to make sure you always grab X amount of each celeb, say 10. Then grab a random image with <img src='<?=/img_dir/$keyword . rand(1,10)?>'/>

Good luck.
 
Status
Not open for further replies.