https://medium.com/@lmeyer./get-an-error-free-e-commerce-web-site-using-sentry-b6061264efc8...
Assembler des images et du text avec ImageMagick sur Prestashop
https://imagemagick.org/index.php
sudo apt-get update
sudo apt-get install php5-imagick
sudo php5enmod imagick
https://www.carlseibert.com/using-imagemagick-on-mamp/
Pour faire mes images de couverture d'article, je passe un un process assez long:
je fais une capture d'ecran, je fais le montage via Photopea, je redimensionne pour faire une miniature etc...
Après avoir décidé de mettre en place la version anglaise, je me suis posé la question de comment optimisé le processus de creation des images de couverture.
J'ai donc décidé de créer un module prestashop qui utilisera ImageMagik un étiteur d'image en ligne de commande.
Objectif:
- création automatique d'une image de couverture et création de la miniature
- l'idée et de superposer deux images plus un texte de titre
https://forums.fedora-fr.org/viewtopic.php?id=34993
<?php
if (!defined('_PS_VERSION_')) {
exit;
}
use Symfony\Component\Filesystem\Exception\IOException;
require_once __DIR__ . '/classes/CoverGenerator.php';
class Cafe_covergenerator extends Module
{
public function __construct()
{
$this->name = 'cafe_covergenerator';
$this->tab = 'administration';
$this->version = '1.0.0';
$this->author = 'presta.cafe';
$this->bootstrap = true;
parent::__construct();
$this->displayName = $this->l('cafe_covergenerator');
$this->description = $this->l('generate blog covers');
}
/**
* Installation du module
* @return bool
*/
public function install()
{
$tab = new Tab();
foreach (Language::getLanguages() as $language) {
$tab->name[$language['id_lang']] = 'Cover Generator';
}
$tab->class_name = 'AdminCoverGenerator';
$tab->module = $this->name;
$idParent = (int)Tab::getIdFromClassName('IMPROVE');
$tab->id_parent = $idParent;
$tab->position = Tab::getNbTabs($idParent);
$tab->icon = 'image';
if (!$tab->save()) {
return false;
}
if (!parent::install()
|| !TesserectOcr::installSql()
) {
return false;
}
}
/**
* Load the configuration form
*/
public function getContent()
{
/**
* If values have been submitted in the form, process.
*/
if (((bool)Tools::isSubmit('submitCoverGenerator')) == true) {
$this->postProcess();
}
return $this->renderForm();
}
/**
* Create the form that will be displayed in the configuration of your module.
*/
protected function renderForm()
{
$helper = new HelperForm();
$helper->show_toolbar = false;
$helper->table = $this->table;
$helper->module = $this;
$helper->default_form_language = $this->context->language->id;
$helper->allow_employee_form_lang = Configuration::get('PS_BO_ALLOW_EMPLOYEE_FORM_LANG', 0);
$helper->identifier = $this->identifier;
$helper->submit_action = 'submitCoverGenerator';
$helper->currentIndex = $this->context->link->getAdminLink('AdminModules', false)
.'&configure='.$this->name.'&tab_module='.$this->tab.'&module_name='.$this->name;
$helper->token = Tools::getAdminTokenLite('AdminModules');
$helper->tpl_vars = array(
'languages' => $this->context->controller->getLanguages(),
'id_language' => $this->context->language->id,
);
return $helper->generateForm(array($this->getConfigForm()));
}
/**
* Create the structure of your form.
*/
protected function getConfigForm()
{
return array(
'form' => array(
'legend' => array(
'title' => $this->l('Settings'),
'icon' => 'icon-cogs',
),
'input' => [
['name'=>'titre','label' => 'Titre','type' => 'textarea',],
['name'=>'image','label' => 'Image','type' => 'file',],
],
'submit' => array(
'title' => $this->l('Save'),
),
),
);
}
protected function postProcess()
{
// path
$path = _PS_MODULE_DIR_.'cafe_covergenerator/images/';
$path_font = _PS_MODULE_DIR_.'cafe_covergenerator/fonts/';
$path_logo = _PS_MODULE_DIR_.'cafe_covergenerator/images/logos/';
$path_mini = _PS_MODULE_DIR_.'cafe_covergenerator/images/miniatures/';
$path_cover = _PS_MODULE_DIR_.'cafe_covergenerator/images/covers/';
//on upload l'image
$uploader = new \Uploader('image');
$uploader->setSavePath(_PS_MODULE_DIR_.'cafe_covergenerator/images');
$uploader->process($image_cover);
// image principale
$image_cover = $_FILES['image']['name'];
$image_cover= new Imagick($path. $_FILES['image']['name']);
//logo
$logo = new Imagick($path_logo . 'prestashop-logo.png');
$logo->resizeImage(207, 167, Imagick::FILTER_LANCZOS, 1);
/* Create some objects */
$background = new Imagick();
$background->newImage(750, 750, new ImagickPixel('rgba(50.2%, 50.2%, 50.2%, 0.8)'));
$background->setImageFormat('png');
/* New image */
$mini = new Imagick();
$pixel = new ImagickPixel();
// generation miniature
$mini->newImage(750, 750, $pixel);
/* Give image a format */
$mini->setImageFormat('png');
$mini->compositeImage($image_cover, Imagick::COMPOSITE_DEFAULT,0, 0);
$mini->compositeImage($background, Imagick::COMPOSITE_DEFAULT,0, 0);
//logo et position
$x = 80;
$y= 50;
$mini->compositeImage($logo, Imagick::COMPOSITE_DEFAULT, $x, $y);
//text
$draw = new ImagickDraw();
$draw->setFillColor('black');
$draw->setFont($path_font . 'DejaVu_Sans/DejaVuSansCondensed-bold.ttf');
$draw->setFontSize(65);
$x = 80;
$y= 280;
$angle = 0;
$padding = 10;
$text = Tools::getValue('titre');
$newtext = wordwrap($text, 15, "\n");
$newtext = strtoupper($newtext);
$mini->annotateImage($draw, $x, $y $padding, $angle, $newtext);
$mini->writeImage($path_mini . 'mini-1.png'); //replace original background
// generation cover
$cover = new Imagick($path. $_FILES['image']['name']);
$cover->compositeImage($mini, Imagick::COMPOSITE_DEFAULT,0, 0);
$cover->cropImage(1920,750,0,0);
$cover->writeImage($path_cover . 'cover-1.png'); //replace original background
//$image1 = new Imagick($path . $_FILES['image1']['name']);
//$composition =
/* Output the image with headers */
header('Content-type: image/png');
echo $cover;
//$overlay->destroy();
//$image->destroy();
//$tesseract = new CoverGenerator($uploader->files[0]['save_path']);
//$text = $tesseract->run();
echo "<pre>";
var_dump('ok');
echo "</pre>";
die();
}
/**
* Désinstallation du module
* @return bool
*/
public function uninstall()
{
if (
!parent::uninstall()
|| !CoverGenerator::uninstallSql()
) {
return false;
}
return true;
}
}
Publié dans:
Pimp Prestashop
Laissez un commentaire