Giter VIP home page Giter VIP logo

laravel-imageproxy's Introduction

laravel-imageproxy

A pure-PHP image proxy for Laravel.

Configuration

Publish the config file:

php artisan vendor:publish --tag=imageproxy-config

This'll create a new file at config/imageproxy.php.

Now, you can either directly edit that file, or add the following to your app's .env:

IMAGEPROXY_KEY=<your-secret-key>

You may have to run php artisan config:clear if you've previously cached your app's config.

Then, in your app, use either https://example.org/imageproxy/<hash>/<original-image-url> or something like https://example.org/imageproxy/<hash>/100x100/<original-image-url> to have images (or video) delivered through your domain (example.org, in this case) and over HTTPS.

The hash is calculated as follows:

$hash = hash_hmac('sha1', $url, config('imageproxy.secret_key'));

Fragments and query strings should be stripped away prior to this (and can be readded again after).

Some Notes

This package is designed to stream unedited resources (i.e., those linked to using the first URL type) right through to the client, so as to minimally burden your server. It passes on headers mostly unchanged, too, which helps prevent large videos blocking page load, and should make video scrubbing work.

The second URL type will try to resize remote images before delivering them to the client. Use it on (remote) user avatars, or Open Graph images. Use of this function requires PHP's Imagick extension. Resized images are cached (indefinitely, for now) using Laravel's Storage::put().

Finally, there's the following additional .env variables:

IMAGEPROXY_QUALITY=82
IMAGEPROXY_USER_AGENT=<your-user-agent-of-choice>
IMAGEPROXY_SSL_VERIFY_PEER=true
IMAGEPROXY_SSL_VERIFY_PEER_NAME=true

You might wish to, at your own risk, set the latter two to false to temporarily work around remote hosts' faulty SSL certificates. Additionaly, it may be needed to explicitly set a user agent in order for certain resources to load.

laravel-imageproxy's People

Contributors

janboddez avatar

Stargazers

 avatar

Watchers

 avatar  avatar  avatar

laravel-imageproxy's Issues

Some URL-encoded characters lead to a failing checksum validation

Other than strip fragments and remove dots, I tend not to prep (rawurlencode or otherwise) original URLs but rather plug them in Laravel's route() helper:

$newUrl = route('imageproxy', [$hash, ($size !== '' ? "$size/" : '').$url]).((substr($url, -1) === '/') ? '/' : '');

(That last bit re-adds a trailing slash if there used to be one. route() takes it off, it seems.)

Still, some encoding happens.

So either we do this explicitly before calculating the hash, or we take it into account validating the thing.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.