samayo/bulletproof
Composer 安装命令:
composer require samayo/bulletproof
包简介
A simple and secure PHP image uploader
README 文档
README
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
samayo/bulletproof 适用场景与选型建议
samayo/bulletproof 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 55.43k 次下载、GitHub Stars 达 397, 最近一次更新时间为 2015 年 06 月 01 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「image」 「security」 「upload」 「Simple」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 samayo/bulletproof 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 samayo/bulletproof 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 samayo/bulletproof 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
The file manager intended for using Laravel with CKEditor / TinyMCE / Colorbox
Yii2 LightBox image galary widget uses Lightbox v2.10.0 by Lokesh Dhakar
The Yii2 extension uses jQuery jquery.carousel-1.1.min.js and makes image carousel from php array of structure defined.
Provide a way to secure accesses to all routes of an symfony application.
It's a barebone security class written on PHP
The Yii2 extension uses jQuery PrettyPhoto and OwlCarousel js and makes image galary from php array of structure defined.
统计信息
- 总下载量: 55.43k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 400
- 点击次数: 26
- 依赖项目数: 2
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-06-01