Need help with bulletproof?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

samayo
353 Stars 80 Forks MIT License 231 Commits 6 Opened issues

Description

PHP secure Image uploader, with a nice API

Services available

!
?

Need anything else?

Contributors list

# 69,365
HTML
vuejs2
PHP
vuex
135 commits
# 468,232
PHP
Securit...
image-u...
5 commits
# 406,749
PHP
HTML
WordPre...
version...
5 commits
# 32,280
Gulp
CSS
React
TypeScr...
3 commits
# 59,139
SQLite
PHP
mariadb
angular...
1 commit
# 84
netlify
TypeScr...
GraphQL
angular...
1 commit
# 620,504
PHP
Securit...
image-u...
1 commit
# 598,229
PHP
Securit...
image-u...
1 commit
# 175,982
C#
keystro...
.NET
WPF
1 commit
# 618,050
PHP
Securit...
image-u...
1 commit

BULLETPROOF Build Status

Latest Stable Version Total Downloads Scrutinizer Code Quality Gitter chat License

Bulletproof is a single-class library to upload images in PHP with security.

Install

Using git

bash
$ git clone https://github.com/samayo/bulletproof.git
Or composer
bash
$ composer require samayo/bulletproof:4.0.*
Or [download it manually][bulletproof_archive] based on the archived version of release-cycles.

Usage

Create an HTML form like this.

html

And copy & paste the following code to upload the image

require_once  "path/to/bulletproof.php";

<p>$image = new Bulletproof\Image($_FILES);</p>

<p>if($image["pictures"]){
  $upload = $image-&gt;upload(); </p>

<p>if($upload){
    echo $upload-&gt;getFullPath(); // uploads/cat.gif
  }else{
    echo $image-&gt;getError(); 
  }
}

For more flexibility, check the options and examples below.

Configs

Setting Properties

Before uploading, you can use these methods to restrict the image size, dimensions, mime types, location... ```php
// Pass a custom name, or it will be auto-generated $image->setName($name);

// define the min/max image upload size (size in bytes) $image->setSize($min, $max);

// define allowed mime types to upload $image->setMime(array('jpeg', 'gif'));

// set the max width/height limit of images to upload (limit in pixels) $image->setDimension($width, $height);

// pass name (and optional chmod) to create folder for storage $image->setLocation($folderName, $optionalPermission); ```

Getting Properties

Methods for getting image info before/after upload. ```php // get the provided or auto-generated image name $image->getName();

// get the image size (in bytes) $image->getSize();

// get the image mime (extension) $image->getMime();

// get the image width in pixels $image->getWidth();

// get the image height in pixels $image->getHeight();

// get image location (folder where images are uploaded) $image->getLocation();

// get the full image path. ex 'images/logo.jpg' $image->getFullPath();

// get the json format value of all the above information $image->getJson(); ```

Customized example

This will set image constrains and return output after upload ```php $image = new Bulletproof\Image($_FILES);

$image->setName("samayo") ->setMime(["gif"]) ->setLocation(DIR . "/avatars");

if($image["pictures"]){ if($image->upload()){ echo $image->getName(); // samayo echo $image->getMime(); // gif echo $image->getLocation(); // avatars echo $image->getFullPath(); // avatars/samayo.gif } } ```

Image Manipulation

To crop, resize or watermak images, use functions stored in [

src/utils
][utils]

Creating custom errors

Use php exceptions to define custom error responses ```php if($image['pictures']){ try { if($image->getMime() !== 'png'){ throw new \Exception('Only PNG image types are allowed'); }

// check size, width, height...

if(!$image->upload()){ throw new \Exception($image->getError()); } else { echo $image->getFullPath(); }

} catch (\Exception $e){ echo "Error " . $e->getMessage(); } } ```

What makes this secure?

  • Uses [
    exif_imagetype()
    ][exifimagetypelink]
    to get the true image mime (
    .extension
    )
  • Uses [
    getimagesize()
    ][getimagesize_link]
    to check if image has a valid height / width in pixels.
  • Sanitized images names, strict folder permissions and more...

License: MIT

[utils]: https://github.com/samayo/bulletproof/tree/master/src/utils [bulletproofarchive]: http://github.com/samayo/bulletproof/releases [exifimagetypelink]: http://php.net/manual/de/function.exif-imagetype.php [getimagesizelink]: http://php.net/manual/en/function.getimagesize.php

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.