PHPackages                             dhaval/image-uploader - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Image &amp; Media](/categories/media)
4. /
5. dhaval/image-uploader

ActiveLibrary[Image &amp; Media](/categories/media)

dhaval/image-uploader
=====================

A simple and elegant PHP library for securely uploading images

622.4k↑57.9%17[2 PRs](https://github.com/DhavalKapil/image-uploader/pulls)PHP

Since Feb 27Pushed 4y ago4 watchersCompare

[ Source](https://github.com/DhavalKapil/image-uploader)[ Packagist](https://packagist.org/packages/dhaval/image-uploader)[ RSS](/packages/dhaval-image-uploader/feed)WikiDiscussions master Synced today

READMEChangelogDependenciesVersions (1)Used By (0)

image-uploader
==============

[](#image-uploader)

> A simple and elegant PHP library for securely uploading images

The aim of this project is to provide users with a **simple** interface to upload images in their applications in a **highly secure** manner while also being **highly customizable**.

The uploaded images go through a lot of security checks before they are uploaded.

Features
--------

[](#features)

1. A **clean** interface - **easy to use**.
2. Uploaded images are referenced by a unique identifier - **removes the hassles of storage from the user**.
3. **Customizable** - users of this library can operate on the uploaded images(i.e. crop, add filters, etc.) without even worrying about the uploading process.
4. **Highly secure** - a lot of security checks were compiled from many sources into this one library.

Security Checklist
------------------

[](#security-checklist)

1. Checks `$_FILES` parameter for correctness.
2. Checks uploaded image file size, where `min` and `max` sizes are given by the user.
3. Checks upload errors by analyzing the variable `$_FILES["image-name"]["error"]`
4. Checks for unwanted bytes in the first 100 bytes of the uploaded image.
5. Checks image's mime type by using the `getimagesize` function. Does not rely on `$_FILES["image-name"]["type"]`.
6. Reprocesses the image using the [GD](http://php.net/manual/en/book.image.php) library to remove any malicious code.
7. Renames the uploaded image, uses `md5` hash(with a user given salt) as the new image name.
8. Uses [move\_uploaded\_file](http://php.net/manual/en/function.move-uploaded-file.php) to properly move the uploaded file to the target destination as well as setting appropriate permissions.

Prerequisites
-------------

[](#prerequisites)

1. Successfully tested on PHP &gt;= 5.5
2. [GD](http://php.net/manual/en/book.image.php) library

```
sudo apt-get install php5-gd

sudo service apache2 restart

```

Installation
------------

[](#installation)

### Composer

[](#composer)

Add the following in your composer.json:

```
{
  "require": {
    "dhaval/image-uploader": "dev-master"
  }
}
```

And then run

```
php composer.phar install

```

Include `image-uploader` in your php code

```
require("vendor/autoload.php");
```

### Manually

[](#manually)

Clone this repository in your project's home directory

```
git clone https://github.com/dhavalkapil/image-uploader

```

Include `ImageUploader.php` in your php code.

```
require("image-uploader/src/ImageUploader.php");
```

Usage
-----

[](#usage)

### Frontend

[](#frontend)

First of all create an HTML form to allow people to upload files

```

```

### Backend

[](#backend)

Create an instance of `ImageUploader` and set appropriate attributes

```
$imageUploader = new ImageUploader();

// Compulsory
$imageUploader->setPath("my/upload/image/dir");   // The directory where images will be uploaded

// The rest are optional
$imageUploader->setSalt("my_application_specific_salt");  // It is used while hashing image names
$imageUploader->setMinFileSize(0);                           // Set minimum file size in bytes
$imageUploader->setMaxFileSize(100000);                      // Set maximum file size in bytes
```

You can also pass these attributes directly while calling the constructor.

It is advised to create the upload directory(passed above in path) as follows:

```
mkdir upload_dir
chmod 755 upload_dir
[sudo] chown www-data:www-data upload_dir

```

**Note**: `www-data` is the user that apache runs under. You might need to change it depending on your machine.

To upload an image use the `upload` function

```
$imageUploader->upload($_FILES["my_image"], "my_id");
```

Here, `my_image` was the name of the input element in your HTML and `my_id` is a unique identifier for your image. This needs to be decided by the user.

To serve an image at a particular page use the `serve` function

```
$imageUploader->serve("my_id");
```

The image uploaded with this particular identifier will be served. Hence, there will be no direct link to the image itself. This allows for images to be served at say /user/&lt;user\_id&gt;/image. Where `user_id` might be used as an identifier for user images.

Check out sample [example](https://github.com/DhavalKapil/image-uploader/tree/master/example) for more details.

### Additional Usage

[](#additional-usage)

You can also check whether a particular image exists for a given identifier using the `exists` function

```
$result = $imageUploader->exists("my_id");
```

It returns a boolean value.

You can also customize the uploaded image, say by adding filters, cropping, etc by passing a `callback` function to the `upload` or `serve` function.

```
$callback = function(&$image) {
 imagefilter($image, IMG_FILTER_GRAYSCALE);
};

$imageUploader->upload($_FILES["my_image"], "my_id", $callback);
```

This `callback` accepts a reference `&$image`, which is an image resource used by the [GD](http://php.net/manual/en/book.image.php) library. If passed to the `upload` function, it is called just before the image is being saved. Whereas, if passed to the `serve` function, it is called just before the image is buffered to the browser.

Contribution
------------

[](#contribution)

Contributions are welcome to this repository. If you know of any other security vulnerability, any bug, etc. feel free to file [issues](https://github.com/DhavalKapil/image-uploader/issues) and submit [pull requests](https://github.com/DhavalKapil/image-uploader/pulls).

Developers
----------

[](#developers)

- [Dhaval Kapil](https://github.com/DhavalKapil)
- [Aditya Prakash](https://github.com/adiitya)

License
-------

[](#license)

image-uploader is licensed under the MIT license.

###  Health Score

30

—

LowBetter than 67% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity34

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 89.3% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://www.gravatar.com/avatar/f419717fd145d39da19c78a2f71276968e1dea3f1efe258572347d0e77d6eb2e?d=identicon)[dhavalkapil](/maintainers/dhavalkapil)

---

Top Contributors

[![DhavalKapil](https://avatars.githubusercontent.com/u/6170016?v=4)](https://github.com/DhavalKapil "DhavalKapil (25 commits)")[![adiitya](https://avatars.githubusercontent.com/u/6408188?v=4)](https://github.com/adiitya "adiitya (2 commits)")[![iAmMrinal0](https://avatars.githubusercontent.com/u/890062?v=4)](https://github.com/iAmMrinal0 "iAmMrinal0 (1 commits)")

### Embed Badge

![Health badge](/badges/dhaval-image-uploader/health.svg)

```
[![Health](https://phpackages.com/badges/dhaval-image-uploader/health.svg)](https://phpackages.com/packages/dhaval-image-uploader)
```

###  Alternatives

[liip/imagine-bundle

This bundle provides an image manipulation abstraction toolkit for Symfony-based projects.

1.7k37.7M206](/packages/liip-imagine-bundle)[bkwld/croppa

Image thumbnail creation through specially formatted URLs for Laravel

508489.5k19](/packages/bkwld-croppa)[intervention/gif

Native PHP GIF Encoder/Decoder

5518.1M9](/packages/intervention-gif)[maestroerror/php-heic-to-jpg

Converts HEIC/HEIF image to JPG type, without any dependencies

181705.3k1](/packages/maestroerror-php-heic-to-jpg)[goat1000/svggraph

Generates SVG graphs

130831.7k3](/packages/goat1000-svggraph)[cohensive/embed

Media Embed (for Laravel or as a standalone).

118365.3k](/packages/cohensive-embed)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
