Code Monkey home page Code Monkey logo

bulletproof's Introduction

BULLETPROOF Build Status

Total Downloads Latest Stable Version Latest Unstable Version License

A one file/class to upload/crop/resize/watermark images in PHP securely!!.

enable the php_exif extension in your php.ini before using this class.

Install

{
    "require": {
        "bullet-proof/image-uploader": ">=1.0.0"
    }
}

Setup

// just require the class and create the object
require_once  "src\bulletproof.php";
$image = new ImageUploader\BulletProof;

1 - upload with default settings

The below code will:

  • Only upload images with (jpg, gif, png, jpeg) extension,
  • Only sizes ranging from 0.1kb to 30kb max
  • Creates [if it doesn't exist] "uploads/" folder for storing the image
if($_FILES){
    $result = $image->upload($_FILES['picture']);
}

Also, the variable $result will contain the upload directory/image name. Which can be useful, for storage in database or render the uploaded image using <img src=' <?= $result ?> '/>

2 - customized size, dimension and location.

$image->fileTypes(["png", "jpeg"])  //only accept png/jpeg image types
    ->uploadDir("pics")  //create folder 'pics' if it does not exist.
    ->limitSize(["min"=>1000, "max"=>55000])  //limit image size (in bytes)
    ->limitDimension(["height"=>100, "width"=>120]);  //limit image dimensions
    ->upload($_FILES['picture']);  // upload to folder 'pics'

3 - shrink and upload

shrink() method shrinks/resizes the image according to the given dimensions (in pixels)

$image->fileTypes(["jpg", "gif", "png", "jpeg"])
    ->uploadDir("small_pics")
    ->shrink(["height"=>100, "width"=>200]) // shrink to 100*200 pixels
    ->upload($_FILES["pictures"]); // upload to small_pics folder

4 - watermark and upload.

watermark() create a watermark at the top of the main image, accepts 2 args: 1# - The image to use as watermark. (best to use a PNG). 2# - The Location where to put the watermark on the image. Location can be a string 'center', 'bottom-right', 'bottom-left', 'top-left'...

$image->fileTypes(array("png"))
    ->uploadDir("watermarked")
    ->watermark('me.png', 'bottom-right')) // put 'me.png' on center 
    ->upload($_FILES['logo']);

5 - crop and upload.

crop() method simply crops the images, by the given cordination

$image->fileTypes(array("png"))
   ->crop(array("height"=>40, "width"=>50)) // crop/trim to 40*50 pixels
   ->upload($_FILES['logo']);

Please check the examples/ folder for list of boilerplate codes

Renaming images

upload() accepts 2 arguments; image path & image name [optional] If you want to set a costume name, then pass a second argument, otherwise it will have auto-generated name.

// Image will be renamed 'cheeez' plus the extension 
->upload($_FILES['fileName'], 'cheeez');

// Image will be given random name like: 1531e4b0e3bc82_QPIJLMPKQNJGF plus ext. 
->upload($_FILES['fileName']);

Manipulating uploaded images:

The change() method is different from upload() and should not be mixed. as the first will simply allow you to change images in your server, that do no need uploading.

// The change method is like accessing any file physically and making change to it. 
//CROP IMAGES
$change = $image
 	->crop(array("height"=>10, "width"=>10))
 	->change("crop", "my_pictures/awesome.gif");

// WATERMARK IMAGES
$change = $image
 	->watermark("logo.png", "center")
 	->change("watermark", "my_pictures/passport.gif");

// SHRINK IMAGES
$change = $image
 	->shrink(array("height"=>30, "width"=>50))
 	->change("shrink", "my_pictures/paris.jpg");

What makes this secure ?

  • It checks any errors thrown from the $_FILES[]['error'] array.
  • Uses exif_imagetype() method to get the real mime/image type,
  • Checks if image type exists in the expected types ie. array('jpg', 'png', 'gif', 'jpeg')
  • Checks getimagesize(); to see if the image has a valid width/height measurable in pixels.
  • Uses is_uploaded_file() to check for a secure upload HTTP Post method, (extra security check).

Todo

  • Allow Image Resizing Done.
  • Allow Image Watermarking Done.
  • Allow Image Cropping Done.
  • Handle Errors with Exceptions Done.
  • Backward compatability for PHP 5.3 Done.
  • Single Responsibility Principle don't count on it yet.

License

Luke 3:11 ( Free; No license! )

bulletproof's People

Contributors

dv336699 avatar peehaa avatar petervanderdoes avatar samayo avatar xerkus avatar

Watchers

 avatar  avatar

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.