| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | declare(strict_types=1); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * @copyright Copyright (c) 2018-2019 Branko Kokanovic <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * @copyright Copyright (c) 2018-2020 Matias De lellis <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @author Branko Kokanovic <[email protected]> | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  * @license GNU AGPL version 3 or any later version | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * This program is free software: you can redistribute it and/or modify | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * it under the terms of the GNU Affero General Public License as | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * published by the Free Software Foundation, either version 3 of the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * License, or (at your option) any later version. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * This program is distributed in the hope that it will be useful, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * GNU Affero General Public License for more details. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * You should have received a copy of the GNU Affero General Public License | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  | namespace OCA\FaceRecognition\Helper; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | use OCP\Image; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  | use OCP\ILogger; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  | use OCP\ITempManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  | class TempImage extends Image { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  | 	/** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  | 	private $imagePath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  | 	/** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  | 	private $tempPath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  | 	/** @var string */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  | 	private $preferredMimeType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  | 	/** @var int */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  | 	private $maxImageArea; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  | 	/** @var ITempManager */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  | 	private $tempManager; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  | 	/** @var int */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  | 	private $minImageSide; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  | 	/** @var float */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  | 	private $ratio = -1.0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  | 	/** @var bool */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  | 	private $skipped = false; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 | 5 |  | 	public function __construct(string $imagePath, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  | 	                            string $preferredMimeType, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  | 	                            int    $maxImageArea, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  | 	                            int    $minImageSide) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  | 	{ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 5 |  | 		parent::__construct(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 5 |  | 		$this->imagePath         = $imagePath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 5 |  | 		$this->preferredMimeType = $preferredMimeType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 | 5 |  | 		$this->maxImageArea      = $maxImageArea; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 | 5 |  | 		$this->minImageSide      = $minImageSide; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 | 5 |  | 		$this->tempManager       = \OC::$server->getTempManager(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 | 5 |  | 		$this->prepareImage(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 4 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 4 |  | 	public function __destruct() { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 4 |  | 		$this->tempManager->clean(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 4 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  | 	 * Get the path of temporary image | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  | 	 * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 3 |  | 	public function getTempPath(): string { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 | 3 |  | 		return $this->tempPath; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  | 	 * Obtain the ratio of the temporary image against the original | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  | 	 * @return float | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 2 |  | 	public function getRatio(): float { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 2 |  | 		return $this->ratio; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  | 	/** Return if image was skipped | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  | 	 * @return bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 4 |  | 	public function getSkipped(): bool { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 4 |  | 		return $this->skipped; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  | 	 * Obtain a temporary image according to the imposed restrictions. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  | 	 * | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 107 |  |  | 	 */ | 
            
                                                                        
                            
            
                                    
            
            
                | 108 | 5 |  | 	private function prepareImage() { | 
            
                                                                        
                            
            
                                    
            
            
                | 109 | 5 |  | 		$this->loadFromFile($this->imagePath); | 
            
                                                                        
                            
            
                                    
            
            
                | 110 | 4 |  | 		$this->fixOrientation(); | 
            
                                                                        
                            
            
                                    
            
            
                | 111 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 112 | 4 |  | 		if (!$this->valid()) { | 
            
                                                                        
                            
            
                                    
            
            
                | 113 |  |  | 			throw new \RuntimeException("Image is not valid, probably cannot be loaded"); | 
            
                                                                        
                            
            
                                    
            
            
                | 114 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 115 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 116 | 4 |  | 		if ((imagesx($this->resource()) < $this->minImageSide) || | 
            
                                                                        
                            
            
                                    
            
            
                | 117 | 4 |  | 		    (imagesy($this->resource()) < $this->minImageSide)) { | 
            
                                                                        
                            
            
                                    
            
            
                | 118 | 2 |  | 			$this->skipped = true; | 
            
                                                                        
                            
            
                                    
            
            
                | 119 | 2 |  | 			return; | 
            
                                                                        
                            
            
                                    
            
            
                | 120 |  |  | 		} | 
            
                                                                        
                            
            
                                    
            
            
                | 121 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 122 | 3 |  | 		$this->ratio = $this->resizeImage(); | 
            
                                                                        
                            
            
                                    
            
            
                | 123 | 3 |  | 		$this->tempPath = $this->tempManager->getTemporaryFile(); | 
            
                                                                        
                            
            
                                    
            
            
                | 124 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 125 | 3 |  | 		$this->save($this->tempPath, $this->preferredMimeType); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 | 3 |  | 	} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 |  |  | 	/** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  | 	 * Resizes the image to reach max image area, but preserving ratio. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 |  |  | 	 * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  | 	 * @return float Ratio of resize. 1 if there was no resize | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  | 	 */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 | 3 |  | 	private function resizeImage(): float { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 | 3 |  | 		$widthOrig = imagesx($this->resource()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 | 3 |  | 		$heightOrig = imagesy($this->resource()); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 | 3 |  | 		if (($widthOrig <= 0) || ($heightOrig <= 0)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  | 			$message = "Image is having non-positive width or height, cannot continue"; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  | 			throw new \RuntimeException($message); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 | 3 |  | 		$areaRatio = $this->maxImageArea / ($widthOrig * $heightOrig); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 | 3 |  | 		$scaleFactor = sqrt($areaRatio); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 | 3 |  | 		$newWidth = intval(round($widthOrig * $scaleFactor)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 | 3 |  | 		$newHeight = intval(round($heightOrig * $scaleFactor)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 | 3 |  | 		$success = $this->preciseResize($newWidth, $newHeight); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 | 3 |  | 		if ($success === false) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  | 			throw new \RuntimeException("Error during image resize"); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  | 		} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 | 3 |  | 		return 1 / $scaleFactor; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  | 	} | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 155 |  |  |  | 
            
                                                        
            
                                    
            
            
                | 156 |  |  | } |