Okay, so on my website a user can upload a profile picture. But the issue is, if they update it by uploading and overwriting the existing profile picture they have to wait for their browser cache to clear and the same for everyone else on the site.
I know I could easily beat this by sticking a string on the end of the image URL e.g. ?id=22185 , but that will make my site loading times VERY slow.
Could any of you recommend a way of making the user's profile picture update instantly for every user on the site?
Use the file modified time as the URL variable. That way the image will be cached until that number changes, which only would happen of the file is updated.
Set unique name for each image. When user change image, e filename change too and browser will load new image instead of serving old FROM cache
You could easily add a timestamp to your files or you could use the "headers" function of PHP to change the "Expire" param.
Related
On my website I would like to have the possibility of uploading an image, modifying it and printing. I won't be using the images later on, it's important for me to have them available until leaving the "print" page.
Do I need to create a database in order to store the images or is there any other (more simple) way of achieving this? Can you store such things in RAM (excuse my noob question)?
Basically I need to upload the image, display it on the screen, play with it and print it.
Alternativly:
When a user enters the page and uploads an image, create a new folder for this user only and store the relative path in a cookie. This way you could grab the users folder at any time no matter what the user is doing (leaving, coming back, reloading etc.).
Appended according to the comments
Dont use plain-text values (I bet nobody knew this when using cookies).
Well, I would upload the image to the folder on a server (user-images), then save the image-path as a $_COOKIE variable (so you can pass it to other files like modify.php or print.php). You'll delete the file with the unlink() function when the session data is no longer available. Something like:
if( empty($_SESSION) ){
unlink( $_COOKIE['img_path'] );
setcookie( "img_path", true, time()-10 ); // set negative expire time in order to destroy the cookie
}
I am creating a dynamic image dependent on a lot of information about the logged in user, but with so many users online, the image system is using a lot of the resources my website hosts have allocated to me.
Is there a way to tell the browser to temporarily cache an image? Similar to temporary cookies?
The image contains numbers such as post count and other frequently incrementing numbers that can cause the browser's automatically cached images to re-download the image potentially every page load. I wouldn't mind a little inaccuracy to save a lot of processing for my server
[Edit]
Browsers already do cache content(unless specifically told not to do so), if it detects a change in last modified then it reloads it.
I am taking a guess that your image being generated is either a) being returned to the browser by a specific php script() or b) is being stored temporarily but is being updated and so the browser retrieves the updated version.
[/Edit]
If you are outputting an image based on dynamically generated content you can write this temporarily to a directory and then based on a predefined time period pull the image if its valid.
if(file_exists($file) && filemtime($file) <= time()-60*20) {
// Regenerate image
} else {
// Load image from cache
}
The example above will not regenerate the image for 20 minutes.
Try to look here
http://www.webscalingblog.com/performance/caching-http-headers-cache-control-max-age.html
In other way, you can set up cache-control header in PHP, and display images using script.
I'm working on a PHP application to store some personal information (including photo). I'm storing the image on a specific folder (let's say myapp/images/people/).
After saving a photo my app is redirected to a page showing the information of the specific person (kind of a profile).
If the photo is saved for the first time (no other photo was previously saved for that person) then the photo is shown in the profile. Everything seems to work at this point.
The problem is when I want to change the photo. When I replace a photo my applications keeps showing the old one. I've checked the server and the old photo is gone, there's only the new one (as I need) but the application doesn't show it.
I guess it's something to do with cache.
I've tried by adding the html tag with no cache values, I've tried by adding the same values by using the header() PHP function but nothing.
I also tried by using:
if(file_exists($imagepath))
{
unlink($imagepath);
}
and similar I've used
if(file_exists($imagepath))
{
unlink($imagepath);
clearstatcache();
}
but also nothing.
Can someone help me with this? Any idea about what's going on? The new photo is in the server, the old one is not but the app keeps showing the previous file.
The image is cached in the browser. The best way is to generate a new image name on the server and return the new image in the HTML. Since it is a new image for the browser, it won't get it from the cache and you will see the new image in the application.
I allow users to add a profile picture of themselves to an account, pretty standard stuff.
To make things simple, when they update their image I simply overwrite the image they currently have stored (its not a big part of what we do so simple approach)
So, page is shown with their current image, they can then choose to upload a new one, the uploaded file is then saved, image URL updated in MySQL table and page is displayed again.
The image is uploaded correctly, MySQL table updated correctly, but the image is cached so old image shows on page even though they have uploaded a new one.
Can I un cache one image? Is there a better way to do this?
For avoiding image caching you can simply add a query sting in the image src. Best would be adding a date time.
{$t = getDate();}
and in your image src add: src= "path.jpg?t=".$t
For this you need not to change anything for new image..Date time automatically gets changed every second ;)
Hope it helps..
Use versioning. In the link for the image, instead of abc.jpg say abc.jpg?v=5. And when a new image is uploaded, change the number.
see this question
I want a help,I created a facebook app using php gd.The program is when user open the app a image will appear.The image contains the username of the user,the profile pic and random generated nick name.Iam saving the output image to the server as resized.jpg and post that image to the users wall using facebook graph.
The problem is when 2 users use the app at same time,the output varies.
How to generate image to each user without saving it to the server and post to facebook.
now iam using html img tag to display the image in app..
Answer is quite simple: your image file name must have different names per user. If you have user id in $uid variable,why don't you save target file as:
imagejpeg( $rImg, "resized".$uid.".jpg" );
This way you'll have different images per user.
Rather than saving the file to your server as "resized.jpg", what you want to do is to output the generated image directly to the user. From http://php.net/manual/en/function.imagejpeg.php it says to set filename to NULL, which causes the output to be sent to the user instead of to a file. Note: you may also need to set the correct Content-Type using the header function.
This method is an alternative to Tomasz's. With his method, each image is cached on your server. With mine, the image is generated each time the page is requested. Here is an example showing the difference:
<img src="http://example.com/resized1234.jpg"> #1234 is the user's id
<img src="http://example.com/generate-image.php?uid=1234">
In your case, you said you are posting the image directly to Facebook, so I would advise my method, as Facebook will store its own copy of the image if you post it on their site, and won't need to be cached. Also, you won't have a other user's images piling up on your site.
Let me know if you need me to clarify anything in my answer!