Giter VIP home page Giter VIP logo

bulletproof's Introduction

BULLETPROOF

SECURE PHP IMAGE UPLOADER

This class allows you to do two things! First is to upload images while cropping, resizing and watermarking the image. The second is, to do the same as above but without uploading. It means, you can crop/resize/watermark any image any time. Please check examples.php for a complete guide.

Enable php_exif extension in your php.ini before using this class.

=====

THE START: Include & Instantiate the class.
/** As usual: Require and call the class only */

require_once "BulletProof.php";
$bulletProof = new ImageUploader\BulletProof;
SCENARIO 1: Uploading images with the default setting. (Less code)
/*
 *   This will use the default settings of the class and will upload only
 *   (jpg, gif, png, jpeg) images with sizes ranging from 0.1kb to max 30kbs
 *   It will also create a folder called "uploads" with chmod 0777 if it does not exist.
 */ 
if($_FILES){
    echo $bulletProof->upload($_FILES['picture']);
 }
SCENARIO 2: Upload images with specific size/type/dimension (Moaarr code)
/*
 *   fileTypes() - What type of images to upload. ex: jpg, gif, png..
 *   limitSize() - Set the min and max image size limit (in bytes)
 *   limitDimension() - Set the max height and width of image upload  (in pixels)
 *   folder() - set a folder to store the uploads. It will be created automatically.
 *   upload() - the final method that checks everything and uploads the file.
 *     The variable $bulletProof will contain the path/image from the upload,
 *     So, you can simply store it in db or echo it like  <img src='$bulletProof' />;
 */
echo $bulletProof
        ->fileTypes(array("png", "jpeg"))
        ->folder("my_pictures")
        ->limitSize(array("min"=>1000, "max"=>100000))
        ->limitDimension(array("height"=>100, "width"=>100));
        ->upload($_FILES['picture']);
SCENARIO 3: Shrink and upload image.
/*
 *   shrink() - will shrink/resize the image to the given dimensions
 */
$bulletProof
    ->fileTypes(array("jpg", "gif", "png", "jpeg"))
    ->folder("shrinked_images")
    ->shrink(array("height"=>100, "width"=>200))
    ->upload($_FILES["pictures"]);
SCENARIO 4: Watermark and upload image.
/*
 *   watermark() - will accept two arguments.
 *     First: The image to use as watermark. (best to use PNG).
 *     Second: The Location where to put your watermark on the image.
 *     Location: 'center', 'bottom-right', 'bottom-left', 'top-left'...
 */
$bulletProof
    ->fileTypes(array("jpeg"))
    ->folder("watermarked")
    ->watermark('watermark.png', 'bottom-right'))
    ->upload($_FILES['logo']);
SCENARIO 5: Crop and upload image
/*
 *   crop() - Width and height (in pixels) for image crop.
 *   crop is not like shrink, it simply will trim/cut the image
 *   and return what is left, whereas shrink will not trim the image.
 */
$bulletProof
    ->fileTypes(array("jpeg"))
    ->folder("watermarked")
    ->crop(array("height"=>40, "width"=>50))
    ->upload($_FILES['logo']);

Please check the examples.php for more functions and all tested examples.

NOTE:

The upload() method accepts two arguments. First the Image, and second [optional], a new name for the image. If you provide a name, that name will be used for renaming, if not a unique name will be generated.

// Uploaded file will be renamed 'cheeez' plus the file mime type.
->upload($_FILES['fileName'], 'cheeez');

// file will be named ex '1531e4b0e3bc82_QPIJLMPKQNJGF' plus the mime type
->upload($_FILES['fileName']);

The change() method is different from upload() and should not be mixed. The change() will allow you to directly crop/resize/watermark an image that is already uploaded.

// The change method is like accessing any file physically and making change to it. 

//CROP IMAGES
$change = $bulletProof
 	->crop(array("height"=>10, "width"=>10))
 	->change("crop", "my_pictures/awesome.gif");

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

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

What makes this secure?

  • It checks & handles any errors thrown by $_FILES[]['error'].
  • It uses exif_imagetype() method to get the real mime/image type,
  • Checks if MIME type exists in the expected image 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).

Whats next?

  • Allow image resizing Done!
  • Allow image watermarking Done!
  • Allow image cropping Done!
  • Handle errors with exceptions Done!
  • Allow text watermarking <-- discontinued!

###License
Luke 3:11

bulletproof's People

Contributors

dv336699 avatar peehaa avatar onehoopyfrood avatar

Watchers

James Cloos avatar

Forkers

sesn

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.