61 lines
2.2 KiB
PHP
61 lines
2.2 KiB
PHP
<?php
|
|
|
|
class Zend_Controller_Action_Helper_Base64Image extends Zend_Controller_Action_Helper_Abstract
|
|
{
|
|
/**
|
|
* Passata una stringa di un immagine base 64 la salva con il nome file passato e nel path passato ne restituisce poi il path completo
|
|
*
|
|
* @param $dataString
|
|
* @param $nomeFile
|
|
* @param $path
|
|
* @return string
|
|
* @throws App_Controller_Errors_WebServiceError
|
|
*/
|
|
public function direct($dataString, $nomeFile, $path)
|
|
{
|
|
|
|
if (!$dataString) {
|
|
throw new App_Controller_Errors_WebServiceError('Datastring non valido',
|
|
App_Controller_Errors_WebServiceError::WEBSERVICE_GENERIC_ERROR);
|
|
}
|
|
|
|
if (!$nomeFile) {
|
|
throw new App_Controller_Errors_WebServiceError('Nome File non valido',
|
|
App_Controller_Errors_WebServiceError::WEBSERVICE_GENERIC_ERROR);
|
|
}
|
|
|
|
if (!$path) {
|
|
throw new App_Controller_Errors_WebServiceError('Path non valido',
|
|
App_Controller_Errors_WebServiceError::WEBSERVICE_GENERIC_ERROR);
|
|
}
|
|
|
|
if (preg_match('/^data:image\/(\w+);base64,/', $dataString, $type)) {
|
|
$dataString = substr($dataString, strpos($dataString, ',') + 1);
|
|
$type = strtolower($type[1]); // jpg, png, gif
|
|
|
|
if (!in_array($type, ['jpg', 'jpeg', 'gif', 'png'])) {
|
|
throw new App_Controller_Errors_WebServiceError('Immagine Base64 non valida',
|
|
App_Controller_Errors_WebServiceError::WEBSERVICE_GENERIC_ERROR);
|
|
}
|
|
|
|
$data = base64_decode($dataString);
|
|
|
|
if ($data === false) {
|
|
throw new App_Controller_Errors_WebServiceError('Errore di decodifica Base64',
|
|
App_Controller_Errors_WebServiceError::WEBSERVICE_GENERIC_ERROR);
|
|
}
|
|
} else {
|
|
throw new App_Controller_Errors_WebServiceError('Formato stringa immagine base 64 non valido',
|
|
App_Controller_Errors_WebServiceError::WEBSERVICE_GENERIC_ERROR);
|
|
}
|
|
|
|
$nomeFile = explode('.',$nomeFile)[0];
|
|
|
|
$completePathFile = "$path/$nomeFile.{$type}";
|
|
file_put_contents($completePathFile, $data);
|
|
|
|
return $completePathFile;
|
|
}
|
|
|
|
}
|