Questo script PHP che deve essere utilizzato da linea di comando, permette di analizzare una directory per riconoscere e “marcare” le immagini con orientamento verticale da quelle con orientamento orizzontale. In questo modo è possibile separare i due blocchi di file, per esempio per comporre una galleria fotografica omogenea nel layout.
Lo script utilizza la funzione PHP getimagesize() per calcolare il rapporto fra altezza e larghezza di una immagine jpg. Se il rapporto è maggiore o uguale a 1 è verticale, altrimenti è orizzontale. (N.B.: le immagini quadrate risulteranno verticali…).
I parametri in ingresso sono 2. Il primo definisce il percorso della directory che contiene le immagini e il secondo indica se si vogliono rinominare i file o copiarli. Al nome dei file viene aggiunta una stringa “_H_” per indicare che l’immagine è orizzontale e “_V_” per le immagini ad orientamento verticale. Queste stringhe possono essere cambiate modificando i valori delle due costanti “PRE_H” e “PRE_V” alle righe 26 e 27 dello script.
Se lo script viene lanciato senza nessun parametro vengono analizzati i file della directory corrente e viene prodotto l’output dell’orientamento per ogni file e di come verrebero modificati i nomi dei file, senza però effettuare alcuna modifica.
Questo è il codice:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
| #!/usr/bin/php
<?php
/*
* sortimages.php
*
* Copyright 2015 mario spada <spadamar@spadamar.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/
define("PRE_H","_H_");
define("PRE_V","_V_");
function getImgMode($filename) {
$size = getimagesize($filename);
$width = $size[0];
$height = $size[1];
$aspect = $height / $width;
if ($aspect >= 1) $mode = "vertical";
else $mode = "horizontal";
return $mode;
}
$directory = empty($argv[1]) ? "./" : rtrim($argv[1], '/') . '/';
if (!file_exists($directory))
die($directory." directory does not exist!");
$renameFls = empty($argv[2]) ? 0 : $argv[2];
if (!empty($renameFls)){
if($renameFls == 'rename') {
$renameFls = 1;
} elseif($renameFls == 'copy') {
$renameFls = 2;
} else {
$renameFls = 0;
}
}
$images = glob($directory."*.{jpg,JPG,jpeg}", GLOB_BRACE);
foreach($images as $image) {
$path_parts = pathinfo($image);
$newFln = "";
$action = " It will change in: ";
$imgLayout = getImgMode($image);
if($imgLayout=='horizontal') {
$newFln = $directory.PRE_H.$path_parts['filename'].".".$path_parts['extension'];
} else {
$newFln = $directory.PRE_V.$path_parts['filename'].".".$path_parts['extension'];
}
if ( $renameFls == 1 ) {
$res = @rename ( $image , $newFln );
$action = !$res ? " Error while renaming with: " : " Renamed with: ";
}
if ( $renameFls == 2 ) {
$res = @copy ( $image , $newFln );
$action = !$res ? " Error while copying in: " : " Copied in: ";
}
echo "File: ".$image." is ".$imgLayout.". ".$action.$newFln."\n";
}
?> |
#!/usr/bin/php
<?php
/*
* sortimages.php
*
* Copyright 2015 mario spada <spadamar@spadamar.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*
*
*/ define("PRE_H","_H_");
define("PRE_V","_V_"); function getImgMode($filename) {
$size = getimagesize($filename);
$width = $size[0];
$height = $size[1];
$aspect = $height / $width;
if ($aspect >= 1) $mode = "vertical";
else $mode = "horizontal";
return $mode;
} $directory = empty($argv[1]) ? "./" : rtrim($argv[1], '/') . '/';
if (!file_exists($directory))
die($directory." directory does not exist!"); $renameFls = empty($argv[2]) ? 0 : $argv[2]; if (!empty($renameFls)){
if($renameFls == 'rename') {
$renameFls = 1;
} elseif($renameFls == 'copy') {
$renameFls = 2;
} else {
$renameFls = 0;
}
} $images = glob($directory."*.{jpg,JPG,jpeg}", GLOB_BRACE); foreach($images as $image) {
$path_parts = pathinfo($image);
$newFln = "";
$action = " It will change in: ";
$imgLayout = getImgMode($image);
if($imgLayout=='horizontal') {
$newFln = $directory.PRE_H.$path_parts['filename'].".".$path_parts['extension'];
} else {
$newFln = $directory.PRE_V.$path_parts['filename'].".".$path_parts['extension'];
}
if ( $renameFls == 1 ) {
$res = @rename ( $image , $newFln );
$action = !$res ? " Error while renaming with: " : " Renamed with: ";
}
if ( $renameFls == 2 ) {
$res = @copy ( $image , $newFln );
$action = !$res ? " Error while copying in: " : " Copied in: ";
}
echo "File: ".$image." is ".$imgLayout.". ".$action.$newFln."\n";
} ?>
Un esempio di utilizzo in ambiente Linux rinominando i file jpg della directory “tmp” nella propria home (dove ho messo 10 file verticali e 10 orizzontali di prova):
~$ ./sortimages.php ~/tmp rename
e questo è l’output:
File: /home/mario/tmp/IMG_5546.JPG is horizontal. Renamed with: /home/mario/tmp/_H_IMG_5546.JPG
File: /home/mario/tmp/IMG_5547.JPG is vertical. Renamed with: /home/mario/tmp/_V_IMG_5547.JPG
File: /home/mario/tmp/IMG_5549.JPG is vertical. Renamed with: /home/mario/tmp/_V_IMG_5549.JPG
File: /home/mario/tmp/IMG_5551.JPG is horizontal. Renamed with: /home/mario/tmp/_H_IMG_5551.JPG
File: /home/mario/tmp/IMG_5552.JPG is vertical. Renamed with: /home/mario/tmp/_V_IMG_5552.JPG
File: /home/mario/tmp/IMG_5553.JPG is horizontal. Renamed with: /home/mario/tmp/_H_IMG_5553.JPG
File: /home/mario/tmp/IMG_5554.JPG is horizontal. Renamed with: /home/mario/tmp/_H_IMG_5554.JPG
File: /home/mario/tmp/IMG_5556.JPG is horizontal. Renamed with: /home/mario/tmp/_H_IMG_5556.JPG
File: /home/mario/tmp/IMG_5558.JPG is vertical. Renamed with: /home/mario/tmp/_V_IMG_5558.JPG
File: /home/mario/tmp/IMG_5559.JPG is horizontal. Renamed with: /home/mario/tmp/_H_IMG_5559.JPG
File: /home/mario/tmp/IMG_5560.JPG is horizontal. Renamed with: /home/mario/tmp/_H_IMG_5560.JPG
File: /home/mario/tmp/IMG_5561.JPG is vertical. Renamed with: /home/mario/tmp/_V_IMG_5561.JPG
File: /home/mario/tmp/IMG_5562.JPG is horizontal. Renamed with: /home/mario/tmp/_H_IMG_5562.JPG
File: /home/mario/tmp/IMG_5563.JPG is vertical. Renamed with: /home/mario/tmp/_V_IMG_5563.JPG
File: /home/mario/tmp/IMG_5564.JPG is vertical. Renamed with: /home/mario/tmp/_V_IMG_5564.JPG
File: /home/mario/tmp/IMG_5565.JPG is vertical. Renamed with: /home/mario/tmp/_V_IMG_5565.JPG
File: /home/mario/tmp/IMG_5566.JPG is vertical. Renamed with: /home/mario/tmp/_V_IMG_5566.JPG
File: /home/mario/tmp/IMG_5571.JPG is horizontal. Renamed with: /home/mario/tmp/_H_IMG_5571.JPG
File: /home/mario/tmp/IMG_5575.JPG is vertical. Renamed with: /home/mario/tmp/_V_IMG_5575.JPG
File: /home/mario/tmp/IMG_5576.JPG is horizontal. Renamed with: /home/mario/tmp/_H_IMG_5576.JPG
Potete scaricare il file in formato compresso .zip qui