Close Menu
    DevStackTipsDevStackTips
    • Home
    • News & Updates
      1. Tech & Work
      2. View All

      From Data To Decisions: UX Strategies For Real-Time Dashboards

      September 13, 2025

      Honeycomb launches AI observability suite for developers

      September 13, 2025

      Low-Code vs No-Code Platforms for Node.js: What CTOs Must Know Before Investing

      September 12, 2025

      ServiceNow unveils Zurich AI platform

      September 12, 2025

      DistroWatch Weekly, Issue 1139

      September 14, 2025

      Building personal apps with open source and AI

      September 12, 2025

      What Can We Actually Do With corner-shape?

      September 12, 2025

      Craft, Clarity, and Care: The Story and Work of Mengchu Yao

      September 12, 2025
    • Development
      1. Algorithms & Data Structures
      2. Artificial Intelligence
      3. Back-End Development
      4. Databases
      5. Front-End Development
      6. Libraries & Frameworks
      7. Machine Learning
      8. Security
      9. Software Engineering
      10. Tools & IDEs
      11. Web Design
      12. Web Development
      13. Web Security
      14. Programming Languages
        • PHP
        • JavaScript
      Featured

      Optimizely Mission Control – Part III

      September 14, 2025
      Recent

      Optimizely Mission Control – Part III

      September 14, 2025

      Learning from PHP Log to File Example

      September 13, 2025

      Online EMI Calculator using PHP – Calculate Loan EMI, Interest, and Amortization Schedule

      September 13, 2025
    • Operating Systems
      1. Windows
      2. Linux
      3. macOS
      Featured

      DistroWatch Weekly, Issue 1139

      September 14, 2025
      Recent

      DistroWatch Weekly, Issue 1139

      September 14, 2025

      sudo vs sudo-rs: What You Need to Know About the Rust Takeover of Classic Sudo Command

      September 14, 2025

      Dmitry — The Deep Magic

      September 13, 2025
    • Learning Resources
      • Books
      • Cheatsheets
      • Tutorials & Guides
    Home»News & Updates»CodeSOD: The Big Pictures

    CodeSOD: The Big Pictures

    May 6, 2025

    Loading times for web pages is one of the key metrics we like to tune. Users will put up with a lot if they feel like they application is responsive. So when Caivs was handed 20MB of PHP and told, “one of the key pages takes like 30-45 seconds to load. Figure out why,” it was at least a clear goal.

    Combing through that gigantic pile of code to try and understand what was happening was an uphill battle. Eventually, Caivs just decided to check the traffic logs while running the application. That highlighted a huge spike in traffic every time the page loaded, and that helped Caivs narrow down exactly where the problem was.

    <span class="hljs-variable">$first_image</span> = <span class="hljs-string">''</span>;
    <span class="hljs-keyword">foreach</span>(<span class="hljs-variable">$images</span> <span class="hljs-keyword">as</span> <span class="hljs-variable">$the_image</span>)
    { 
        <span class="hljs-variable">$image</span> = <span class="hljs-variable">$the_image</span>[<span class="hljs-string">'url'</span>];
     
      <span class="hljs-keyword">if</span>(<span class="hljs-title function_ invoke__">file_exists</span>(<span class="hljs-variable">$config</span>->base_url.<span class="hljs-string">'/uploads/'</span>.<span class="hljs-variable">$image</span>))
      {
        <span class="hljs-keyword">if</span>(<span class="hljs-variable">$first_image</span>==<span class="hljs-string">''</span>)
        {
          <span class="hljs-variable">$first_image</span> = <span class="hljs-variable">$image</span>;
        }
       
        <span class="hljs-variable">$image_dimensions</span> = <span class="hljs-string">'&w=648&h=432'</span>;
        <span class="hljs-variable">$get_dimensions</span> = <span class="hljs-title function_ invoke__">getimagesize</span>(<span class="hljs-string">'http://old.datacenter.ip.address/'</span>.<span class="hljs-variable">$config</span>->base_url.<span class="hljs-string">'/uploads/'</span>.<span class="hljs-variable">$image</span>);
        <span class="hljs-keyword">if</span>(<span class="hljs-variable">$get_dimensions</span>[<span class="hljs-number">0</span>] < <span class="hljs-variable">$get_dimensions</span>[<span class="hljs-number">1</span>])
          <span class="hljs-variable">$image_dimensions</span> = <span class="hljs-string">'&h=432'</span>;
    
        <span class="hljs-keyword">echo</span> <span class="hljs-string">'<li>'</span>.<span class="hljs-variable">$config</span>->base_url.<span class="hljs-string">'/timthumb.php?src='</span>.<span class="hljs-variable">$config</span>->base_url.<span class="hljs-string">'/uploads/'</span>.<span class="hljs-variable">$image</span>.<span class="hljs-string">'&w=125&h=80&zc=1'</span>), <span class="hljs-string">'javascript:;'</span>, <span class="hljs-keyword">array</span>(<span class="hljs-string">'onclick'</span>=><span class="hljs-string">'$('.image_gallery .feature .image').html(''</span>.<span class="hljs-variable">$config</span>->base_url.<span class="hljs-string">'/timthumb.php?src='</span>.<span class="hljs-variable">$config</span>->base_url.<span class="hljs-string">'/uploads/'</span>.<span class="hljs-variable">$image</span>.<span class="hljs-variable">$image_dimensions</span>.<span class="hljs-string">'&zc=1'</span>).<span class="hljs-string">'');$('.image_gallery .feature .title').show();$('.image_gallery .feature .title').html("'</span>.<span class="hljs-title function_ invoke__">str_replace</span>(<span class="hljs-string">'"'</span>, <span class="hljs-string">''</span>, <span class="hljs-variable">$the_image</span>[<span class="hljs-string">'Image Description'</span>]).<span class="hljs-string">'");$('.image_gallery .bar ul li a').removeClass('active');$('.image_gallery .bar ul li').removeClass('active');$(this).addClass('active');$(this).parents('li').addClass('active');sidebarHeight();curImg=$(this).attr('id');translate()'</span>,<span class="hljs-string">'id'</span>=><span class="hljs-variable">$img_num</span>)).<span class="hljs-string">'</li>'</span>;
        <span class="hljs-variable">$img_num</span>++;
      }
    }
    

    For every image they want to display in a gallery, they echo out a list item for it, which that part makes sense- more or less. The mix of PHP, JavaScript, JQuery, and HTML tags is ugly and awful and I hate it. But that’s just a prosaic kind of awful, background radiation of looking at PHP code. Yes, it should be launched into the Kupier belt (it doesn’t deserve the higher delta-V required to launch it into the sun), but that’s not why we’re here.

    The cause of the long load times was in the lines above- where for each image, we getimagesize– a function which downloads the image and checks its stats, all so we can set $image_dimensions. Which, presumably, the server hosting the images uses the query string to resize the returned image.

    All this is to check- if the height is greater than the width we force the height to be 432 pixels, otherwise we force the whole image to be 648×432 pixels.

    Now, the server supplying those images had absolutely no caching, so that meant for every image request it needed to resize the image before sending. And for reasons which were unclear, if the requested aspect ratio were wildly different than the actual aspect ratio, it would also sometimes just refused to resize and return a gigantic original image file. But someone also had thought about the perils of badly behaved clients downloading too many images, so if a single host were requesting too many images, it would start throttling the responses.

    When you add all this up, it meant that this PHP web application was getting throttled by its own file server, because it was requesting too many images, too quickly. Any reasonable user load hitting it would be viewed as an attempted denial of service attack on the file hosting backend.

    Caivs was able to simply remove the check on filesize, and add a few CSS rules which ensured that files in the gallery wouldn’t misbehave terribly. The performance problems went away- at least for that page of the application. Buried in that 20MB of PHP/HTML code, there were plenty more places where things could go wrong.

    [Advertisement]
    Utilize BuildMaster to release your software with confidence, at the pace your business demands. Download today!

    Source: Read More 

    Facebook Twitter Reddit Email Copy Link
    Previous ArticleQuantum Systems raises €160M for AI-powered aerial intelligence
    Next Article TEKEVER becomes the latest unicorn in Europe’s defencetech industry

    Related Posts

    News & Updates

    DistroWatch Weekly, Issue 1139

    September 14, 2025
    News & Updates

    Building personal apps with open source and AI

    September 12, 2025
    Leave A Reply Cancel Reply

    For security, use of Google's reCAPTCHA service is required which is subject to the Google Privacy Policy and Terms of Use.

    Continue Reading

    “Everybody’s jobs will be affected” — but NVIDIA’s CEO believes society can think its way out of AI-related job loss

    News & Updates

    CVE-2025-8807 – “Xujeff Tianti 天梯 Remote Missing Authorization Vulnerability”

    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-4844 – FreeFloat FTP Server CD Command Handler Buffer Overflow Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    CVE-2025-55192 – Tapo-Control GitHub Actions Code Injection Vulnerability

    Common Vulnerabilities and Exposures (CVEs)

    Highlights

    Development

    Intelligent Parsing and Formatting of Names in PHP Applications

    July 3, 2025

    Name of Person is a PHP package inspired by Basecamp’s name_of_person Ruby gem, which gives…

    CVE-2025-6781 – Copymatic – WordPress CSRF (Cross-Site Request Forgery)

    July 18, 2025

    DOGE Big Balls Ransomware Outlook

    April 22, 2025

    CVE-2025-23317 – NVIDIA Triton Inference Server HTTP Server Remote Code Execution Vulnerability

    August 6, 2025
    © DevStackTips 2025. All rights reserved.
    • Contact
    • Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.