A script I've inherited is showing a random image on the homepage. The random image is currently determined (ugh) by grabbing an array of all of the images, then
$rand_keys = array_rand($array_photoid, 2);
$photoid = $photo_rows[$rand_keys[0]];
No doubt this worked swell when there were only a few dozen images, but now there are 5000 and this needs fixing.
While rewriting this I'd like to add in some functionality that allows users to go to the Previous and Next image. Going forward is easy enough, just draw up another random ID from the table.
How would I allow users to go back over previously shown random images? Is this even possible without resorting to a hack like above (storing all id's in a session array).
You might want to use jQuery for this instead and use the image slider plugin that supports ajax. So you load the next image via ajax request. When going back to previous image, its already loaded before so don't need to worry about the 'prev' function. (https://www.google.com.sg/search?q=photo+slider).
And then you load the page to a random image on the slideshow.
Related
My MODX site needs to grab the first image from all pages for Open Graph Meta tags, which will be plugged into the Head chunk for all templates.
The problem with this is that not all images are located in the content part of a page. Some are located inside Chunks and others inside TVs. (Finding an image tag from the content is not an issue.)
It might be possible to get all Chunks and TVs and loop through their values to check for images.
But is there a way to get the <body> contents of the resource?
Probably several ways, you can try writing a plugin to parse through the entire content of a page, looks like the OnWebPageComplete event may be the one to use (take a look at the different events to see if one is more appropriate}
You can try and grab the resource from the cache, keeping in mind that any chnks/snippets/TVs called in the page un-cached will not show up in the resource cache file.
You can get a list of TVs once you have loaded a resource & then use getTVValue to get the value.
If you have an image in a chunk, getChunk might work [might, I've never tried to use it that way] to get it's contents but I would image that the image in a chunk would come from a TV ~ so you should be able to retrieve it with getTVValue.
You could also just setup a TV for the OpenGraph image and explicitly set it on a page by page basis.
Probably writing a plugin & some regex is going to be the least painful way of going about it.
I have a WordPress site. On this site, I am trying to create a dice-rolling game that displays above specific post formats. Here is the way it will work:
There will be a directory on my server. In the directory, there are six different images, each of one dice side. One will be a picture of a dice side with one dot, another picture will have two dots, etc.
The "rolling" of the dice is simulated by using PHP to display a random image from that directory. But there's a catch...
Once the post that displays a "rolling" of the dice is created, I want that particular random image that is picked to be stuck to that post, so that no matter how many times you refresh the page, that same "roll" will be associated with that post.
In essence, every visitor will see the same image, the same "roll", no matter what server is used, or whether they've refreshed the page or not. If the image that shows up is "five", then all users will see "five" even if they refresh the page.
Although it is possible to do this by playing Random Number God and just creating an algorithm with the post time, post ID, and other things in order to get a single "random" number for each post based on post meta, I would like to know if it would be easier to make it so that the image chosen was stored in the database instead.
It's likely that there are other options that I may not have thought of. If there are, I would certainly like to hear about them. Thanks.
You could accomplish this by making use of PHP sessions. It will essentially store the result as a cookie in the user's browser.
When the page's reloaded, check for the dice in the appropriate session variable, if it's available show that or generate something new.
When a user visits for the first time, he gets an image (anything b/w 1 and 6) and this info's stored in the cookie. On subsequent refreshes, the same image is shown as the server looks for the cookie before generating a new image.
Hope it helps.
By default when a webpage is loaded, images are loaded one single time for each image. If you have 5 instances of the same image on a page, that image is loaded once, and then used in all 5 places seemingly from the cache of the first image load.
What I want to do is have a single image displayed 5 times on a page, and each time the images is called have it re-loaded.
The reason for this is I have an image that is called from a database and each time the image is called it loads a different picture. Now this works perfectly when refeshing the page, but not when the image is loaded multiple times on the same page.
For example if I put the following into a webpage:
<img src='http://bannerpillar.com/u/viraladmin.jpg'>
The image loads perfectly. If I reload the page, a different picture is displayed for each time the page reloads. However if I add the image to a page in 2 different locations, the same one picture is displayed from both locations.
How can I make it so the image is refreshed every time it is called on a page? Is that possible?
Try adding something to the end of the file reference, like this:
<img src='http://bannerpillar.com/u/viraladmin.jpg?<?=rand(11111,99999)?>'>
The browser will think each image is unique and load each one separately rather than using the image from cache.
You can find a tutorial on this exact question here:
http://www.marcofolio.net/webdesign/php_random_image_rotation.html
Basically, in the src, you call a php file. The php file loads a filename from a directory of images.
For your case, you would need to write some logic to offset the database return, so this becomes a mysql question as well?
If you wanted the markup to call for an image file, you could redirect using a .htaccess rewriterule
Random no. generetion can be a good option.
but more better if you load those images serially i.e. one after another
n u can use looping for that.
Add a ?rand= to the image url.
<img src='http://bannerpillar.com/u/viraladmin.jpg?rand=<?php echo rand(100000,999999); ?>'>
I have a web application something like image gallery for the user with carousel at the bottom of the application. I was thinking of
a scenario for example what if the user uploaded 1000 images on the application image gallery.
I was just wandering what is the proper way or technique to load 1000 images that is fast so that the user can view it immediately.
Please help me.
1000 images is a lot. I would definitely not load them all at once, but rather in batches somehow. Kind of like Facebook does (I think). It fills up the view, and when you scroll down it keeps loading more, but only as you scroll down. Could also simplify it by using a "Load more"-button.
If it really is a carousel as you say, I would expect it to only show a certain number of images. In that case I would just load the ones visible and for example twice that in a buffer. Then when the user goes to the next page you can replace the current ones with the first ones in the buffer and load some more into the buffer.
Loading 1000 images at one time would be a ridiculous waste of bandwidth, not to mention it would cause major lag for the client. Some client-side JavaScript/jQuery would be required rather than PHP to load the images dynamically as the user scrolls through them, so no more resources/images are requested than needed at the specific time, say 10 at a time. Using the .click() event in jQuery on an element and checking for the end of the 'carousel' so to speak, and then using something like $("#element").attr("src", "imagePath.jpg"); to swap the images shown on the carousel should work.
So I have my own webpage here, which is a sortable thumbnails page. The load() event activates each thumbnail when the first related image is loaded. Since I'm grabbing <img> tags and text content from a hidden div on the page, the thumb activation prevents the user from clicking through to a yet-unloaded image and then waiting while the preload takes place in the background.
The call is pretty simple:
$('#content img:first-child').load(activateThumb).each(function(){
if(this.complete || this.complete === undefined)this.load();});
the .each() catches any cached images and manually triggers the load() event. Worked great and was a fast and lean website. Now, as the site continues to grow, there are over 100 <img> tags in the single HTML file and I'm wondering if there's a conventional limit that I'm approaching. Should I split the page onto 35 different html files? Should I lose the tags and the slick preloading effect in favor of a server-side request for the images on demand?
What's your instinct, as a good programmer?
Well there is no clear limit. You can continue doing the way you have done on your site for as many images as possible.
But it's just that the user might get frustrated while waiting for all the images to get 'activated'.
So in turn what you could do is to have pagination & display say 20 images per page. This way you make the image loading relatively faster.
Also after you load page1, if the user is still in page1 you could start pre-fetching page2. So as the user clicks page2 he sees a very responsive site :)
There is no one rule here. In fact if you see Google Images now a days they do something like what you have done.
You can do it like many Web 2.0 sites do it:
In the beginning, load the images displayed on currently visible part of the page.
Then load other images when user scrolls the page down.