Skip to content
프로젝트

소개

Simple and secure image uploader in PHP

보안

이 수치를 측정하는 방법

측정된 스타 증가

in 24h
0
in 7 days
0
in 15 days
기록 수집 중
in 1 month
기록 수집 중

서로 겹치는 기간의 순증가량이며 일별 합계가 아닙니다. 측정값이 없으면 비워 둡니다. 스타는 관심도를 나타내며 품질을 보장하지 않습니다.

README.md

Original

BULLETPROOF Test

Latest Stable Version Total Downloads Scrutinizer Code Quality License

Bulletproof is a single-class PHP library to upload images securely.

Installation

Install using git

$ git clone https://github.com/samayo/bulletproof.git

Install using Composer

$ composer require samayo/bulletproof:5.0.*

Or download it manually in a ZIP format

Usage

To quickly upload images, use the following HTML & PHP code:

<form method="POST" enctype="multipart/form-data">
  <input type="hidden" name="MAX_FILE_SIZE" value="1000000"/>
  <input type="file" name="pictures" accept="image/*"/>
  <input type="submit" value="upload"/>
</form>
require_once  "path/to/bulletproof.php";

$image = new Bulletproof\Image($_FILES);

if($image["pictures"]){
  $upload = $image->upload(); 

  if($upload){
    echo $upload->getPath(); // uploads/cat.gif
  }else{
    echo $image->getError(); 
  }
}

For more options or configurations, check the following examples:

Configs

Setting Properties

Methods to set restriction on the image name, size, type, etc.. to upload

// To provide a name for the image. If unused, image name will be auto-generated.
$image->setName($name);

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

// To define a list of allowed image types to upload
$image->setMime(array('jpeg', 'gif'));

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

// To create a folder name to store the uploaded image, with optional chmod permission
$image->setStorage($folderName, $optionalPermission);

Getting Properties

Methods to retrieve image data before/after upload.

// To get the image name
$image->getName();

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

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

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

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

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

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

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

Extended Configuration Usage

How to use the property setters and getters.

$image = new Bulletproof\Image($_FILES);

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

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

Image Manipulation

To crop, resize or watermak images, use functions stored in src/utils

Creating custom errors

Use php exceptions to define custom error responses

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->getPath();
    }
    
  } catch (\Exception $e){
    echo "Error " . $e->getMessage();
  }
}

What makes this secure?

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

License: MIT

관련 프로젝트 살펴보기

불러오는 중…