예제 #1
0
파일: Tinify.cs 프로젝트: vlad-G/tinify
    /// <summary>
    /// Upload image to Tinify and attempt to shrink it, lossless.
    /// </summary>
    /// <param name="inputFile">File to upload.</param>
    /// <param name="outputFile">Filename to save to.</param>
    /// <param name="store">Amazon S3 credentials and information.</param>
    /// <returns>JSON with info about the upload/shrink.</returns>
    public TinifyResponse Shrink(string inputFile, string outputFile = null, TinifyOptionsStore store = null)
    {
        var response = new JavaScriptSerializer()
                       .Deserialize <TinifyResponse>(
            getResponse(request(binaryFile: inputFile)));

        if (outputFile == null ||
            response.output == null ||
            string.IsNullOrEmpty(response.output.url))
        {
            return(response);
        }

        if (store != null)
        {
            executeOption(
                response,
                null,
                null,
                null,
                null,
                store);
        }

        var webClient = new WebClient();

        webClient.DownloadFile(
            response.output.url,
            outputFile);

        return(response);
    }
예제 #2
0
파일: Tinify.cs 프로젝트: nagilum/tinify
 /// <summary>
 /// Scales the image down proportionally so that it fits within the given dimensions. You must provide both a width and a height.
 /// </summary>
 /// <param name="response">Response object from a upload/shrink operation.</param>
 /// <param name="width">Width to scale within.</param>
 /// <param name="height">Height to scale within.</param>
 /// <param name="outputFile">Filename to save to.</param>
 /// <param name="store">Amazon S3 credentials and information.</param>
 /// <returns>Stream</returns>
 public Stream Fit(TinifyResponse response, int width, int height, string outputFile = null, TinifyOptionsStore store = null)
 {
     return executeOption(response, width, height, "fit", outputFile, store);
 }
예제 #3
0
파일: Tinify.cs 프로젝트: nagilum/tinify
    /// <summary>
    /// Execute a set of options against a previous uploaded/shrinked file.
    /// </summary>
    /// <param name="response">Response object from a upload/shrink operation.</param>
    /// <param name="width">Width to scale within.</param>
    /// <param name="height">Height to scale within.</param>
    /// <param name="method">Scale method to apply.</param>
    /// <param name="outputFile">Filename to save to.</param>
    /// <param name="store">Amazon S3 credentials and information.</param>
    /// <returns>Stream</returns>
    private Stream executeOption(TinifyResponse response, int? width, int? height, string method = null, string outputFile = null, TinifyOptionsStore store = null)
    {
        var options = new TinifyOptions();

        if (method != null &&
            (width.HasValue ||
             height.HasValue)) {
            options.resize = new TinifyOptionsResize {
                method = method
            };

            if (width.HasValue)
                options.resize.width = width.Value;

            if (height.HasValue)
                options.resize.height = height.Value;
        }

        if (store != null)
            options.store = store;

        var stream = request(
            "POST",
            response.output.url,
            null,
            options);

        writeStreamToDisk(
            stream,
            outputFile);

        return stream;
    }
예제 #4
0
파일: Tinify.cs 프로젝트: nagilum/tinify
    /// <summary>
    /// Upload image to Tinify and attempt to shrink it, lossless.
    /// </summary>
    /// <param name="inputFile">File to upload.</param>
    /// <param name="outputFile">Filename to save to.</param>
    /// <param name="store">Amazon S3 credentials and information.</param>
    /// <returns>JSON with info about the upload/shrink.</returns>
    public TinifyResponse Shrink(string inputFile, string outputFile = null, TinifyOptionsStore store = null)
    {
        var response = new JavaScriptSerializer()
            .Deserialize<TinifyResponse>(
                getResponse(request(binaryFile: inputFile)));

        if (outputFile == null ||
            response.output == null ||
            string.IsNullOrEmpty(response.output.url))
            return response;

        if (store != null)
            executeOption(
                response,
                null,
                null,
                null,
                null,
                store);

        var webClient = new WebClient();

        webClient.DownloadFile(
            response.output.url,
            outputFile);

        return response;
    }
예제 #5
0
파일: Tinify.cs 프로젝트: vlad-G/tinify
 /// <summary>
 /// Scales the image down proportionally. You must provide either a target width or a target height, but not both.
 /// </summary>
 /// <param name="response">Response object from a upload/shrink operation.</param>
 /// <param name="width">Width to scale within.</param>
 /// <param name="height">Height to scale within.</param>
 /// <param name="outputFile">Filename to save to.</param>
 /// <param name="store">Amazon S3 credentials and information.</param>
 /// <returns>Stream</returns>
 public Stream Scale(TinifyResponse response, int width, int height, string outputFile = null, TinifyOptionsStore store = null)
 {
     return(executeOption(response, width, height, "scale", outputFile, store));
 }
예제 #6
0
파일: Tinify.cs 프로젝트: vlad-G/tinify
    /// <summary>
    /// Execute a set of options against a previous uploaded/shrinked file.
    /// </summary>
    /// <param name="response">Response object from a upload/shrink operation.</param>
    /// <param name="width">Width to scale within.</param>
    /// <param name="height">Height to scale within.</param>
    /// <param name="method">Scale method to apply.</param>
    /// <param name="outputFile">Filename to save to.</param>
    /// <param name="store">Amazon S3 credentials and information.</param>
    /// <returns>Stream</returns>
    private Stream executeOption(TinifyResponse response, int?width, int?height, string method = null, string outputFile = null, TinifyOptionsStore store = null)
    {
        var options = new TinifyOptions();

        if (method != null &&
            (width.HasValue ||
             height.HasValue))
        {
            options.resize = new TinifyOptionsResize {
                method = method
            };

            if (width.HasValue)
            {
                options.resize.width = width.Value;
            }

            if (height.HasValue)
            {
                options.resize.height = height.Value;
            }
        }

        if (store != null)
        {
            options.store = store;
        }

        var stream = request(
            "POST",
            response.output.url,
            null,
            options);

        writeStreamToDisk(
            stream,
            outputFile);

        return(stream);
    }