Add script for resizing group logos.
authorRaphaël Barrois <raphael.barrois@polytechnique.org>
Mon, 9 May 2011 20:07:21 +0000 (22:07 +0200)
committerRaphaël Barrois <raphael.barrois@polytechnique.org>
Mon, 9 May 2011 20:07:48 +0000 (22:07 +0200)
Signed-off-by: Raphaël Barrois <raphael.barrois@polytechnique.org>
upgrade/1.1.1/group_logos.php [new file with mode: 0755]
upgrade/1.1.1/update.sh

diff --git a/upgrade/1.1.1/group_logos.php b/upgrade/1.1.1/group_logos.php
new file mode 100755 (executable)
index 0000000..b95a046
--- /dev/null
@@ -0,0 +1,56 @@
+#!/usr/bin/php5
+<?php
+
+require_once 'connect.db.inc.php';
+
+$globals->debug = 0; // Do not store backtraces.
+$MAX_X = 200;
+$MAX_Y = 100;
+
+$it = XDB::rawIterator('SELECT  id, diminutif, logo, logo_mime
+                          FROM  groups
+                         WHERE  logo IS NOT NULL AND logo != ""');
+
+while ($row = $it->next()) {
+  $group_id = $row['id'];
+  $group_name = $row['diminutif'];
+  $logo = $row['logo'];
+  $mime = $row['mime'];
+  $img = imagecreatefromstring($logo);
+  if ($img === false) {
+    print "\n\nError reading image for:\n     $group_name\n\n";
+    continue;
+  }
+  $x = imagesx($img);
+  $y = imagesy($img);
+  $nx = $x;
+  $ny = $y;
+  if ($x > $MAX_X || $y > $MAX_Y) {
+    if ($x > $MAX_X) {
+      $ny = intval($y * $MAX_X / $x);
+      $nx = $MAX_X;
+    }
+    if ($y > $MAX_Y) {
+      $nx = intval($x*$MAX_Y/$y);
+      $ny = $MAX_Y;
+    }
+    $img2 = imagecreatetruecolor($nx, $ny);
+    imagealphablending($img2, false);
+    imagesavealpha($img2,true);
+    $transparent = imagecolorallocatealpha($img2, 255, 255, 255, 127);
+    imagefilledrectangle($img2, 0, 0, $nx, $ny, $transparent);
+    imagecopyresampled($img2, $img, 0, 0, 0, 0, $nx, $ny, $x, $y);
+    $tmpf = tempnam('/tmp', 'upgrade_111_group_logos');
+    imagepng($img2, $tmpf);
+    $f = fopen($tmpf, 'r');
+    $logo2 = fread($f, filesize($tmpf));
+    fclose($f);
+    unlink($tmpf);
+    XDB::execute("UPDATE  groups
+                     SET  logo = {?}, logo_mime = 'image/png'
+                   WHERE  id = {?}", $logo2, $group_id);
+  }
+}
+
+
+
index 9230e34..6269d56 100755 (executable)
@@ -7,3 +7,6 @@
 
 confirm "* Running database upgrade scripts"
 mysql_run_directory .
+
+confirm "* Running upgrade scripts"
+script_run ./group_logos.php