1

I have a PHP script that generates images. At this moment with every request PHP creates a temporary directory and fills it with the created images, then it sends the links to JS, which displays them in the browser in a special order.

Is there any way to pass these images directly from PHP to JS without a temporary directory?

I don't want to use temporary directories because it takes space and resources of server and I have to check that images download has been completed before deleting them.

I am thinking on using Json encoding and decoding but I don't like this because it's time consuming.

Is there another way to send images directly from PHP to JS?

There is a way to work with using src to php script with headers: Using PHP to send a certain image or How to generate image file using this PHP function?

But this I could only work with one image per request. I mean - if I have 5 or 6 divs with images I have to make 5 or 6 requests from JS to my php script. Is there a way to send multiple images from PHP to JS?

O.O
  • 127
  • 8
  • Set the image src attribute to you PHP script with a query parameter indicating which image ( `src="getImage.php?q=ball"`). Have the PHP script echo the image...done. – Randy Casburn May 12 '18 at 21:43
  • Randy, Do you mean I need to bind my JS to php script, which create images? – O.O May 12 '18 at 21:48
  • Close-voters and @AndrewL this is not an unreasonable question. It's narrowly scoped, there are a few ways to do this, the most valid one being answered already, and it's clear enough that no code is really needed. – Félix Adriyel Gagnon-Grenier May 12 '18 at 21:59
  • 2
    @O.O People are free to vote however they wish. There is nothing wrong with that, nor with the people here. – Félix Adriyel Gagnon-Grenier May 12 '18 at 22:00
  • 1
    @O.O — It is "difficult" to tell you how to adjust your code to output the image directly without knowing how the code currently works. – Quentin May 12 '18 at 22:05
  • @O.O - You would do this in your image tags: `` -- your PHP script reads the query parameter, generates the image and echos it back to the browser. Your PHP script will have to send out the correct MIME header prior to sending the image. – Randy Casburn May 12 '18 at 22:05
  • @O.O - see: https://stackoverflow.com/questions/18252668/using-php-to-send-a-certain-image – Randy Casburn May 12 '18 at 22:07
  • Randy, like this way on PHP side: header('X-Accel-Redirect: storage/file.jpeg'); header('Content-Type: application/octet-stream'); header('Content-Disposition: attachment; filename="My new file.jpeg"'); exit(); I have Nginx. – O.O May 12 '18 at 22:09
  • Possible duplicate of [How to generate image file using this PHP function?](https://stackoverflow.com/questions/11849552/how-to-generate-image-file-using-this-php-function) – Randy Casburn May 12 '18 at 22:13
  • Randy, thank u 4 this link! https://stackoverflow.com/questions/18252668/using-php-to-send-a-certain-image – O.O May 12 '18 at 22:15
  • The way with src and headers does not work if I want to send multiple images at one moment. – O.O May 12 '18 at 22:26
  • @FélixGagnon-Grenier I agree that it is clear but check Quentin's comment above on why a code snippet is needed. – AndrewL64 May 12 '18 at 22:46
  • @AndrewL already read it :) It's not directly asked in the question to fix code. It's asked how to proceed, which the answer here is saying. In any event, I'm totally ok with you casting your close votes, I said my opinion on it, but OP seems intent on changing the requirements, so I might switch to the "let's close" side in a while. – Félix Adriyel Gagnon-Grenier May 12 '18 at 23:01

2 Answers2

0

My first idea would be to encode those images with base64_encode and pass that encoded data to your JavaScript. With this approach, you don't have to save those images anywhere.

More information on displaying base64 encoded images: Base64 Encoding Image

Paul
  • 932
  • 2
  • 8
  • 15
0

What you can do to not overload your server is to use an image hosting external website, like imgur, giphy, etc. This will scale the image for you in different size, see their api.

Here I am going to share a little hack, to host permanently contents into the internet archive. it is easy to implement from php.

Please don't use it for high traffic.

1/ Host your content on your site, create a url_link.

2/ Post your link to archive.org:

https://web.archive.org/save/*url_link*

3/ Response is a link on the form:

https://web.archive.org/web/20180XXXXXX626if_/*url_link*

4/ Modify the link, remove if_ this is the permanent link for the next centuries.

https://web.archive.org/web/20180XXXXXX626/*url_link*

5/ Delete content from your server

NVRM
  • 11,480
  • 1
  • 88
  • 87