Skip to content

Instantly share code, notes, and snippets.

@clemgrim
Last active December 9, 2015 15:31
Show Gist options
  • Save clemgrim/b02931d533bc29d3a5b1 to your computer and use it in GitHub Desktop.
Save clemgrim/b02931d533bc29d3a5b1 to your computer and use it in GitHub Desktop.
<?php
use Imagine\Image\ImageInterface;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
set_time_limit(60*10);
require 'vendor/autoload.php';
$serveur = 'localhost';
$login = 'root';
$mdp = '';
$nom_bdd = 'test';
$distDir = realpath('./output/');
$baseDir = realpath('./photos');
$imageExts = ['jpg', 'jpeg', 'png', 'gif'];
$options = [
'resolution-units' => ImageInterface::RESOLUTION_PIXELSPERINCH,
'resolution-x' => 72,
'resolution-y' => 72,
'resampling-filter' => ImageInterface::FILTER_LANCZOS,
'jpeg_quality' => 50,
];
$k = 0;
function human_size($bytes) {
$size = $bytes / 1024;
if ($size < 1024) {
$size = number_format($size, 2);
$size .= ' Ko';
} else {
if ($size / 1024 < 1024) {
$size = number_format($size / 1024, 2);
$size .= ' Mo';
} else if ($size / 1024 / 1024 < 1024) {
$size = number_format($size / 1024 / 1024, 2);
$size .= ' Go';
}
}
return $size;
}
try {
// Image
$imagine = new Imagine\Imagick\Imagine();
$transform = new Imagine\Filter\Transformation();
$it = new RecursiveDirectoryIterator($baseDir, RecursiveDirectoryIterator::SKIP_DOTS);
$files = new RecursiveIteratorIterator($it, RecursiveIteratorIterator::SELF_FIRST);
// BDD
$pdo = new PDO("mysql:host=$serveur;dbname=$nom_bdd", $login, $mdp);
$query = $pdo->prepare(
'INSERT INTO cms_photos (base,resize, size, width, height, dpi, human_size) VALUES (:base,:resize,:size,:width,:height,:dpi,:hsize)'
);
// Logs
$log = new Logger('logger');
$log->pushHandler(new StreamHandler(__DIR__.'/logs/log.txt', Logger::DEBUG));
$transform->thumbnail(new Imagine\Image\Box(900, 900));
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->beginTransaction();
foreach ($files as $info) {
$output = str_replace($baseDir, $distDir, $info->getRealPath());
$log->addDebug('----------------------------');
$log->addDebug("New entry : $output");
if ($info->isDir() && !is_dir($output)) {
if (mkdir($output, true, 0775)) {
$log->addDebug("Create new directory : $output");
}
} else {
$ext = strtolower($info->getExtension());
if (!in_array($ext, $imageExts)) continue;
$pathname = $info->getPathname();
$log->debug('Start resize', ['file' => $pathname, 'index' => ++$k]);
$image = $imagine->open($pathname);
$output = dirname($output);
$input = trim(str_replace($baseDir, '', $info->getRealPath()), DIRECTORY_SEPARATOR);
$filename = md5($input . time()).'.'.$ext;
$dpi = $image->getImagick()->getImageResolution();
$size = $info->getSize();
list ($width, $height) = getimagesize($pathname);
$data = [
':base' => $input,
':resize' => $filename,
':size' => $size,
':width' => $width,
':height' => $height,
':dpi' => $dpi['x'],
':hsize' => human_size($size),
];
$transform->apply($image)->save($output . '/' . $filename, $options);
$log->addDebug('Image resized');
$query->execute($data);
$log->addDebug('Saved in database', $data);
}
}
echo "<strong>$k images trait&eacute;es</strong>";
$pdo->commit();
} catch (Exception $e) {
echo 'Erreur : ' , $e->getMessage();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment