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 1mo ago

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 64% 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

[milon/barcode

Barcode generator like Qr Code, PDF417, C39, C39+, C39E, C39E+, C93, S25, S25+, I25, I25+, C128, C128A, C128B, C128C, 2-Digits UPC-Based Extention, 5-Digits UPC-Based Extention, EAN 8, EAN 13, UPC-A, UPC-E, MSI (Variation of Plessey code)

1.5k13.3M39](/packages/milon-barcode)[bkwld/croppa

Image thumbnail creation through specially formatted URLs for Laravel

510496.0k22](/packages/bkwld-croppa)[marc1706/fast-image-size

fast-image-size is a PHP library that does almost everything PHP's getimagesize() does but without the large overhead of downloading the complete file.

959.4M20](/packages/marc1706-fast-image-size)[char0n/ffmpeg-php

PHP wrapper for FFmpeg application

495225.1k1](/packages/char0n-ffmpeg-php)[goat1000/svggraph

Generates SVG graphs

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

Media Embed (for Laravel or as a standalone).

120370.4k](/packages/cohensive-embed)

PHPackages © 2026

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