Skip to content

Instantly share code, notes, and snippets.

@rabehasy
Created March 22, 2020 14:52
Show Gist options
  • Save rabehasy/a3b01c8e9435537e9da1a83b65789233 to your computer and use it in GitHub Desktop.
Save rabehasy/a3b01c8e9435537e9da1a83b65789233 to your computer and use it in GitHub Desktop.
<?php
/**
* Usage :
* php src/scriptmagick.php "source" "destination"
*/
namespace Miary;
require_once __DIR__.'/../vendor/autoload.php';
use Symfony\Component\Process\Process;
// 3 args required
if ($argc!==3) {
throw new ArgsmissingException('Arguments manquants ' . $argc);
}
// Folders source and destination
$source_folder = $argv[1] . '/';
$destination_folder = $argv[2] . '/';
// Texte added in Image
$texte_image = 'VENDU';
// font name used to write text
$texte_image_font = 'DejaVu-Sans caption';
$text_color = 'black';
$background_color = 'rgba(255,255,255,0.3)';
// All files inside source folder
$files_source = glob($source_folder . '*');
if (empty($files_source)) {
throw new SourcemissingException('Fichiers manquants ou dossier inexistant ' . $source_folder);
}
// Each files in source folder
foreach ($files_source as $filename) {
$finfo = new \SplFileInfo($filename);
// get Filename extension and basename - useful to create same file in destination folder
$file_extension = $finfo->getExtension();
$file_basename = $finfo->getBasename();
$file_basename_without_extension = str_replace('.'.$file_extension, '', $file_basename);
// Tmp filename to create - eg blurred image next step
$filename_tmp = uniqid().'.'.$file_extension;
// get Image dimension
list($width, $height, $type, $attr) = getimagesize($filename);
// Blurred Image
$cmd_blur = 'magick '.$filename.' -blur 0x6 '.$filename_tmp ;
$arrCmdBlur = explode(' ',$cmd_blur);
$process = new Process($arrCmdBlur);
$process->run();
// Add background opacity and text center and output filename to destination folder
$filename_destination = $destination_folder . $file_basename_without_extension.'_'.uniqid().'.'.$file_extension;
$cmd_destination = 'magick -background ' . $background_color . ' -fill ' . $text_color . ' -gravity center -size '.$width.'x'.$height;
$cmd_destination .= ' -font ' . $texte_image_font . ':' . $texte_image . ' '.$filename_tmp.' +swap -gravity south -composite '.$filename_destination;
$arrCmdDestination = explode(' ',$cmd_destination);
$process = new Process($arrCmdDestination);
$process->run();
// Remove Blured image
unlink($filename_tmp);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment