diff --git a/CHANGELOG.md b/CHANGELOG.md index 0aa85a2..84b1eef 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ # CHANGELOG +### 5.1.0 + - Removed functions used to crop, resize and watermark images. Image manipulation is no longer supported + ### 5.0.0 - Compatibility with PHP8+ @@ -35,7 +38,7 @@ - git went haywire, 2.0.3 had to be born ### 2.0.2 - - enabled checking for 'exif_imagetype' function. + - enabled checking for `exif_imagetype` function. - added configurable array messages thanks to github.com/lordgiotto ### 2.0.1 diff --git a/README.md b/README.md index 6aad8e9..e5faad2 100644 --- a/README.md +++ b/README.md @@ -2,25 +2,25 @@ [![Latest Stable Version](https://poser.pugx.org/samayo/bulletproof/v/stable.svg?format=flat-square)](https://packagist.org/packages/samayo/bulletproof) [![Total Downloads](https://poser.pugx.org/samayo/bulletproof/downloads?format=flat-square)](https://packagist.org/packages/samayo/bulletproof?format=flat-square) [![Scrutinizer Code Quality](https://scrutinizer-ci.com/g/samayo/bulletproof/badges/quality-score.png?b=master)](https://scrutinizer-ci.com/g/samayo/bulletproof/?branch=master) [![License](https://poser.pugx.org/samayo/bulletproof/license)](https://packagist.org/packages/fastpress/framework) -Bulletproof is a single-class PHP library to upload images securely. +A single-file PHP library to upload images securely. Installation ----- -Install using git +Using git ```bash $ git clone https://github.com/samayo/bulletproof.git ``` -Install using Composer +Using Composer ```bash -$ composer require samayo/bulletproof:5.0.* +$ composer require samayo/bulletproof:5.1.* ``` Or [download it manually][bulletproof_archive] in a ZIP format Usage ----- -To quickly upload images, use the following HTML & PHP code: +Use the following example to quickly upload an image ```html
@@ -44,14 +44,13 @@ if($image["pictures"]){ } } ``` -For more options or configurations, check the following examples: - -Configs +Configuration ----- +Settings to upload images with more options -#### Setting Properties -Methods to set restriction on the image name, size, type, etc.. to upload +#### Set options +Settings used before uploading image to set options ```php // To provide a name for the image. If unused, image name will be auto-generated. $image->setName($name); @@ -69,8 +68,8 @@ $image->setDimension($width, $height); $image->setStorage($folderName, $optionalPermission); ``` -#### Getting Properties -Methods to retrieve image data before/after upload. +#### Get options +Settings used after uploading image to set options ```php // To get the image name $image->getName(); @@ -97,32 +96,30 @@ $image->getPath(); $image->getJson(); ``` -#### Extended Configuration Usage +#### Upload example with more options How to use the property setters and getters. ```php $image = new Bulletproof\Image($_FILES); -$image->setName("samayo") - ->setMime(["gif"]) - ->setStorage(__DIR__ . "/avatars"); +$image->setName("dog") + ->setMime(["jpg"]) + ->setStorage(__DIR__ . "/uploads"); if($image["pictures"]){ if($image->upload()){ - echo $image->getName(); // samayo - echo $image->getMime(); // gif - echo $image->getStorage(); // avatars - echo $image->getPath(); // avatars/samayo.gif + echo $image->getName(); // dog + echo $image->getMime(); // jpg + echo $image->getStorage(); // uploads + echo $image->getPath(); // uploads/dog.jpg } } ``` -#### 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 +How to use exceptions to catch errors ```php if($image['pictures']){ + try { if($image->getMime() !== 'png'){ throw new \Exception('Only PNG image types are allowed'); @@ -133,12 +130,13 @@ if($image['pictures']){ if(!$image->upload()){ throw new \Exception($image->getError()); } else { - echo $image->getPath(); + $path = $image->getPath(); } } catch (\Exception $e){ - echo "Error " . $e->getMessage(); + echo "Image upload error: " . $e->getMessage(); } + } ``` diff --git a/src/utils/README.md b/src/utils/README.md deleted file mode 100644 index 654b81e..0000000 --- a/src/utils/README.md +++ /dev/null @@ -1,111 +0,0 @@ -### bulletproof\utils - -Contains seperate, stand-alone functions to crop, rezize or watermark images - -Install ------ -open `utils/` folder and require the function you neeed. - -#### use crop function -```php -require_once 'src/utils/func.image-crop.php'; - -/** - * $image : full image path - * $mime : the mime type of the image - * $width : the image width - * $height : the image height - * $newWidth : the new width of the image - * $newHeight : the new height of the image: - */ -$crop = bulletproof\utils\crop($image, $mime, $width, $height, $newWidth, $newHeight); -``` -##### crop function example -```php -require_once 'src/utils/func.image-crop.php'; -// call the function and pass the right arguments. -$crop = Bulletproof\Utils\crop( - 'images/my-car.jpg', 'jpg', 100, 200, 50, 25 -); -// now 'images/my-car.jpg' is cropped to 50x25 pixels. -``` - -### with bulletproof - -If you want to use these function with the [bulletproof][bulletproof], here are some examples: - -#### Resizing -```php -// include bulletproof and the function you need. -require "src/bulletproof.php"; -require "src/utils/func.image-resize.php"; - -// after you upload the image, call the function -$image = new Bulletproof\Image($_FILES); - -if($image["picture"]){ - $upload = $image->upload(); - - if($upload){ - Bulletproof\Utils\resize( - $image->getPath(), - $image->getMime(), - $image->getWidth(), - $image->getHeight(), - 50, - 50 - ); - } -} -``` - -#### Croping -The `crop()` function supports resizing by ratio, checkout the file for more. -```php -require "src/utils/func.image-crop.php"; - -$crop = Bulletproof\Utils\crop( - $upload->getPath(), - $upload->getMime(), - $upload->getWidth(), - $upload->getHeight(), - 50, - 50 -); - -``` -#### Watermark -The `watermark()` function allows adding watermark into an image - -```php -require 'src/utils/func.image-watermark.php'; -// the image to watermark -$logo = 'my-logo.png'; -// where to place the watermark -$position = 'center'; -// get the width and heigh of the logo -list($logoWidth, $logoHeight) = getimagesize($logo); - -$watermark = Bulletproof\watermark( - $upload->getPath(), - $upload->getMime(), - $upload->getWidth(), - $upload->getHeight(), - $logo, - $logoHeight, - $logoWidth, - $position -); -``` - -Contribution ------ - -You are encouraged to add functions for other features (ex: add text, rotate images .. ) - -LICENSE ------ -Check the main [bulletproof][bulletproof] page for the license. - - -[bulletproof]: http://github.com/samayo/bulletproof diff --git a/src/utils/func.image-crop.php b/src/utils/func.image-crop.php deleted file mode 100644 index 163454b..0000000 --- a/src/utils/func.image-crop.php +++ /dev/null @@ -1,69 +0,0 @@ - array( - * 'orig' => true, // (bool) whether to use same quality as original image (requires ImageMagick) - * 'fallback' => 80, // (int) fallback if original image quality can not be detected or is not set. Accepted values are 1-100 - * 'max' => 85, // (int) Maximal quality, if detected quality is more than this value, than this will be used. Accepted values are 1-100 - * 'min' => 60 // (int) Minimal quality, if detected quality is less than this value, than this will be used. Accepted values are 1-100 - * ), - * 'png' => 9 // (int) PNG compression level. Accepted values are 0-9. Default is zlib's default which is currently ( 11/2018 ) equal to 6 - * ); - * - * Any of the values can be left out. See following example: - * array( 'jpg' => array( 'fallback' => 80 ) ) // this will set JPG quality to 80 on JPGs - * - * @param string $destination Accepts either folder path or file. - * Script can handle both relative and - * absolute paths. Folder path needs to - * end with '/' (forward slash). If left empty (or set to false) it will overwrite original image - * @param integer $permission The permission for destination folder (if it will be created by script) - * - * @throws \Exception (description) - * - * @return boolean True on success false on failure - * - */ -function resize($image, $mimeType, $imgWidth, $imgHeight, $newWidth, $newHeight, $ratio = false, $upsize = true, $cropToSize = false, $quality = array(), $destination = false, $permission = 0755) -{ - // Checks whether image cropping is enabled - if ($cropToSize) { - $source_aspect_ratio = $imgWidth / $imgHeight; - $thumbnail_aspect_ratio = $newWidth / $newHeight; - - // Adjust cropping area and position depending on original and cropped image - if ($thumbnail_aspect_ratio < $source_aspect_ratio) { - $src_h = $imgHeight; - $src_w = $imgHeight * $thumbnail_aspect_ratio; - $src_x = (int) (($imgWidth - $src_w)/2); - $src_y = 0; - } else { - $src_w = $imgWidth; - $src_h = (int) ($imgWidth / $thumbnail_aspect_ratio); - $src_x = 0; - $src_y = (int) (($imgHeight - $src_h)/2); - } - - // Checks whether image upsizing is enabled - if (!$upsize) { - $newHeightOrig = $newHeight; - $newWidthOrig = $newWidth; - - // If the given width is larger than the image width, then resize it - if ($newWidth > $imgWidth) { - $newWidth = $imgWidth; - $newHeight = (int) ($newWidth / $imgWidth * $imgHeight); - if ($newHeight > $newHeightOrig) { - $newHeight = $newHeightOrig; - $src_h = $newHeight; - $src_y = (int) (($imgHeight - $src_h)/2); - } - - $src_x=0; - $src_w = $imgWidth; - } - - // If the given height is larger then the image height, then resize it. - if ($newHeightOrig > $imgHeight) { - $newHeight = $imgHeight; - $src_y=0; - $src_h = $imgHeight; - $src_w = (int) ($src_h * ( $newWidth / $newHeight )); - $src_x = (int) (($imgWidth - $src_w)/2); - } - } - } else { - - // First, calculate the height. - $height = (int) ($newWidth / $imgWidth * $imgHeight); // 75 - - // If the height is too large, set it to the maximum height and calculate the width. - if ($height > $newHeight) { - $height = $newHeight; - $newWidth = (int) ($height / $imgHeight * $imgWidth); - } - - // If we don't allow upsizing check if the new width or height are too big. - if (!$upsize) { - // If the given width is larger than the image width, then resize it - if ($newWidth > $imgWidth) { - $newWidth = $imgWidth; - $newHeight = (int) ($newWidth / $imgWidth * $imgHeight); - } - - // If the given height is larger then the image height, then resize it. - if ($newHeight > $imgHeight) { - $newHeight = $imgHeight; - $newWidth = (int) ($height / $imgHeight * $imgWidth); - } - } - - if ($ratio == true) { - $source_aspect_ratio = $imgWidth / $imgHeight; - $thumbnail_aspect_ratio = $newWidth / $newHeight; - if ($imgWidth <= $newWidth && $imgHeight <= $newHeight) { - $newWidth = $imgWidth; - $newHeight = $imgHeight; - } elseif ($thumbnail_aspect_ratio > $source_aspect_ratio) { - $newWidth = (int) ($newHeight * $source_aspect_ratio); - $newHeight = $newHeight; - } else { - $newWidth = $newWidth; - $newHeight = (int) ($newWidth / $source_aspect_ratio); - } - } - - $src_x = 0; - $src_y = 0; - $src_w = $imgWidth; - $src_h = $imgHeight; - } - - $imgString = file_get_contents($image); - - $imageFromString = imagecreatefromstring($imgString); - $tmp = imagecreatetruecolor($newWidth, $newHeight); - imagealphablending($tmp, false); - imagesavealpha($tmp, true); - imagecopyresampled( - $tmp, - $imageFromString, - 0, - 0, - $src_x, - $src_y, - $newWidth, - $newHeight, - $src_w, - $src_h - ); - - if ($destination !== false) { // checks if was destination provided - if(substr( $destination, -1 ) == '/' ) { // check whether prowided destination was folder or file. - $create = !is_dir($destination) ? @mkdir($destination, $permission, true) : true; // if it was folder, it will crtete it if needed - if ($create) { - $path_parts = pathinfo($image); - $destination = $destination . $path_parts['basename']; - } else { - return false; // TODO: throw error/exception - } - } - - } else { - $destination = $image; - } - - $path_parts = pathinfo($destination); - $create = !is_dir($path_parts['dirname'].'/') ? @mkdir($path_parts['dirname'].'/', $permission, true) : true; - if (!$create) { - return false; // TODO: throw error/exception - } - - switch ($mimeType) { - case "jpeg": - case "jpg": - $q = 90; // function's default value - if everything else fails, this is used. - if( false !== $image ) { - if ((!empty($quality['jpg']['fallback'])) AND ( (int) $quality['jpg']['fallback'] ) AND ( $quality['jpg']['fallback'] > 0 ) AND ( $quality['jpg']['fallback'] <=100 ) ) { - $q = $quality['jpg']['fallback']; - } - - if ((!empty($quality['jpg']['orig'])) AND true ===$quality['jpg']['orig'] ){ - if (extension_loaded('imagick')){ - $im = new \Imagick($image); - $q = $im->getImageCompressionQuality(); - } - } - - if((!empty($quality['jpg']['max'])) AND $quality['jpg']['max'] < $q ){ - $q = $quality['jpg']['max']; - } - - if((!empty($quality['jpg']['min'])) AND $quality['jpg']['min'] > $q ){ - $q = $quality['jpg']['min']; - } - } - return imagejpeg($tmp, $destination, $q ); - break; - case "png": - if ((!empty($quality['png'])) AND ( (int) $quality['png'] ) AND ( $quality['png'] >= -1 ) AND ( $quality['png'] <=9 ) ) { - $q = $quality['png']; - } else { - $q = -1; // -1 is zlib's default value which is currently ( 11/2018 ) equal to 6 - } - return imagepng($tmp, $destination, $q ); - break; - case "gif": - return imagegif($tmp, $destination); - break; - default: - throw new \Exception(" Only jpg, jpeg, png and gif files can be resized "); - break; - } -} diff --git a/src/utils/func.image-watermark.php b/src/utils/func.image-watermark.php deleted file mode 100644 index 30f1735..0000000 --- a/src/utils/func.image-watermark.php +++ /dev/null @@ -1,102 +0,0 @@ -