Programmatically combining images in PHP

后端 未结 2 1567
-上瘾入骨i
-上瘾入骨i 2021-01-14 04:54

I\'m a big fan of Yahoo\'s recommendations for speeding up websites. One of the recommendations is to combine images where possible to cut down on size and the number of req

2条回答
  •  花落未央
    2021-01-14 05:30

    I wouldn't go down this route if I were you. Sure, you may save a few bytes in protocol overhead by reducing the number of requests, but this would more-tha-likely end up being self-defeating.

    Imagine this scenario: A blog site, whose front page has 10 articles at a time. Each article has it's own image associated with it. To save a byte or two of transfer time, you programatically create a composite image of all 10 article images. You now have one of two problems.

    1. You must update the composite image each time a new post is made, as the most recent 10 images will have a modified set of content.
    2. You decide to create a new composite each request, on the fly.

    Obviously, #1 is preferable here, and would not be difficult to implement. However, what if a user searches for all posts tagged with the word "SQL"? You are unlikely to have a composite image of the first 10 results already created for this simple query, let alone a more complex one. Also, what happens if you want to update or delete an image? Once again you'd have to trigger the background creation of the composite.

    How about an RSS aggregator, like Google Reader? It wouldn't have the required logic to figure out which portion of a composite image it would need to display, and would probably display the full image. (I mention Google Reader because I very rarely visit blog sites directly, tending to trust to an RSS aggregation service like Reader)

    If it were me, I'd leave the single images alone. With modern connection speeds, the tradeoff between additional bandwidth overhead and on-server processing time is unlikely to win you and great gains.

    Having said that, if you decide to go down this route anyway, I'd say the GD library is an excellent place to start.

提交回复
热议问题