Good day!

Can you please tell me how to display a formatted image in the browser using PHP functions for working with imagejpeg() image? In the JPG file, everything is perfectly displayed and subsequently correctly displayed, but for some reason the output to the file does not work. Actually, first output to the file:

 imagejpeg($img,'file.jpg',100); 

And in the browser:

 header('Content-type: image/jpeg'); imagejpeg($img); 

Bottom line: either nothing is displayed in the browser if the headers are not sent, or the image is in text form (which is also incorrect in general!). Help me please!

Thank!

  • make a check on is_writable ('file.jpg') - Serge
  • I'm not writing anywhere. I display the information from the stream as a graphic in the browser. - Salivan

1 answer 1

I think I know what the problem is, try this:

 imagejpeg($img,'file.jpg',100); header('Content-type: image/jpeg'); imagejpeg($img,null,100); 

See the first post in the manual

It may also be due to the fact that you are trying to output progressive jpg without using the imageinterlace () function. See the note in the same manual.

  • Unfortunately, no ... - Salivan
  • Well, then you can try to output to a file, get the file name and just surround it with the appropriate HTML tag. Something like this: echo '<img style = "margin:'. $ Margin.'px;" src = "'$ imgfilename'" height = "'. $ ht.'"> '; - igumnov
  • @ igumnov, memory is limited - there are too many pictures. - Salivan September
  • one
    @Asen. As far as I understand, they are dynamically generated there, then what prevents to generate it every time, then output and then delete it from the server, it will eventually end up in the browser cache and everything, and not the server, will not be there if the client refreshes the page once more there is nothing wrong with that. - igumnov
  • one
    @Asen And it is not necessary to delete them immediately after it is uploaded by the first user, you can gradually delete it depending on the amount of free space on the hosting and the number of accesses to this or that picture. That is, what is more often addressed to save, and to generate unpopular on the fly. And so a compromise between the amount of occupied memory and performance is very common. You want speed pay with memory. - igumnov