-1

I am trying to figure out if it's possible to rename images "on the fly" - by that meaning if you can do some sort of rename function, so in code the imagenames are changed, but the orginal image name is not.

Example:

I have a database with a table containing 2 rows, like this:

new_diverse    id   tekst   active       pic
----------------------------------------------
Row1            1    yellow     1    testimage.jpg
Row2            2    green      1    testimage.jpg 

<img src="/images/testimage.jpg" />

Let's say on page yellow.php the row id # 1 is called and on page green.php, row id # 2 is called.

Both rows link to the same jpg image, but I would like for the code (the ) to have the color from the "tekst" in the table as filename instead, so if you right click the image, properties would state that the filename would be eg. green.jpg instead of testimage.jpg - is this possible?

Someone told me that GDlib perhaps could do the trick, but I have no knowledge of this.

Thank you so much in advance

EDIT --

Sorry for the late response to update this question.

What I am trying to accomplish by doing this is in regards to SEO. I'm in the process of developing a website with several languages (I believe it'll be in 10 languages, when done). For each page there will be images, and I would like to be as good in SEO as possible and therefore I would like to have the images named in their respective languages without having to make 10 identical copies of the images only with the filename in difference. Therefore I am looking for a way to write a filename in a specific language "on the fly".

E.g. I have an image named "2012-nice-red-mustang.jpg" in my root folder on my webhotel. When I access my page in english, the filename of the image would be as the original, but when viewing the spanish page with the image, the filename has been renamed to "2012-bonito-rojo-mustang.jpg" (where the translated filename is retrieved from a table in a database).

I hope this clarifies the problem and what I am trying to do.

MazeyMazey
  • 303
  • 1
  • 2
  • 14
  • 1
    So you want to use a filename in the URL that is not equal to the filename on the filesystem? There are many different ways to do that, and without some more information about what you're really trying to achieve, there's no way to tell which is best for you. GD is certainly a possibility, but so is simple URL rewriting. – jornane Aug 06 '14 at 22:21
  • Hi Yorn, I have updated my question to clarify the subject – MazeyMazey Sep 09 '14 at 19:51

1 Answers1

0

You need a few different pieces to make this work:

  1. Send the browser's image requests to a php script instead of just an image file on disk. This can be done in your http server (Apache's mod_rewrite, for example) or directly in the html (<img src="/image.php?name=testimage.jpg" alt=""/>).
  2. Set the appropriate header so the browser will correctly interpret the response as an image file. Normally this is done for you by your http server (Apache). If it's a jpg, that would look like this: header('Content-Type: image/jpeg');
  3. Send the image data to the browser. The easiest way to do this would be to read it straight from an image file (which could be outside of the web root if you don't want people to access it directly): readfile($actualFilePath); If you wanted to generate or modify the image on the fly, though, this is where GD would come into play.

If you're looking for more info on how the mod_rewrite part would work, you could look at this question: Apache - rewrite images to php file with .htaccess

Community
  • 1
  • 1
grossvogel
  • 6,694
  • 1
  • 25
  • 36
  • Hi Grossvogel, I have updated my question, so you can see more in detail what I am looking for. I would like to make it look like a "real" path to an images, instead of the image.php?name=testimage.jpg. I hope you might be able to help. – MazeyMazey Sep 11 '14 at 13:28
  • @MazeyMazey: I hinted at this when I mentioned *mod_rewrite* in #1 above. For example, *mod_rewrite* can cause a user's request for `/images/2012-bonito-rojo-mustang.jpg` to internally call the script `image.php?name=2012-bonito-rojo-mustang.jpg` instead. That script can do the DB lookup, set the correct headers, and return the image data. The user will never know the difference. (If you're not using Apache as your web server, there is probably an analogous mechanism, like the [`nginx rewrite module`](http://nginx.org/en/docs/http/ngx_http_rewrite_module.html).) – grossvogel Sep 11 '14 at 16:08
  • thanks for your quick reply. My web server has Apache installed and I do also have a .htaccess file, but I am quite new to Apache and Mod_rewrite. I do have a little experience in PHP, but I guess this is a bit more "advanced" that I'm used to, so I really hope that maybe you can give me an example on how to do this like what you wrote in your answer. From what I understand in you first example, it looks to me as the code will display the image source as PHP with a variable? The user won't notice this, but the Google crawler will and I'm afraid that that will affect the SEO. – MazeyMazey Sep 11 '14 at 21:13
  • @MazeyMazey: Google won't know either, because the HTML markup will contain the image URL you specify even if that's not where the file is on your server. If you want to learn more about mod_rewrite, there are lots of [resources on the web](https://www.google.com/search?q=mod_rewrite) and [questions on SO](http://stackoverflow.com/search?q=mod_rewrite). – grossvogel Sep 12 '14 at 19:57
  • Thanks @grossvogel - I have searched around a read a bit, and actually have come up with maybe an easier way of accomplishing what I need - The fastest and probably for me the best way would be to be able to add a "product-text" to the filename, and seperate with e.g. --- and then the filename. Like this: `RewriteRule ^(p/.+?)--.+$ /$1.jpg [L,NC]` Unfortunately I can't get this to work though. I have placed it in my .htaccess and have tried with simple img src `` but it does not display the image. I really hope that maybe you could hint me a bit? – MazeyMazey Sep 12 '14 at 23:04
  • When I come to think about it, my URL on the pages where I want the altered image names are rewritten as well. What I do want to do is to make any image ready and able to be altered when "triggered" with the `---`I have tried to update the mod_rewrite to what I think it could be, but unfortunately still no luck. `RewriteRule ^(.*)/(.*)/(.+?)--.+$ /(.*)/(.*)/$3.jpg [L,NC]` My URL right now looks like this: `http://www.site.dk/kohlmann/dk/warmth/varmelegemer/2/5.php` images path like this `http://www.site.dk/kohlmann/images/tube_heating_elements.jpg` – MazeyMazey Sep 12 '14 at 23:24