From 6f9b9a96efa8fe19f77a0df59bd6bacf74184455 Mon Sep 17 00:00:00 2001 From: Anthony Borghi <anthony.borghi@veremes.com> Date: Tue, 30 Oct 2018 12:14:44 +0100 Subject: [PATCH] Auto image resampling on upload Move re sampling function from vitis mother class to phpUtil. Call of this function when an image file is uploaded on the server with height/width values. --- vas/rest/class/vmlib/phpUtil.inc | 192 +++++++++++++++++++++++++----- vas/rest/ws/vitis/Vitis.class.inc | 85 ------------- 2 files changed, 163 insertions(+), 114 deletions(-) diff --git a/vas/rest/class/vmlib/phpUtil.inc b/vas/rest/class/vmlib/phpUtil.inc index ba7d89ab..f5e5d11b 100755 --- a/vas/rest/class/vmlib/phpUtil.inc +++ b/vas/rest/class/vmlib/phpUtil.inc @@ -119,6 +119,16 @@ function stripslashes_deep($aString) { $sErrorMsg .= FILE_LABEL_PHPUTIL . $aFileValues['name'] . ERROR_EXCEED_MAX_SIZE_PHP . ' (' . $sMaxSize . LABEL_BYTES_PHPUTIL . ').'; } + // si c'est une image avec les infos de taille final on resample + if($sFileType === "image" && (isset($aFileValues["width"]) && isset($aFileValues["height"]))){ + $sTmpFile = pictureResampler($sTmpFile, $aFileValues["width"], $aFileValues["height"], 0, 0, 0, 0, $sFileExtension); + if(!$sTmpFile){ + $bAllowUpload = false; + writeToErrorLog(ERROR_COPYING_FILE . $aFileValues['name'] . ON_SERVER_PHPUTIL . ', while resampling picture, ' . $sServerPath); + $sErrorMsg = ERROR_COPYING_FILE . $aFileValues['name'] . ON_SERVER_PHPUTIL . '.'; + } + } + //Lance l'upload. if ($bAllowUpload) { if ($properties['fileS3Uploader']){ @@ -175,35 +185,43 @@ function stripslashes_deep($aString) { *@param $aValues $aValues to copy file in tmp. *@return $aFileStruct FileStuct or null if an error block the write in tmp. */ - function extractFileStruct ($sField, $aValues = null){ - global $properties; - if (empty($aValues)){ - // Extract From Post $File Struct - return $aFileStruc = array( - "name" => $_FILES[$sField]['name'], - "tmp_name" => $_FILES[$sField]['tmp_name'], - "error" => $_FILES[$sField]['error'], - "size" => $_FILES[$sField]['size'] - ); - } else { - // Extraction de $aValues, on le met dans tmp pour préparer la copie dans upload file - $sTmpFile = $properties['extract_dir'] . "/" . getUniqRandomId(); - $oFile = fopen($sTmpFile, 'w+'); - if (!$oFile){ - writeToErrorLog("Can't open file in " . $properties['extract_dir']); - return null; - }else{ - fwrite($oFile, $aValues[$sField . "_file"]); - fclose($oFile); - return $aFileStruc = array( - "name" => $aValues[$sField . "_name"], - "tmp_name" => $sTmpFile, - "error" => "0", - "size" => filesize($sTmpFile) - ); - } - } - } + function extractFileStruct ($sField, $aValues = null){ + global $properties; + $aFileStruc = array(); + if (!isset($aValues[$sField . "_name"])){ + // Extract From Post $File Struct + $aFileStruc = array( + "name" => $_FILES[$sField]['name'], + "tmp_name" => $_FILES[$sField]['tmp_name'], + "error" => $_FILES[$sField]['error'], + "size" => $_FILES[$sField]['size'] + ); + } else { + // Extraction de $aValues, on le met dans tmp pour préparer la copie dans upload file + $sTmpFile = $properties['extract_dir'] . "/" . getUniqRandomId(); + $oFile = fopen($sTmpFile, 'w+'); + if (!$oFile){ + writeToErrorLog("Can't open file in " . $properties['extract_dir']); + return null; + }else{ + fwrite($oFile, $aValues[$sField . "_file"]); + fclose($oFile); + $aFileStruc = array( + "name" => $aValues[$sField . "_name"], + "tmp_name" => $sTmpFile, + "error" => "0", + "size" => filesize($sTmpFile) + ); + } + } + + if(isset($aValues[$sField . "_width"]) && isset($aValues[$sField . "_height"])){ + $aFileStruc["width"] = $aValues[$sField . "_width"]; + $aFileStruc["height"] = $aValues[$sField . "_height"]; + } + + return $aFileStruc; + } /** *This method upload a file in ws_data. *@file vmlib/phpUtil.inc @@ -1200,6 +1218,122 @@ function copyDirectory($src,$dst) { closedir($dir); } +/** + *This method will resample a picture. Preserve picture ratio. + *@file vmlib/phpUtil.inc + *@param $sFilePath Path to the picture on the server. + *@param $iDstWidth Final Width. + *@param $iDstHeight Final Height. + *@param $iCropX Number of pixel to crop (horizontal). + *@param $iCropY Number of pixel to crop (vertical). + *@param $iOffsetX Number of pixel to begin the resampling (horizontal). + *@param $iOffsetY Number of pixel to begin the resampling (vertical). + *@param $sType Picture file extension if known. + *@return $sPath Path of the resampled picture or false if error. + */ +function pictureResampler($sFilePath, $iDstWidth, $iDstHeight, $iCropX = 0, $iCropY = 0, $iOffsetX = 0, $iOffsetY = 0, $sType = null) { + // récupération de l'extension pour ouverture du fichier + if(empty($sType)){ + $sType = extension($sFilePath); + } + // Récupération du format de la source + list($iCurrentWidth, $iCurrentHeight) = getimagesize($sFilePath); + list($width, $height) = sizeCalculator($iCurrentWidth, $iCurrentHeight, $iDstWidth, $iDstHeight); + // Ouverture de la ressource return false si format non réconnu + switch ($sType) { + case 'bmp': + $img = imagecreatefromwbmp($sFilePath); + break; + case 'gif': + $img = imagecreatefromgif($sFilePath); + break; + case 'jpg': + case 'jpeg': + $img = imagecreatefromjpeg($sFilePath); + break; + case 'png': + $img = imagecreatefrompng($sFilePath); + break; + default : + return false; + } + //Suppression du fichier sinon problème de réouverture après si jpg et doublon si autre format + unlink($sFilePath); + + if ($img === false) { + writeToErrorLog("ERROR : Vitis cannot open this file"); + return false; + } + // création de l'image de sortie + $image_p = imagecreatetruecolor($width, $height); + $white = imagecolorallocate($image_p, 255, 255, 255); + imagefill($image_p, 0, 0, $white); + // resizer + if (!imagecopyresampled($image_p, $img, $iOffsetX, $iOffsetY, $iCropX, $iCropY, $width, $height, $iCurrentWidth, $iCurrentHeight)) { + writeToErrorLog("ERROR : Vitis cannot resampling the picture"); + return false; + } + // transformation au format jpeg pour supprimer l'alpha et gagner en taille du fichier + $dst = $sFilePath; + if (strpos($sFilePath, ".") > -1){ + $aPath = explode(".", $sFilePath); + $aPath[count($aPath) - 1] = "jpg"; + $dst = implode(".", $aPath); + } + + // Enregistrement du jpeg + if (!imagejpeg($image_p, $dst)) { + writeToErrorLog("ERROR : Vitis failed to write this picture"); + return false; + } + // Libération espace mémoire + imagedestroy($image_p); + imagedestroy($img); + // retour du nouveau nom de fichier + return $dst; +} + +/** + *This method will process the final height/width for resampling a picture by preserve initial ratio. + *@file vmlib/phpUtil.inc + *@param $iCurrentWidth Width of the picture. + *@param $iCurrentHeight Height of the picture. + *@param $iTargetWidth Maximum final width of the picture. + *@param $iTargetHeight Maximum final height of the picture. + *@return $aSize couple width, height for resample the picture. + */ +function sizeCalculator($iCurrentWidth, $iCurrentHeight, $iTargetWidth, $iTargetHeight) { + $iWidth = $iTargetWidth; + $iHeight = $iTargetHeight; + // si image plus grande on retourne les cibles + if ($iCurrentWidth > $iTargetWidth && $iCurrentHeight > $iTargetHeight) { + return array($iTargetWidth, $iTargetHeight); + } + // On détermine si on conserve la largeur ou la hauteur + if ($iCurrentWidth > $iTargetWidth && $iCurrentHeight < $iTargetHeight) { + $iHeight = $iCurrentHeight * $iTargetWidth / $iCurrentWidth; + $iWidth = $iTargetWidth; + } else if ($iCurrentWidth < $iTargetWidth && $iCurrentHeight > $iTargetHeight) { + $iHeight = $iTargetHeight; + $iWidth = $iCurrentWidth * $iTargetHeight / $iCurrentHeight; + } else { + // dans ce cas les les deux distance sont plus petite que le but on + // calcul donc le delta pour déterminer quel coté est gardé + $fDeltaWidth = $iCurrentWidth / $iTargetWidth; + $fDeltaHeight = $iCurrentHeight / $iTargetHeight; + if ($iDeltaWidth < $iDeltaHeight) { + $iHeight = $iCurrentHeight; + $iWidth = $iCurrentHeight * $iTargetWidth / $iTargetHeight; + } else { + $iWidth = $iCurrentWidth; + $iHeight = $iCurrentWidth * $iTargetHeight / $iTargetWidth; + } + } + + return array($iWidth, $iHeight); +} + + // end of class /** diff --git a/vas/rest/ws/vitis/Vitis.class.inc b/vas/rest/ws/vitis/Vitis.class.inc index e6022e65..40d43aa7 100644 --- a/vas/rest/ws/vitis/Vitis.class.inc +++ b/vas/rest/ws/vitis/Vitis.class.inc @@ -1463,91 +1463,6 @@ class Vitis extends DbClass { return $sRelation; } - function pictureResampler($sFilePath, $iDstWidth, $iDstHeight, $iCropX = 0, $iCropY = 0, $iOffsetX = 0, $iOffsetY = 0) { - // récupération de l'extension pour ouverture du fichier - $sType = strtolower(substr(strrchr($sFilePath, "."), 1)); - if ($sType == 'jpeg' || $sType == 'JPG') { - $sType = 'jpg'; - } - // Récupération du format de la source - list($iCurrentWidth, $iCurrentHeight) = getimagesize($sFilePath); - list($width, $height) = $this->sizeCalculator($iCurrentWidth, $iCurrentHeight, $iDstWidth, $iDstHeight); - // Ouverture de la ressource return false si format non réconnu - switch ($sType) { - case 'bmp': $img = imagecreatefromwbmp($sFilePath); - break; - //case 'gif': $img = imagecreatefromgif($sFilePath); - // break; - case 'jpg': $img = imagecreatefromjpeg($sFilePath); - break; - case 'png': $img = imagecreatefrompng($sFilePath); - break; - default : - return false; - } - //Suppression du fichier sinon problème de réouverture après si jpg et doublon si autre format - unlink($sFilePath); - - if ($img === false) { - writeToErrorLog("ERROR : Vitis cannot open this file"); - return false; - } - // création de l'image de sortie - $image_p = imagecreatetruecolor($width, $height); - $white = imagecolorallocate($image_p, 255, 255, 255); - imagefill($image_p, 0, 0, $white); - // resizer - if (!imagecopyresampled($image_p, $img, $iOffsetX, $iOffsetY, $iCropX, $iCropY, $width, $height, $iCurrentWidth, $iCurrentHeight)) { - writeToErrorLog("ERROR : Vitis cannot resampling the picture"); - return false; - } - // transformation au format jpeg pour supprimer l'alpha et gagner en taille du fichier - $aPath = explode(".", $sFilePath); - $aPath[count($aPath) - 1] = "jpg"; - $dst = implode(".", $aPath); - // Enregistrement du jpeg - if (!imagejpeg($image_p, $dst)) { - writeToErrorLog("ERROR : Vitis failed to write this picture"); - return false; - } - // Libération espace mémoire - imagedestroy($image_p); - imagedestroy($img); - // retour du nouveau nom de fichier - return $dst; - } - - function sizeCalculator($iCurrentWidth, $iCurrentHeight, $iTargetWidth, $iTargetHeight) { - $iWidth = $iTargetWidth; - $iHeight = $iTargetHeight; - // si image plus grande on retourne les cibles - if ($iCurrentWidth > $iTargetWidth && $iCurrentHeight > $iTargetHeight) { - return array($iTargetWidth, $iTargetHeight); - } - // On détermine si on conserve la largeur ou la hauteur - if ($iCurrentWidth > $iTargetWidth && $iCurrentHeight < $iTargetHeight) { - $iHeight = $iCurrentHeight * $iTargetWidth / $iCurrentWidth; - $iWidth = $iTargetWidth; - } else if ($iCurrentWidth < $iTargetWidth && $iCurrentHeight > $iTargetHeight) { - $iHeight = $iTargetHeight; - $iWidth = $iCurrentWidth * $iTargetHeight / $iCurrentHeight; - } else { - // dans ce cas les les deux distance sont plus petite que le but on - // calcul donc le delta pour déterminer quel coté est gardé - $fDeltaWidth = $iCurrentWidth / $iTargetWidth; - $fDeltaHeight = $iCurrentHeight / $iTargetHeight; - if ($iDeltaWidth < $iDeltaHeight) { - $iHeight = $iCurrentHeight; - $iWidth = $iCurrentHeight * $iTargetWidth / $iTargetHeight; - } else { - $iWidth = $iCurrentWidth; - $iHeight = $iCurrentWidth * $iTargetHeight / $iTargetWidth; - } - } - - return array($iWidth, $iHeight); - } - /** * Structure date in selectedField array * @param string $sField -- GitLab