Using ImageResizer with custom query string

ε祈祈猫儿з 提交于 2019-12-24 13:22:52

问题


I'm using ImageResizer while the clients find images with this format: http://website/imagehandler.aspx?id=120&width=240&height=300 This is a different way of addressing images using folder hierarchy, as I use id to find the image location.

Formerly, I wrote a custom plugin for my case of usage but I had some thread-safety issues with that solution, as mentioned here: imageresizer-shows-wrong-image. Nathanael (developer of image resizer) advised me not to use custom plugin and use the default ImageResizingModule.

How I can maintain my URL api (using id in query string) while leveraging ImageResizer?

EDIT :

This is how I have developed my own Plugin

public class MyImageResizerPlugin : IPlugin, IQuerystringPlugin, IVirtualImageProvider
{
    private IVirtualFile _imageVirtualFile;

    public IPlugin Install(Config c)
    {
        c.Plugins.add_plugin(this);
        return this;
    }

    public bool Uninstall(Config c)
    {
        c.Plugins.remove_plugin(this);
        return true;
    }

    public bool FileExists(string virtualPath, NameValueCollection queryString)
    {
        if (!virtualPath.ToLower().Contains("imagehandler"))
        {
            return false;
        }
        _imageVirtualFile = new ImageLocalFile(queryString);
        return _imageVirtualFile.VirtualPath != null;
    }

    public IVirtualFile GetFile(string virtualPath, NameValueCollection queryString)
    {
        return _imageVirtualFile;
    }

    public IEnumerable<string> GetSupportedQuerystringKeys()
    {
        return new [] { "id", "width", "height" };
    }

    public class ImageLocalFile : IVirtualFileWithModifiedDate, IVirtualFileSourceCacheKey
    {
        private readonly ResizeSettings _query;
        private readonly int id;

        public ImageLocalFile(NameValueCollection q)
        {
            _query = new ResizeSettings(q);
            id = _query.get("id", 0);
            VirtualPath = path of file based on id
        }

        public Stream Open()
        {
            return new FileStream(VirtualPath, FileMode.Open, FileAccess.Read);
        }

        public string VirtualPath { get; private set; }

        public DateTime ModifiedDateUTC
        {
            get
            {
                return File.GetCreationTimeUtc(VirtualPath);
            }
        }

        public string GetCacheKey(bool includeModifiedDate)
        {
            return VirtualPath +
                   PathUtils.BuildQueryString(NameValueCollectionExtensions.Keep(_query, "id", "width", "height"));
        }
    }
}

And this is what I have in web.config

<modules>
    <add name="ImageResizingModule" type="ImageResizer.InterceptModule" />
</modules>
<rewrite>
    <rules>
    <rule name="ImageHandler">
        <match url="^imagehandler.*" />
        <action type="Rewrite" url="imagehandler.jpg" />
    </rule>
    </rules>
</rewrite>

<resizer>
    <plugins>
        <add name="ImageUtilities.MyImageResizerPlugin" />
    </plugins>
    <clientcache minutes="10080" />
</resizer>

回答1:


Any plugin is installed once. So you should not store _imageVirtualFile in a private field in your plugin. Evaluate that _imageVirtualFile in GetFile() method.




回答2:


A. URL Rewriting. If you have a low-latency lookup table, you can handle the Config.Current.Rewrite event and change the file that is actually being loaded.

B. Inherit from BlobProviderBase and handle I/O yourself. If the images aren't on local storage, this might be an option. It appears you had trouble with this path but refused to show any source code. Perhaps a psychic could step in and help with this one?



来源:https://stackoverflow.com/questions/32410278/using-imageresizer-with-custom-query-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!