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
Related
I'm currently working on a system right now that involves the use of many images. There are some images (specially background images) that I used where when I updated the images that includes the picture path, the image does not update. I checked the path if it's correct and even the folder containing the picture.
However, I noticed that when I clear the history of the picture, that is the time the picture becomes accurate. I did not use any session or cookie for saving my image. Is there an explanation for this?
Add some parameter to image link and change this parameter after image update ;)
Example :
src="image.png?timestamp=20180602" ;
Im working on a PDF. I just wanted to change the logos so I changed the Image and on the first site I get the new image in correct size so but in the second pdf the image is very large.....
Here the line : $this->Image('images/logo.jpg', 160,12.0,30 );
I find it screws up if I used the same image twice. Best practise is to use an image only one time in a PDF. Make a copy of it under a different name if you are going to use it again.
Well this is the only problem that bugs me a lot every time I create a CMS.
Say I have included the functionality to add some images with text (Not a image gallery) . This is the create function of CRUD. The read and delete are also not very difficult. The only problem I face is with the update function.
How should the image update be handled. How can I know that if the user wants to change the image and later if he submits a new image.
If he checks to change the image and uses the same image to upload, it would be useless to first unlink the previous image and upload the same image again.
How are these kind of things handled?
At this moment I am using codeigniter for Development.
Most CMS's will have image functionality baked in. I'd first suggest not trying to reinvent the wheel here.
In ExpressionEngine for example (on a codeigniter backend) you're givin a 'field tag' <img src="{{channel.img}}" />
The CMS is smart enough on the backend to change that field's value on update without you having to manually do anything. The only issue is if it overwrites the image name/location rather than created a new image in the uploads and leaving the previous one in there.
This is how it's handled in Umbraco CMS as well.
What else do you need?
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.
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.