1 2/** 3 * This file is part of the Phalcon Framework. 4 * 5 * (c) Phalcon Team <team@phalcon.io> 6 * 7 * For the full copyright and license information, please view the LICENSE.txt 8 * file that was distributed with this source code. 9 */ 10 11namespace Phalcon\Validation\Validator\File\Resolution; 12 13use Phalcon\Messages\Message; 14use Phalcon\Validation; 15use Phalcon\Validation\Validator\File\AbstractFile; 16 17/** 18 * Checks if a file has the right resolution 19 * 20 * ```php 21 * use Phalcon\Validation; 22 * use Phalcon\Validation\Validator\File\Resolution\Max; 23 * 24 * $validator = new Validation(); 25 * 26 * $validator->add( 27 * "file", 28 * new Max( 29 * [ 30 * "resolution" => "800x600", 31 * "message" => "Max resolution of :field is :resolution", 32 * "included" => true, 33 * ] 34 * ) 35 * ); 36 * 37 * $validator->add( 38 * [ 39 * "file", 40 * "anotherFile", 41 * ], 42 * new Max( 43 * [ 44 * "resolution" => [ 45 * "file" => "800x600", 46 * "anotherFile" => "1024x768", 47 * ], 48 * "included" => [ 49 * "file" => false, 50 * "anotherFile" => true, 51 * ], 52 * "message" => [ 53 * "file" => "Max resolution of file is 800x600", 54 * "anotherFile" => "Max resolution of file is 1024x768", 55 * ], 56 * ] 57 * ) 58 * ); 59 * ``` 60 */ 61class Max extends AbstractFile 62{ 63 protected template = "File :field exceeds the maximum resolution of :resolution"; 64 65 /** 66 * Constructor 67 * 68 * @param array options = [ 69 * 'message' => '', 70 * 'template' => '', 71 * 'resolution' => '1000x1000', 72 * 'included' => false 73 * ] 74 */ 75 public function __construct(array! options = []) 76 { 77 parent::__construct(options); 78 } 79 80 /** 81 * Executes the validation 82 */ 83 public function validate(<Validation> validation, var field) -> bool 84 { 85 var height, maxHeight, maxWidth, resolution, resolutionArray, 86 tmp, value, width, replacePairs, included = false, result; 87 88 // Check file upload 89 if this->checkUpload(validation, field) === false { 90 return false; 91 } 92 93 let value = validation->getValue(field), 94 tmp = getimagesize(value["tmp_name"]), 95 width = tmp[0], 96 height = tmp[1]; 97 98 let resolution = this->getOption("resolution"); 99 100 if typeof resolution == "array" { 101 let resolution = resolution[field]; 102 } 103 104 let resolutionArray = explode("x", resolution), 105 maxWidth = resolutionArray[0], 106 maxHeight = resolutionArray[1]; 107 108 let included = this->getOption("included"); 109 110 if typeof included == "array" { 111 let included = (bool) included[field]; 112 } else { 113 let included = (bool) included; 114 } 115 116 if included { 117 let result = width >= maxWidth || height >= maxHeight; 118 } else { 119 let result = width > maxWidth || height > maxHeight; 120 } 121 122 if typeof resolution == "array" { 123 let resolution = resolution[field]; 124 } 125 126 if result { 127 let replacePairs = [ 128 ":resolution" : resolution 129 ]; 130 131 validation->appendMessage( 132 this->messageFactory(validation, field, replacePairs) 133 ); 134 135 return false; 136 } 137 138 return true; 139 } 140} 141