Upload images using PHP?

Asked

Viewed 472 times

0

I have a page that loads about 90% of the content based on images, the size of the images varies from 20kb up to 150kb depending on the size of the page, to improve the loading performance of the page I’m thinking of calling the images through a php script, which will identify whether the user is using Chrome or another browser with Webp support, and then upload the image with the most appropriate browser extension. My question is:

  • It’s a good thing to make this call through a php script?
  • 90% of content is image, but the user sees all that content at a time? A good way to save both server and client resources would be to use the loading strategy of the images.

  • At home I have a banner that occupies 90% of the resolution of the user, followed by a list with approximately 30 images, when the user der scroll plus 10 lists of these are created, and when it arrives at the end another 10 will be created again.

  • When user gives scroll it loads 300 images?

  • Currently are loaded approximately 100 initial images and each list we have the same images organized differently, but yes in the near future will be something like this 300/400 images in total

  • Depending on your layout Lazy loading would still be good. See http://ressio.github.io/lazy-load-xt/demo/infinite.htm

1 answer

3


In my opinion, it is not a good thing to use PHP to upload images.

If you do so, you will be causing PHP to have to process all the contents of an image and then send a response to the browser.

This can be expensive for the server in terms of processing, since PHP needs to use memory to render this image.

Not to mention that the browser has a specific mechanism for image caches. Perhaps by doing it for PHP, you cannot enjoy this functionality, since it is actually a PHP script that will be processed, and not an image.

The solution I usually use for cases where I need to use PHP to process images is: Process the image once, giving it the desired format, and save it. I usually convert them all to the same format.

I believe that if you want to improve the performance of your images, you can invest some time in creating a Thumbs system. Using resized images or with proper treatment, you can achieve desired performance. But I don’t think that always loading with a PHP script is appropriate. Now, if you do this once, creating a static image optimizes, I think that would be a good solution.

  • but processing through php I can use header to create the cache of the files, if you access the Netflix in Chrome and firefox you will see that the extension of the images are different in the two browsers, as they do this?

  • But anyway, you are using PHP to process the images. Let’s face it, you need two steps: PHP processing the image and the response being sent to the browser. In the case of static image, the browser only needs to read the image and ready.

  • 1

    So it’s a bad idea to also use PHP to stream video? I got this idea from this feature

  • I agree that it will use memory of the server for nothing, but who will render the image is the browser. PHP is only serving bytes, even when you’re accessing an extension page .php who will decide what to do with what was served is the browser.

  • @Thomas the problem is that if he does not know how to do it right, the answer coming from apache would come as 200, if I’m not mistaken image comes as 304. Not to mention other specific headers that people always forget to put when they do this kind of thing.

  • Actually, I don’t have much experience with image processing, but I can guarantee that using a static file would be better than processing everything before for a script. Unless he needs to do something specifically within that script, then maybe it’ll make up for it on the other hand.

  • Here the images come as 200

  • @Leoletto every time you reload the page?

  • Yes, if the problem is memory consumption there is no problem as they are loaded from a dedicated server, reasonably good

  • It’s up to you, young man. I just can’t help but quote that I don’t think this would be the way. I would rather have the image replicated 2 times on my server and make this display according to the browser through a Javascript decision, than having to process all the time through a PHP script.

  • But that’s the point, the images will be duplicated in jpg and webp, I’m only thinking of using the php script to detect which browser the call was made from, and not to create the image on time

  • The problem of coming as 200 every time the images are downloaded every time they are accessed. The ideal would be for them to be cached, and when accessed a second time, do not need to download again.

  • But they are coming as 200 and are being loaded from the local disk

  • @Thomas then, I would leave a static cache on the server, but then it’s very complicated. He asked "if it was a good one or not", so I will not exemplify, otherwise the question would be too broad.

  • @Leoletto well, the answer I already gave: Not a good one. Unless you need to do very specific things with each image loaded (like controlling the number of views for example). Now, if your question is about "how to do," then you would have to ask another question.

  • @Wallacemaxters understand, well I will use jquery to detect the useragent and load the extension according to the user obtained, assuming what was seen here, it is a bad idea also to use PHP to make video stream, or it will just require a little more processing, but will give a better performance to the customer?

  • 2

    @Leoletto you are asking a lot of questions on a subject that was specific to "it’s good or not to use PHP to upload images". I suggest you ask another (or other) questions. The space of comments is small to explain these things.

Show 12 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.