Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7412363
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T06:31:42+00:00 2026-05-29T06:31:42+00:00

I generate dynamically graphic in my controller: if not file exists (unique filename) then

  • 0

I generate dynamically graphic in my controller:

  1. if not file exists (unique filename) then generate graphic and save it

  2. return RedirectResponse($url)

(3. I’ve tried also:

    $fp = fopen($url, "rb");
    $str = stream_get_contents($fp);
    fclose($fp);

    $response = new Response($str, 200);
    $response->headers->set('Content-Type', 'image/png');
    return $response;

)

I need it to generate dynamically userbars for users [on this graphic some text will be automatically updating if some changes will be done on my websites].

So it’s important for me that this script works fast and doesn’t kill my server/bandwitch. To achieve that I’ve made cache where all generated graphics are stored. But i’ve got a problem with speed, solutions which i’ve described above are pretty slow, when i’ve tested it in chrome with developer tool (F12) then it shows me that both solution (2. and 3.) takes about ~1s (when the same graphics [without controllers etc. just direct url to .png] loads 44ms).

So if only for me it takes about ~1s per graphic then if for example 1000 users will generate at the same time this graphic it can slower my website I think.

Please help, do you know any faster ways to do that?

PS. Redirect isn’t most desire thing for me, because I would prefer to leave url as it is, for example smth/username/userbar/ insted of smth/img/userbars/cache/blabla.png. But if it will be much faster then I can stand it :).

EDIT: To be clear: I’m not looking for solution to cache images, my problem is that returning images via controller (already generated images, just from ftp) takes about 1s, when normally if you do for example

           <img src="direct_url_to_png"> 

it takes about 44ms.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T06:31:43+00:00Added an answer on May 29, 2026 at 6:31 am

    You can use Varnish to cache the images, Varnish is a HTTP proxy that sits in front of the webserver. It takes some time & knowledge to configure it, but it is a very good piece of software.

    Alternatively, you may cache the images in memory using shared memory, memcache, or redis. This would probably be less effectly than Varnish, but it would save the filesystem IO.

    You should test where the most time is spent, I expect it’s a combination of file I/O and PHP parsing time, but as we saying in Dutch, “Measuring is knowing” 😉

    Here’s a simple example which should give you the general idea on how to do that:

    <?php
    class MyClass
    {
      function __construct()
      {
         $this->timer = array(
           'start' => microtime(True);
         );
       }
    
      function myAction()
      {
        $this->timer['startaction'] = microtime(True);
    
        $fp = fopen($url, "rb");
        $this->timer['startstream'] = microtime(True);
        $str = stream_get_contents($fp);
        fclose($fp);
        $this->timer['endstream'] = microtime(True);
    
        $response = new Response($str, 200);
        $response->headers->set('Content-Type', 'image/png');
    
        $this->timer['endaction'] = microtime(True);
        print_r($this->timer)
        return $response;
      }
    }
    

    Another thing that may be an option is to use HTML/CSS to show the graphs. This may or may not work depending in what your graphs look like but I’ve used this in the past and it worked quite well.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'd like to dynamically generate content and then render to a PDF file. This
Is there any way to dynamically generate an swf file? Or insert variables after
I need to dynamically generate some macros into a .h configuration file that C
I am trying to dynamically generate JavaScript with just a URL get request. I
So I want to dynamically generate a MIDI file on a web request.Coming from
I'm trying to dynamically generate root folder in a url that's in the body
I want to dynamically generate bands, that will then be grouped in reports. My
I'm trying to dynamically generate an image file that will be composed by other
Based on the data, I need to dynamically generate a graphic in my ASP.NET
If I generate methods dynamically on runtime and then call them - how can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.