Hello, will you help me write some code to generate random-colored ellipses for an html page? The ellipses will serve as a side-bar coloration for the regular text content. Each successive image will have an increasing z-index and will overlap each other.

I am having trouble figuring this out as I am only a beginner.

Recommended Answers

All 11 Replies

hello again.. you were interested in learning java, i recall. thanks for help here.. i'm a big dreamer, accomplish little.. i actually want to retrieve random images from google searches now, but i've settled for the effect so far: http://listenlight.net/04. I have left ample space 'neath the texts for another design implementation.. cheers.. hope you like the poems.. I'm JC, there, and co-editor. i'm feeling terribly proud right now !

Hi,

Take a look at PHP's GD functions:
http://us3.php.net/gd

You'll need the imagepolygon function, http://us3.php.net/manual/en/function.imagepolygon.php, to create your ellipses.

:)

Yep, I'm still on the quest to learn java.. lol.

As for using random googel images, I think it would be easier to use images from flickr. They have RSS feeds for images categorized by tags. Its much easier to use RSS feeds then to parse a google search HTML page, or get images using the Google Soap API.

Flickr even allows a format for serialized PHP or just plain PHP!

Example URL:

http://api.flickr.com/services/feeds/photos_public.gne?tags=colorful&format=php

That returns all images for the tags: "colorful" in a PHP array named $feed. The array is structured like an RSS document where $feed contains each feed item..

So you could use it like:

// includes the images from flickr for the comma seperated $tags
$tags = urlencode('colorful');
include( "http://api.flickr.com/services/feeds/photos_public.gne?tags=$tags&format=php" );


foreach($feed['items'] as $item) {
		echo $item['description'];
}

too simple huh...

Your site design is quite unique by the way... congrats!

awesome! thanks

i just ran your code and the task suddenly seems within reach.

how do i parse out the "<img src... />" strings ? now i'm excited. all i really know of php are the random and array functions

it works fine on my localhost, but on the hosting server i get these errors.

[B]Warning[/B]:  include() [ function.include ]: URL file-access is disabled in the server configuration in [B]/home/.highjump/tefflox/listenlight.net/flickr.php[/B] on line [B]13[/B]

[B]Warning[/B]:  include([url]http://api.flickr.com/services/feeds/photos_public.gne?tags=colorful&format=php ) [ function.include ]: failed to open stream: no suitable wrapper could be found in [B]/home/.highjump/tefflox/listenlight.net/flickr.php[/B] on line [B]13[/B]

[B]Warning[/B]:  include() [ function.include ]: Failed opening 'http://api.flickr.com/services/feeds/photos_public.gne?tags=colorful&format=php' for inclusion (include_path='.:/usr/local/php5/lib/php') in [B]/home/.highjump/tefflox/listenlight.net/flickr.php[/B] on line [B]13[/B]

[B]Warning[/B]:  Invalid argument supplied for foreach() in [B]/home/.highjump/tefflox/listenlight.net/flickr.php[/B] on line [B]15

[/B]

It looks like the allow_url_fopen setting has disabled in PHP.ini. You cannot change this setting at runtime, you will have to edit PHP.ini.

see: http://us3.php.net/filesystem

If you can't edit PHP.ini then you will have to contact your web host 'bout it, or use CURL.

how do i parse out the "<img src... />" strings ? now i'm excited. all i really know of php are the random and array functions

For that you can use Regex.

if (preg_match("/<img src=\"([^\"]+)\" .*? alt=\"([^\"]+)\" .*? \/>/", $item['description'], $matches)) {
			$img_html = $matches[0];
			$img_src = $matches[1];
			$img_title = $matches[2];

// ouput image

}

I thought I'd take the time to explain the regex:

I used the function preg_match, which takes a regular expression (string pattern) and matches the pattern in the $item string.
The matches are set as an array in the third parameter, in this case $matches.

Heres the regex expression:
"/<img src=\"([^\"]+)\" .*? alt=\"([^\"]+)\" .*? \/>/"

It is quite simple:
a regext expression starts with a delimiter, in this case /. When the regex engine finds another instance of / it will end the pattern and assume every character after the second delimiter are arguments passed with the regex expression.

$matches[0] will contain a match of the whole pattern (if a match is made).
$matches[1] contains a matche of the first set of parantheses () and so on.

square brackets allow you to match any character inside the brackets.
^ right after the first square bracket tells the regex engine to match any char other than whats in the square brackets.

. is any single character.
* is any number of the character right before it.
+ is one or more of the chracter right before it.
\ is used to escape characters, meaning the character right after the \ is treated as a literal value and does not have any special meaning. eg: \* means match the asterix itself.
? means either or? or 0 or 1 of the character right before it. If it is placed right after a quantifier eg: .*? then it tells the regex engine to match "lazily" - match the smallest possible match, as opposed to matching "greedily" where the largest match is made. eg: In "<tag> cotent </tag>", the regex "<.*>" will match the whole string, however, "<.*?>" will only match "<tag>".

So the regex expression "/<img src=\"([^\"]+)\" .*? alt=\"([^\"]+)\" .*? \/>/" reads:

<img src=\" : Matche a string starting with <img src="
([^\"]+) : create a new match in $matches[1] and match all the chracters that aren't a double quote, ".
.*? continue matching any character (lazily) untill you come to: alt=
alt=\" : match alt="
([^\"]+) : create a new match in $matches[2] and match all the chracters that aren't a "
.*? \/> : match any chracter (lazily) till you reach />

Now you get a resulting array: $matches with the tree matches inside. $matches[0] being the whole string. $matches[1] being the first ([^\"]+) and $matches[2] being the second ([^\"]+)

gee, sorry for being such a newb, but what is CURL?

thanks so much for going over the regex. hopefully i will be able to use it soon. i'm terribly excited about the possibilities of this code for making mosaics on my pages...

gee, sorry for being such a newb, but what is CURL?

thanks so much for going over the regex. hopefully i will be able to use it soon. i'm terribly excited about the possibilities of this code for making mosaics on my pages...

CURL is a php lib that allows you to create connections to remote servers with protocols such as HTTP, FTP etc, just like the built in stream functions. PHP needs to be compiled with CURL support however, but most hosting supports CURL.

CURL PHP Manual: http://php.net/curl


Heres a good simple class for CURL I found on the php CURL manual page:

<?php
/*
Sean Huber CURL library

This library is a basic implementation of CURL capabilities.
It works in most modern versions of IE and FF.

==================================== USAGE ====================================
It exports the CURL object globally, so set a callback with setCallback($func).
(Use setCallback(array('class_name', 'func_name')) to set a callback as a func
that lies within a different class)
Then use one of the CURL request methods:

get($url);
post($url, $vars); vars is a urlencoded string in query string format.

Your callback function will then be called with 1 argument, the response text.
If a callback is not defined, your request will return the response text.
*/

class CURL {
   var $callback = false;
   var $cookiejar = 'cookie.txt';
   var $cookiefile = 'cookie.txt'

function setCookieJar($file_path) {
   var $cookiejar = $file_path;
}

function setCookieFile($file_path) {
   var $cookiefile = $file_path;
}

function setCallback($func_name) {
   $this->callback = $func_name;
}

function doRequest($method, $url, $vars) {
   $ch = curl_init();
   curl_setopt($ch, CURLOPT_URL, $url);
   curl_setopt($ch, CURLOPT_HEADER, 1);
   curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
   curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
   curl_setopt($ch, CURLOPT_COOKIEJAR, $this->cookiejar);
   curl_setopt($ch, CURLOPT_COOKIEFILE, $this->cookiefile);
   if ($method == 'POST') {
       curl_setopt($ch, CURLOPT_POST, 1);
       curl_setopt($ch, CURLOPT_POSTFIELDS, $vars);
   }
   $data = curl_exec($ch);
   curl_close($ch);
   if ($data) {
       if ($this->callback)
       {
           $callback = $this->callback;
           $this->callback = false;
           return call_user_func($callback, $data);
       } else {
           return $data;
       }
   } else {
       return curl_error($ch);
   }
}

function get($url) {
   return $this->doRequest('GET', $url, 'NULL');
}

function post($url, $vars) {
   return $this->doRequest('POST', $url, $vars);
}
}
?>

ref: http://php.net/curl

I've added the two functions:

function setCookieJar($file_path) {
   var $cookiejar = $file_path;
}

function setCookieFile($file_path) {
   var $cookiefile = $file_path;
}

This will allow you to set paths for your cookies to be saved in. Usually you would want to use a file below your webroot, that cannot be read.
IF just for the flickr images, you don't need those two functions... but say if you wanted to use it for the flickr API, which would need authentication for some requests, it would be best to protect the cookies from being read by anyone.

For flickr you'd use it something like:

// you need curl since you cant use the php stream functions
$CURL = new CURL();
$php = $CURL->get("http://api.flickr.com/services/feeds/photos_public.gne?tags=$tags&format=php");

// since you cant use include() you'll need to strip the php tags
$php = str_replace(array('<?php', '?>'), '', $php);
// then you need to evaluate the php within, as would be done with an include()
eval($php);

I havent tested that but, it should work. Hope that gives you a start on getting the mosaics done.. :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.