开发者

Apply CSS to image when browsing directly to it

This is probably a stretch, but is it possible to add css to an image in say the URL bar? For example, if I go directly to http://yoursite.com/someimage.p开发者_JS百科ng, can I apply css to it without embedding it in a html page?


Nope. When the HTTP response returns the binary image data only and not an HTML page there isn't a way for you to control how it is displayed.


As coreyward has already said, it's not possible to do this with just an image in the browser.

To add something of extra value, what you can do is a bit of url rewrite magic on the server. You can rewrite all requests for images to go to a specific page that handles the request.

In this way you'll be able to do which ever manipulations you'd like too.


No, that won't work. CSS is used to style the HTML, in case of images the img-tag, but not the picture itself.


You could probably use a mod_rewrite in your .htaccess that reroutes direct accesses of the image to a page that just includes the image and styles it. But then you have embedded it in a webpage, which isn't what you asked for. Also, you'd have to be careful to ensure that when your site includes the image the rewrite isn't used.

It would go something like:

RewriteCond %{HTTP_REFERER} !^http://(.+\.)?yoursite\.com/ [NC]
RewriteRule /(.*\.png)$ myimagehandler.php?img=$1  [QSA,L]

Where myimagehandler.php would include the image based on the query string and style it as you like.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜