How do you redirect a PHP page to an IMG?

I have a blog website. We provide some HTML that the user can embed on his page, and he has something like this:

<img src = "http://example.com/img.jpg" />

Unfortunately, I had to move images from time to time. Every time I have to move an image, the image no longer works for people who put the code on their site.

I am wondering if there is a way in PHP so that I can do something like this:

<img src = "http://example.com/getImage.php?id=523" />

And let getImage.php really redirect to the actual URL of the image (viewed from my database with this ID). That way, I can have one URL for the user, and if I ever need to move the image, I just do it in my database and the user background is still working.

Any suggestions?

+3
source share
2 answers

Yes. You can use the following for transparent JPEG playback with php:

header('Content-type: image/jpeg');
readfile('/path/to/file.jpg');
exit;

Alternatively, you can simply redirect the image URL:

header('Location: /web/path/to/file.jpg');
exit;
+11
source
header("Location: $url");
+3
source

Source: https://habr.com/ru/post/1756267/


All Articles