943,940 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 5278
  • PHP RSS
You are currently viewing page 1 of this multi-page discussion thread
Nov 8th, 2006
0

Need help making simple random images

Expand Post »
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.
Reputation Points: 12
Solved Threads: 1
Junior Poster
tefflox is offline Offline
174 posts
since Jul 2006
Nov 9th, 2006
0

Re: Need help making simple random images

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.

Last edited by digital-ether; Nov 9th, 2006 at 3:18 pm.
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Nov 9th, 2006
0

Re: Need help making simple random images

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.

Reputation Points: 12
Solved Threads: 1
Junior Poster
tefflox is offline Offline
174 posts
since Jul 2006
Nov 9th, 2006
0

Re: Need help making simple random images

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...ful&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['items'] contains each feed item..

So you could use it like:

[php]// 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'];
}
[/php]

too simple huh...
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Nov 9th, 2006
0

Re: Need help making simple random images

Your site design is quite unique by the way... congrats!
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Nov 9th, 2006
0

Re: Need help making simple random images

awesome! thanks

i just ran your code and the task suddenly seems within reach.
Last edited by tefflox; Nov 9th, 2006 at 5:14 pm.
Reputation Points: 12
Solved Threads: 1
Junior Poster
tefflox is offline Offline
174 posts
since Jul 2006
Nov 9th, 2006
0

Re: Need help making simple random images

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
Reputation Points: 12
Solved Threads: 1
Junior Poster
tefflox is offline Offline
174 posts
since Jul 2006
Nov 9th, 2006
0

Re: Need help making simple random images

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

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

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

Warning:  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 /home/.highjump/tefflox/listenlight.net/flickr.php on line 13

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

Reputation Points: 12
Solved Threads: 1
Junior Poster
tefflox is offline Offline
174 posts
since Jul 2006
Nov 10th, 2006
0

Re: Need help making simple random images

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.
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005
Nov 10th, 2006
0

Re: Need help making simple random images

Click to Expand / Collapse  Quote originally posted by tefflox ...
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.
[PHP]if (preg_match("/<img src=\"([^\"]+)\" .*? alt=\"([^\"]+)\" .*? \/>/", $item['description'], $matches)) {
$img_html = $matches[0];
$img_src = $matches[1];
$img_title = $matches[2];

// ouput image

}[/PHP]

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['description'] 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 ([^\"]+)
Moderator
Reputation Points: 457
Solved Threads: 101
Nearly a Posting Virtuoso
digital-ether is offline Offline
1,250 posts
since Sep 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: apache2-devel file?
Next Thread in PHP Forum Timeline: Making a phpbb installer like fantastico





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC