2017-02-13 12:06:18 +01:00

461 lines
12 KiB
PHP

<?php
class LogoController extends Zend_Controller_Action
{
protected $theme;
protected $pathLogo = '';
/**
* Logger
* @var \Monolog\Logger
*/
protected $logger;
public function init()
{
if (Zend_Registry::isRegistered('logger')) {
$this->logger = Zend_Registry::get('logger');
}
// --- Theme
$this->theme = Zend_Registry::get('theme');
$c = Zend_Registry::get('config');
$this->pathLogo = $c->profil->path->shared.'/persist/logos';
}
public function indexAction()
{
$this->_helper->layout->disableLayout();
$request = $this->getRequest();
$siren = $request->getParam('siren', '');
$isin = $request->getParam('isin', '');
$logo = $this->_findlogo($siren, $isin);
$this->view->assign('siren', $siren);
$this->view->assign('logo', $logo);
}
public function uploadAction()
{
$this->_helper->layout->disableLayout();
$request = $this->getRequest();
$siren = $request->getParam('siren');
if ($request->isPost()) {
if ( !empty($siren) && isset($_FILES['file']) && $_FILES['file']['error']!=UPLOAD_ERR_NO_FILE )
{
$logoFile = $_FILES['file'];
$tmp_file = $logoFile['tmp_name'];
if ( $logoFile['error']!=UPLOAD_ERR_OK ) {
$output.= '';
} elseif ( !is_uploaded_file($tmp_file) ){
$output.= '';
} else {
// On vérifie maintenant l'extension
$extAuthorized = array('jpeg', 'jpg', 'png', 'gif', 'bmp');
$type_file = str_replace('image/', '',$logoFile['type']);
$ext = '';
if ( in_array($type_file, $extAuthorized) ){
$ext = $type_file;
}
if ( !empty($ext) ){
// on copie le fichier dans le dossier de destination
$name_file = $siren.'.'.$ext;
if ( file_exists(PATH_LOGOS . $name_file) ){
unlink($this->pathLogo.'/'.$name_file);
}
if( !move_uploaded_file($tmp_file, $this->pathLogo.'/'.$name_file) ) {
$output.= '';
} else {
$this->view->assign('image', $name_file);
}
} else {
$output.= '';
}
}
}
$this->view->assign('isPost', true);
}
$this->view->assign('siren', $siren);
}
public function cropAction()
{
$this->_helper->layout->disableLayout();
$request = $this->getRequest();
$siren = $request->getParam('siren');
$image = $request->getParam('image');
if ($request->isPost())
{
$jpeg_quality = 90;
$png_quality = 9;
list($name, $ext) = explode('.', $image);
$src = $name.'.'.$ext;
$dst = str_replace('tmp_', '', $src);
//Création image
switch($ext){
case 'gif':
$img_r = imagecreatefromgif($this->pathLogo.'/'.$src);
break;
case 'png':
$img_r = imagecreatefrompng($this->pathLogo.'/'.$src);
break;
case 'jpgeg':
case 'jpg':
$img_r = imagecreatefromjpeg($this->pathLogo.'/'.$src);
break;
}
//Resample
$dst_r = ImageCreateTrueColor( $_POST['w'], $_POST['h'] );
imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
$_POST['w'],$_POST['h'],$_POST['w'],$_POST['h']);
//Enregistrement comme le format de départ
if ( file_exists($this->pathLogo.'/'.$dst) ) {
unlink($this->pathLogo.'/'.$dst);
}
switch($ext){
case 'gif':
imagegif($dst_r,$this->pathLogo.'/'.$dst);
break;
case 'png':
imagepng($dst_r,$this->pathLogo.'/'.$dst, $png_quality);
break;
case 'jpgeg':
case 'jpg':
imagejpeg($dst_r,$this->pathLogo.'/'.$dst, $jpeg_quality);
break;
}
$this->view->assign('image', $dst);
$this->view->assign('isPost', true);
} else {
$this->view->assign('image', $image);
}
$this->view->assign('siren', $siren);
}
public function saveAction()
{
$this->_helper->layout->disableLayout();
$request = $this->getRequest();
$file = $request->getParam('image');
if ( !empty($file) ) {
list($name, $ext) = explode('.', $file);
$name_dst = str_replace('tmp_','',$name);
//Vérifier les dimensions
$max_width = 350;
$max_height = 150;
$size = GetImageSize($this->pathLogo.'/'.$file); // Read the size
$width = $size[0];
$height = $size[1];
$x_ratio = $max_width / $width;
$y_ratio = $max_height / $height;
if( ($width <= $max_width) && ($height <= $max_height) )
{
$tn_width = $width;
$tn_height = $height;
}
elseif (($x_ratio * $height) < $max_height)
{
$tn_height = ceil($x_ratio * $height);
$tn_width = $max_width;
}
else
{
$tn_width = ceil($y_ratio * $width);
$tn_height = $max_height;
}
//Création image
switch($ext){
case 'gif':
$src = imagecreatefromgif($this->pathLogo.'/'.$src);
break;
case 'png':
$src = imagecreatefrompng($this->pathLogo.'/'.$src);
break;
case 'jpgeg':
case 'jpg':
$src = imagecreatefromjpeg($this->pathLogo.'/'.$src);
break;
}
$dst = imagecreatetruecolor($tn_width, $tn_height);
imagecopyresized($dst, $src, 0, 0, 0, 0, $tn_width, $tn_height, $width, $height);
switch($ext){
case 'gif':
imagegif($dst,$this->pathLogo.'/'.$name_dst.'.'.$ext);
break;
case 'png':
imagepng($dst,$this->pathLogo.'/'.$name_dst.'.'.$ext);
break;
case 'jpgeg':
case 'jpg':
imagejpeg($dst,$this->pathLogo.'/'.$name_dst.'.'.$ext);
break;
}
//Affecté au siren
$dst = $this->pathLogo.'/'.str_replace('tmp_', '', $file);
if (rename($this->pathLogo.'/'.$file, $dst)){
chmod($dst, 0755);
$message = 'Image affecté.';
} else {
$message = 'Erreur.';
}
}
$this->view->assign('message', $message);
}
public function urlAction()
{
$this->_helper->layout->disableLayout();
$request = $this->getRequest();
$siren = $request->getParam('siren');
if ($request->isPost()){
$logoUrl = $request->getParam('url');
$tabTmp = parse_url($logoUrl);
$hostUrl = $tabTmp['host'];
$pathUrl = $tabTmp['path'];
$tmp = explode('.', basename($pathUrl));
$ext = strtolower(end($tmp));
$extAuthorized = array('jpeg', 'jpg', 'png', 'gif', 'bmp');
//Vérification fichier est une image
if ( in_array($ext, $extAuthorized ) )
{
$name_file = $siren.'.'.$ext;
try {
$client = new Zend_Http_Client($logoUrl);
$client->setStream();
$response = $client->request('GET');
if ( $response->isSuccessful()
&& copy($response->getStreamName(), $this->pathLogo.'/'.$name_file) ) {
$this->view->assign('image', $name_file);
}
} catch (Zend_Http_Client_Exception $e) {}
}
$this->view->assign('isPost', true);
}
$this->view->assign('siren', $siren);
}
public function deleteAction()
{
$this->_helper->layout->disableLayout();
$request = $this->getRequest();
$file = $request->getParam('image');
if ( !empty($file) ){
$message = "Erreur suppression fichier.";
if ( unlink($this->pathLogo.'/'.$file)){
$message = 'Fichier supprimé.';
}
}
$this->view->assign('message', $message);
}
//====> Function interne
function _logo( $siren )
{
$message = '';
if ( isset($_FILES['logoFile']) &&
$_FILES['logoFile']['error']!=UPLOAD_ERR_NO_FILE ) {
/** Un fichier a été uploadé **/
$logoFile = $_FILES['logoFile'];
$tmp_file = $logoFile['tmp_name'];
if ( $logoFile['error']!=UPLOAD_ERR_OK ) {
$message = 'Erreur lors de la copie du fichier';
}
if ( !is_uploaded_file($tmp_file) ){
$message = "Le fichier est introuvable";
} else {
// on vérifie maintenant l'extension
$type_file = $logoFile['type'];
$ext = '';
if ( strstr($type_file, 'jpg')) $ext='jpg';
elseif( strstr($type_file, 'jpeg')) $ext='jpeg';
elseif( strstr($type_file, 'bmp')) $ext='bmp';
elseif( strstr($type_file, 'gif')) $ext='gif';
elseif( strstr($type_file, 'png')) $ext='png';
if ($ext=='') {
$message = "Le fichier n'est pas une image";
} else {
// on copie le fichier dans le dossier de destination
$name_file = $siren.'.'.$ext;
if( !move_uploaded_file($tmp_file, $this->pathLogo.'/'.$name_file) ) {
$message = "Impossible de copier le fichier dans ".$this->pathLogo;
} else {
$message = "Le fichier a bien été uploadé";
}
}
}
//Suppression ou URL fichier image
} elseif ( isset($_REQUEST['logoUrl']['del']) ||
( isset($_REQUEST['logoUrl']['url']) &&
$_REQUEST['logoUrl']['url']!='' ) ) {
//Suppression du fichier
if ( isset($_REQUEST['logoUrl']['del']) && $_REQUEST['logoUrl']['del'] )
{
$extensions = array('jpeg', 'jpg', 'png', 'gif', 'bmp');
foreach ( $extensions as $ext ) {
if ( file_exists(PATH_LOGOS.$siren.'.'.$ext) ){
unlink($this->pathLogo.'/'.$siren.'.'.$ext);
}
}
} else {
$this->_getlogo($siren);
}
}
return $message;
}
function _saveimg( $siren )
{
$message = '';
if ( isset($_FILES['logoFile']) &&
$_FILES['logoFile']['error']!=UPLOAD_ERR_NO_FILE ) {
$logoFile = $_FILES['logoFile'];
$tmp_file = $logoFile['tmp_name'];
if ( $logoFile['error']!=UPLOAD_ERR_OK ) {
$output = '';
} elseif ( !is_uploaded_file($tmp_file) ){
$output = '';
} else {
// On vérifie maintenant l'extension
$extAuthorized = array('jpeg', 'jpg', 'png', 'gif', 'bmp');
$type_file = $logoFile['type'];
$ext = '';
if ( !in_array($type_file, $extAuthorized ) )
{
$ext = $type_file;
}
if ( !empty($ext) ){
// on copie le fichier dans le dossier de destination
$name_file = 'tmp_'.$siren.'.'.$ext;
if( !move_uploaded_file($tmp_file, $this->pathLogo.'/'.$name_file) ) {
$output = '';
} else {
chmod($this->pathLogo.'/'.$name_file, 0755);
$output = $name_file;
}
} else {
$output = '';
}
}
}
return $output;
}
function _findlogo( $siren, $isin = '' )
{
$img = '';
$extensions = array('jpeg', 'jpg', 'png', 'gif', 'bmp');
//Recherche image sur base siren
foreach ( $extensions as $ext ) {
if ( file_exists($this->pathLogo.'/'.$siren.'.'.$ext) ) {
$img = $siren.'.'.$ext;
break;
}
}
//Recherche image sur base isin
if ( $img == '' && $isin != '' ) {
foreach ( $extensions as $ext ) {
if ( file_exists($this->pathLogo.'/'.$isin.'.'.$ext) ) {
$img = $isin.'.'.$ext;
break;
}
}
}
return $img;
}
function _loadlogo( $siren, $isin = '' )
{
$urlImg = '';
$locImg = $this->pathLogo.'/'.$siren;
$extensions = array('jpeg', 'jpg', 'png', 'gif', 'bmp');
//Recherche image sur base siren
foreach ( $extensions as $ext ) {
if ( file_exists($locImg.'.'.$ext) ) {
$urlImg = '/logos/'.$siren.'.'.$ext;
break;
}
}
//Recherche image sur base isin
if ( $urlImg == '' && $isin != '' ) {
$locImg = PATH_LOGOS . $isin;
foreach ( $extensions as $ext ) {
if ( file_exists($locImg.'.'.$ext) ) {
$urlImg = '/logos/'.$isin.'.'.$ext;
break;
}
}
}
$output = '';
//Redimensionnement
if ( $urlImg != '' ) {
$tabTmp = getimagesize($locImg.'.'.$ext);
$w = $tabTmp[0];
$h = $tabTmp[1];
if ( $w>350 ) {
$strSize = redimage($locImg.'.'.$ext,350,150);
} else {
$strSize = '';
}
$output = '<img src="'.$urlImg.'" '.$strSize.'/>';
}
return $output;
}
function _getlogo( $siren )
{
$logoUrl = $_REQUEST['logoUrl']['url'];
$extAuthorized = array('jpeg', 'jpg', 'png', 'gif', 'bmp');
try {
$client = new Zend_Http_Client($pathUrl);
$client->setStream();
$response = $client->request('GET');
if ( $response->isSuccessful() ) {
$tmp = explode('.', basename($response->getStreamName()));
$ext = strtolower(end($tmp));
if ( !in_array($ext, $extAuthorized ) )
{
$tmp = explode('/', $page['header']['Content-Type']);
$ext = trim ( str_replace('?', '',strtolower(end($tmp)) ) );
}
$name_file = $siren.'.'.$ext;
if( copy($response->getStreamName(), $this->pathLogo.'/'.$name_file) ) {
chmod($this->pathLogo.'/'.$name_file, 0755);
}
}
} catch (Zend_Http_Client_Exception $e) {}
}
}