Files
magento2-modules/app/code/Wowow/Debug/Console/Command/LogoCommand.php
T
2017-12-07 08:57:31 +01:00

190 lines
8.7 KiB
PHP

<?php
namespace Wowow\Debug\Console\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Console\Question\Question;
use Wowow\Debug\Console\Command\LogoOptionsInterface as Options;
class LogoCommand extends Command
{
protected $_input;
protected $_resourceConfig;
protected $_directoryList;
const SCOPE = 'scope';
const SCOPE_ID = 'scope_id';
public function __construct(
\Magento\Config\Model\ResourceModel\Config $resourceConfig,
\Magento\Framework\App\Filesystem\DirectoryList $directoryList
){
parent::__construct();
$this->_resourceConfig = $resourceConfig;
$this->_directoryList = $directoryList;
}
protected function configure()
{
$this->setName('wowow:logo')->setDescription('Change logo pour le store et email.')
->setDefinition([
new InputOption(
Options::SCOPE,
'-s',
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
'scope stores ou default',
['default']
),
new InputOption(
Options::SCOPE_ID,
'-i',
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
'Id du store à configurer',
['0']
),
new InputOption(
Options::FAVICON,
'-f',
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
'change pour favicon',
['1']
),
new InputOption(
Options::LOGO,
'-l',
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
'change pour logo',
['1']
),
new InputOption(
Options::EMAIL,
'-e',
InputOption::VALUE_IS_ARRAY | InputOption::VALUE_OPTIONAL,
'change pour email',
['1']
)
]);
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$this->_input = $input;
$scope = $this->_input->getOption(Options::SCOPE)[0];
$scopeId = $this->_input->getOption(Options::SCOPE_ID)[0];
$faviconOption = $this->_input->getOption(Options::FAVICON)[0];
$logoOption = $this->_input->getOption(Options::LOGO)[0];
$emailOption = $this->_input->getOption(Options::EMAIL)[0];
$finder = new Finder();
$finder->files()->in(__DIR__.'/../../view/frontend/web/media/logo');
$faviconFolder = $this->_directoryList->getPath('media').'/favicon/';
if(!is_dir($faviconFolder)) mkdir($faviconFolder,0775);
$logoFolder = $this->_directoryList->getPath('media').'/logo/';
if(!is_dir($logoFolder)) mkdir($logoFolder,0775);
$folderEmail = $this->_directoryList->getPath('media').'/email/';
$logoEmail = $folderEmail.'logo/';
if(!is_dir($folderEmail)) mkdir($folderEmail,0775);
if(!is_dir($logoEmail)) mkdir($logoEmail,0775);
$arrayLogo = array();
foreach ($finder as $file) {
array_push($arrayLogo,array('realPath' => $file->getRealPath(),'relativePathname'=>$file->getRelativePathname()));
}
foreach ($arrayLogo as $key => $logo) {
$output->writeln($key.' => '.$logo['relativePathname']);
}
$helper = $this->getHelper('question');
$question = new Question('Quel logo ? ','-1');
$logoId = $helper->ask($input,$output,$question);
if($logoId != '-1'){
if($scope == 'default'){
$subfolder = 'default/';
}else{
$subfolder = 'stores/'.$scopeId.'/';
}
$faviconFolder .= $subfolder;
$logoFolder .= $subfolder;
$logoEmail .= $subfolder;
if(!is_dir($faviconFolder)) mkdir($faviconFolder,0775);
if(!is_dir($logoFolder)) mkdir($logoFolder,0775);
if(!is_dir($logoEmail)) mkdir($logoEmail,0775);
if($faviconOption != '0') $this->copyLogo($arrayLogo[$logoId]['realPath'],$faviconFolder.$arrayLogo[$logoId]['relativePathname'],pathinfo($arrayLogo[$logoId]['realPath'])['extension'],array('width'=>32));
if($logoOption != '0') $this->copyLogo($arrayLogo[$logoId]['realPath'],$logoFolder.$arrayLogo[$logoId]['relativePathname'],pathinfo($arrayLogo[$logoId]['realPath'])['extension'],array('width'=>100));
if($emailOption != '0') $this->copyLogo($arrayLogo[$logoId]['realPath'],$logoEmail.$arrayLogo[$logoId]['relativePathname'],pathinfo($arrayLogo[$logoId]['realPath'])['extension'],array('width'=>100));
if($faviconOption != '0') $this->_resourceConfig->saveConfig('design/head/shortcut_icon',$subfolder.$arrayLogo[$logoId]['relativePathname'],$scope,$scopeId);
if($logoOption != '0') $this->_resourceConfig->saveConfig('design/header/logo_src',$subfolder.$arrayLogo[$logoId]['relativePathname'],$scope,$scopeId);
if($emailOption != '0') $this->_resourceConfig->saveConfig('design/email/logo',$subfolder.$arrayLogo[$logoId]['relativePathname'],$scope,$scopeId);
}
$command = $this->getApplication()->find('cache:clean');
$returnCode = $command->run(new ArrayInput(array('command' => 'cache:clean')),$output);
if(!$returnCode) $output->writeln('successfully finished');
}
public function copyLogo($source, $destination,$ext, $taille = null)
{
if($taille){
if(($ext == 'png') || ($ext == 'jpg') || ($ext == 'jpeg')){
list($width, $height) = getimagesize($source);
$ratio_orig = $width/$height;
if(isset($taille['width'])){ $new_width = $taille['width'];}else{ $new_width = $taille['height']*$ratio_orig; }
if(isset($taille['height'])){ $new_height = $taille['height']; }else{ $new_height = $taille['width']/$ratio_orig; }
$image_p = imagecreatetruecolor($new_width, $new_height);
imagealphablending( $image_p, false );
imagesavealpha( $image_p, true );
ini_set("memory_limit","1024M");
if($ext == 'png'){
$image = imagecreatefrompng($source);
}else if(($ext == 'jpg') || ($ext == 'jpeg')){
$image = imagecreatefromjpeg($source);
}else{
return 'image en jpg ou png';
}
$imageFinal = imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
return imagepng($image_p,$destination);
}else if($ext == 'svg'){
$xmlget = simplexml_load_string(file_get_contents($source));
$xmlattributes = $xmlget->attributes();
$width = (string) $xmlattributes->width;
$height = (string) $xmlattributes->height;
$ratio_orig = $width/$height;
if(isset($taille['width'])){ $new_width = $taille['width'];}else{ $new_width = $taille['height']*$ratio_orig; }
if(isset($taille['height'])){ $new_height = $taille['height']; }else{ $new_height = $taille['width']/$ratio_orig; }
$this->resizeSVG($source, $destination,$new_width,$new_height);
}
}else{
copy($source,$destination);
}
}
public function resizeSVG($source,$destination,$width,$height){
$svg = file_get_contents($source);
// I prefer to use DOM, because it's safer and easier as to use preg_match
$svg_dom = new \DomDocument();
libxml_use_internal_errors(true);
$svg_dom->loadXML($svg);
libxml_use_internal_errors(false);
//get width and height values from your svg
$tmp_obj = $svg_dom->getElementsByTagName('svg')->item(0);
$svg_width = floatval($tmp_obj->getAttribute('width'));
$svg_height = floatval($tmp_obj->getAttribute('height'));
// set width and height of your svg to preferred dimensions
$tmp_obj->setAttribute('width', $width.'px');
$tmp_obj->setAttribute('height', $height.'px');
file_put_contents($destination, $svg_dom->saveXML());
}
}