Skip to content

Commit

Permalink
Use of rvaly/dpegenerator to fix issue #2
Browse files Browse the repository at this point in the history
  • Loading branch information
ilhooq committed Oct 31, 2024
1 parent ff25033 commit 96fb326
Show file tree
Hide file tree
Showing 10 changed files with 148 additions and 133 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
/.buildpath
/.project
/.settings
/dist
/vendor
/tests
14 changes: 14 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name" : "joomla-estate-agency/plg_jea_dpe",
"type" : "project",
"description" : "plugin to display french DPE",
"require": {
"lbigroupp/dpegenerator": "^3.1"
},
"scripts" : {
"release": "php scripts/release.php"
},
"scripts-descriptions": {
"release": "Build a component release in a zip archive."
}
}
61 changes: 61 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

166 changes: 33 additions & 133 deletions dpe.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ public function onAfterShowDescription(Event $event)
{
try
{
$img = $this->generateEnergyImage($row->dpe_energy);
$img = $this->generateEnergyImage($row->dpe_energy, $row->dpe_ges, (int) $row->living_space);
echo '<img src="' . $img . '" alt="' . Text::_('PLG_JEA_DPE_ENERGY_CONSUMPTION') . '" style="margin-right: 50px;" />';
}
catch (Exception $e)
Expand All @@ -161,7 +161,7 @@ public function onAfterShowDescription(Event $event)
{
try
{
$img = $this->generateGESImage($row->dpe_ges);
$img = $this->generateGESImage($row->dpe_ges, $row->dpe_ges, (int) $row->living_space);
echo '<img src="' . $img . '" alt="' . Text::_('PLG_JEA_DPE_EMISSIONS_GES') . '" />';
}
catch (Exception $e)
Expand All @@ -181,158 +181,58 @@ private function getLanguageTag()
return $app->getLanguage()->getTag();
}

private function generateEnergyImage($energy = 0)
private function generateEnergyImage($energy = 0, $ges = 0, $superficie = 0)
{
require __DIR__ . '/vendor/autoload.php';

$type = LBIGroupDpeGenerator\DpeGenerator::DPE_TYPE;

$tag = $this->getLanguageTag();
$imagePath = JPATH_ROOT . '/images/com_jea/dpe/energy-' . $tag . '-' . $energy . '.png';
$imageURL = Uri::root(true) . '/images/com_jea/dpe/energy-' . $tag . '-' . $energy . '.png';

if (!file_exists($imagePath))
{
$levels = array(50, 90, 150, 230, 330, 450);
$labels = array(
'measure' => Text::_('PLG_JEA_DPE_ENERGY_MEASURE'),
'top-left' => Text::_('PLG_JEA_DPE_ENERGY_TOP_LEFT_LABEL'),
'bottom-left' => Text::_('PLG_JEA_DPE_ENERGY_BOTTOM_LEFT_LABEL')
);

$this->generateGDImage($energy, $imagePath, 'dpe-energy.png', $levels, $labels);
$dpe = new LBIGroupDpeGenerator\DpeGenerator();
$dpe->setDpeVal($energy);
$dpe->setGesVal($ges);
if ($superficie) $dpe->setSuperficie($superficie);
$dpe->setPictureType($type);
$dpe->setImageSize(LBIGroupDpeGenerator\DpeGenerator::WEB_SIZE_TYPE);
$dpe->setPathToWriteImage(JPATH_ROOT . '/images/com_jea/dpe/');
$dpe->setNameOfPicture('energy-' . $tag . '-' . $energy);
$dpe->setGenerateImage(true);
$dpe->generatePicture();
}

return $imageURL;
}

private function generateGESImage ($ges = 0)
private function generateGESImage ($energy = 0, $ges = 0, $superficie = 0)
{
require __DIR__ . '/vendor/autoload.php';

$type = LBIGroupDpeGenerator\DpeGenerator::GES_TYPE;

$tag = $this->getLanguageTag();
$imagePath = JPATH_ROOT . '/images/com_jea/dpe/ges-' . $tag . '-' . $ges . '.png';
$imageURL = Uri::root(true) . '/images/com_jea/dpe/ges-' . $tag . '-' . $ges . '.png';

if (!file_exists($imagePath))
{
$levels = array(5, 10, 20, 35, 55, 80);
$labels = array(
'measure' => Text::_('PLG_JEA_DPE_GES_MEASURE'),
'top-left' => Text::_('PLG_JEA_DPE_GES_TOP_LEFT_LABEL'),
'bottom-left' => Text::_('PLG_JEA_DPE_GES_BOTTOM_LEFT_LABEL')
);

$this->generateGDImage($ges, $imagePath, 'dpe-ges.png', $levels, $labels);
}

return $imageURL;
}

private function generateGDImage ($dpeValue, $imagePath, $imageModel, $levels, $labels = array())
{
$currentLevel = 0;
$imgWidth = 300;
$imgHeiht = 260;
$fontFile = JPATH_ROOT . '/plugins/jea/dpe/fonts/DejaVuSans.ttf';
$fontBoldFile = JPATH_ROOT . '/plugins/jea/dpe/fonts/DejaVuSans-Bold.ttf';

foreach ($levels as $level => $value)
{
if ($dpeValue <= $value)
{
$currentLevel = $level;
break;
}
}

if ($currentLevel == 0 && $dpeValue > $levels[count($levels) - 1])
{
$currentLevel = 6;
}

$img = @imagecreatetruecolor($imgWidth, $imgHeiht);

if (! $img)
{
throw new Exception('Cannot create a GD image stream');
}

$white = imagecolorallocate($img, 255, 255, 255);
$grey = imagecolorallocate($img, 200, 200, 200);
$grey2 = imagecolorallocate($img, 40, 40, 40);
imagefill($img, 0, 0, $white);
$arrowImg = @imagecreatefrompng(JPATH_ROOT . '/plugins/jea/dpe/images/arrow.png');
$imgModel = @imagecreatefrompng(JPATH_ROOT . '/plugins/jea/dpe/images/' . $imageModel);

// Where the img model start from Y
$destY = ceil(($imgHeiht - imagesy($imgModel)) / 2);

$dpeY = $destY;

if ($currentLevel == 6)
{
$dpeY += imagesy($imgModel) - 15;
}
else
{
/*
* 30 px height per level + 3px margin
* Adjust now y between the levels limits
*/
$dpeY += $currentLevel * 33;
$start = 0;
$end = $levels[$currentLevel];

if (isset($levels[$currentLevel - 1]))
{
$start = $levels[$currentLevel - 1] + 1;
}

$dpeY += floor(($dpeValue - $start) * 30 / ($end - $start));
$dpe = new LBIGroupDpeGenerator\DpeGenerator();
$dpe->setDpeVal($energy);
$dpe->setGesVal($ges);
if ($superficie) $dpe->setSuperficie($superficie);
$dpe->setPictureType($type);
$dpe->setImageSize(LBIGroupDpeGenerator\DpeGenerator::WEB_SIZE_TYPE);
$dpe->setPathToWriteImage(JPATH_ROOT . '/images/com_jea/dpe/');
$dpe->setNameOfPicture('ges-' . $tag . '-' . $ges);
$dpe->setGenerateImage(true);
$dpe->generatePicture();
}

// Draw horizontal line
imageline($img, 0, $dpeY, $imgWidth, $dpeY, $grey);

// Draw vertical line
imageline($img, 220, 0, 220, $imgHeiht, $grey2);

// Copy the image model
imagecopy($img, $imgModel, 0, $destY, 0, 0, imagesx($imgModel), imagesy($imgModel));
$destX = $imgWidth - imagesx($arrowImg);
$destY = $dpeY - (imagesy($arrowImg) / 2);
imagecopy($img, $arrowImg, $destX, $destY, 0, 0, imagesx($arrowImg), imagesy($arrowImg));

// Add the value
imagettftext($img, 11, 0, $destX + 18, $destY + 20, $white, $fontBoldFile, $dpeValue);

// Add the measure
if (isset($labels['measure']))
{
$box = imagettfbbox(7, 0, $fontFile, $labels['measure']);
$x = $box[4] - $box[0];
imagettftext($img, 7, 0, $imgWidth - $x, $destY + 41, $grey2, $fontFile, $labels['measure']);
}

// Add text to top left
if (isset($labels['top-left']))
{
imagettftext($img, 8, 0, 0, 9, $grey2, $fontFile, $labels['top-left']);
}

// Add text to top right
imagettftext($img, 8, 0, 230, 9, $grey2, $fontFile, Text::_('PLG_JEA_DPE_TOP_RIGHT_LABEL'));

// Add text to bottom left
if (isset($labels['bottom-left']))
{
imagettftext($img, 8, 0, 0, $imgHeiht - 3, $grey2, $fontFile, $labels['bottom-left']);
}

$ret = @imagepng($img, $imagePath);

imagedestroy($img);
imagedestroy($arrowImg);
imagedestroy($imgModel);

if (!$ret)
{
throw new Exception('Cannot save image : ' . $imagePath);
}
return $imageURL;
}
}
Binary file removed fonts/DejaVuSans-Bold.ttf
Binary file not shown.
Binary file removed fonts/DejaVuSans.ttf
Binary file not shown.
Binary file removed images/arrow.png
Binary file not shown.
Binary file removed images/dpe-energy.png
Binary file not shown.
Binary file removed images/dpe-ges.png
Binary file not shown.
37 changes: 37 additions & 0 deletions scripts/release.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

define('ROOT_PATH', dirname(__DIR__));

function addDirectory(string $dirname, ZipArchive $zip)
{
/* @var SplFileInfo[] $files */
$files = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(ROOT_PATH . '/' . $dirname),
RecursiveIteratorIterator::LEAVES_ONLY
);

foreach ($files as $name => $file)
{
// Skip directories (they would be added automatically)
if (!$file->isDir())
{
// Get real and relative path for current file
$filePath = $file->getRealPath();
$relativePath = substr($filePath, strlen(ROOT_PATH) + 1);

// Add current file to archive
$zip->addFile($filePath, $relativePath);
}
}
}

$xml = simplexml_load_file(ROOT_PATH . '/dpe.xml');
$zip = new ZipArchive();
$zip->open(ROOT_PATH . '/dist/plg_jea-' . $xml->version . '.zip', ZipArchive::CREATE|ZipArchive::OVERWRITE);
addDirectory('language', $zip);
addDirectory('vendor', $zip);
$zip->addFile(ROOT_PATH . '/dpe.xml', 'dpe.xml');
$zip->addFile(ROOT_PATH . '/dpe.php', 'dpe.php');
$zip->addFile(ROOT_PATH . '/script.php', 'script.php');

$zip->close();

0 comments on commit 96fb326

Please sign in to comment.