I have a PHP page containing a form(method post).
On the submit of the button, I create an image using imagecreatetruecolor and imagesetpixel.
Then, I want to show this picture after the form.
I tried saving the image to a file using imagepng($img, 'test.png'); and echo '<img src="test.png">';, but that doesn't get the new image. How can I achieve that?
            Asked
            
        
        
            Active
            
        
            Viewed 58 times
        
    -1
            
            
        - 
                    Make sure that the absolute path you stored it in is correct, in contrast to the relative path you need to use in your `– droopsnoot Sep 02 '20 at 12:08 
- 
                    Does this answer your question? [Displaying php generated image on a page](https://stackoverflow.com/questions/3734867/displaying-php-generated-image-on-a-page) – Ron Sep 02 '20 at 12:11
- 
                    @Ginso do you want to show the preview of image and upload image after that? – Ankit Jindal Sep 02 '20 at 12:15
- 
                    i just want to show the image somehow(such that i can rightclick and "copy image"). using the the absolute path changes nothing, i still get the old image until i force-reload. – Ginso Sep 03 '20 at 14:47
- 
                    If you have to use a force reload of your page, then this is a caching issue, not anything related to PHP itself – Nico Haase Oct 02 '20 at 14:00
1 Answers
0
            
            
        You need to determine where your image is being created when you call imagepng(), then ensure your image tag has the correct path to it.
For example:
$root = $_SERVER['DOCUMENT_ROOT'];
imagepng($img, $root . '/images/test.png')
Your image will then be available at:
https://example.com/images/test.png
And you can set your image source accordingly:
<img src="/images/test.png">
 
    
    
        BadHorsie
        
- 14,135
- 30
- 117
- 191
- 
                    the image is saved correctly, my problem is, that the resource is not updated until i force-reload the page – Ginso Sep 03 '20 at 14:48
- 
                    It's probably just a caching issue then. See here https://stackoverflow.com/questions/728616/disable-cache-for-some-images – BadHorsie Sep 03 '20 at 15:46
 
    