Marcel Eichner // Ephigenia

  • Home
  • Illustration
  • Code
  • Kontakt

Aktuelle Projekte

Horrorblog.org
jQuery.slideShow
Franklin
code.marceleichner.de

This Blog-Website is built with Harrison!

Blogs & Freunde

Gimmixx
Martin Fleck
Torsten Bergler
Jens Franke
Robokid
Peter Kröner
Polycoder
Coding Horror
Lotterliebe
CodeBalancer
Pseudocoder
Migrador
Dachdeckermeister Peter Arold in Werda, Plauen, Hof und Umgebung La Petite Provence - Pension und Festsaal in Leisnig Piv-Berlin, Immobilienverwaltung Verwaltung Berlin blogoscoop

#507

04.07.2011 19:51
0 Kommentare
Share
  • php
  • apache
  • gd-lib
  • image
  • server
  • nginx
  • static
  • content
  • delivery
  • .htaccess
One problem when relaunching large projects with a ton of images is to re-create all the thumbnails that users have uploaded in the years. If you don’t use paperclip (ruby) or anything like it in PHP (is there any like it!?) where you can run run one command to re-create all the thumbnails in all specified sizes your can try to keep it flexible and create every image on demand.

Theory

The Webserver should serve the image if it exists. If the file does not exist, the request should be redirected to a PHP script that searches and creates the requested image file (in requested size) at exactly the location it was originally requested. The second request on the file will not be redirected to the PHP script and will server the image that now exists.

Practice

So the first thing to archive is to send the request of a not existing image to a PHP file. That’s easy if you’re familiar with all the nginx directives:

This rule can be combined with the anti-hotlinking rules for images with nginx I showed you last week.

After that we need to create a format that includes width and height of any requested image so that the
thumbnailer.php
knows which size the created image should have. A valid request for a resized file should always have all parameters (width, height) in it:
../img/public/9c4be029/438xauto/filename.jpg

This makes it easy to split up width, height with a regexp in
thumbnailer.php
. The following code is just an example. You’re surelly integrate the logic into your frameworks:

That’s it! After that you can request any image in any size on your webserver by only creating it once it’s requested.

There are some things you can add, like other parameters in the
$formatRegexp
string to add different resizing methods or even filters, or limitations on the
width
and
height
parameter.

Appendix: Apache

It’s almost the same thing with apache. Just add a few lines to your
.htaccess.
and all your image requests are redirected to the thumbnailer (or anything):

#457

05.06.2009 13:49
0 Kommentare
Share
  • code
  • osx
  • App
  • terminal
  • tool
  • script
  • scripting
  • apple
iTerm ist eine willkommene Alternative zu der nativen Terminal App für OSX. Jetzt kann man die auch mit AppleScript verwursten.
Wenn man es Leid ist immer wieder die selben Terminal Fenster aufzumachen um Apache-Logs zu lesen oder die Datenbank zu überwachen sollte man überlegen ob das nicht noch besser geht. Klaro geht das besser! Mit Apple-Script!
tell application "iTerm"
  activate
 
  -- create server log terminal
  make new terminal
  tell the last terminal
    activate current session
    launch session "Default Session"
    tell the last session
      write text "clear;"
      write text "tail -f /Applications/MAMP/logs/apache_access.log"
      set background color to {15000, 200, 200}
    end tell
  end tell
  set the bounds of the first window to {0, 700, 840, 900}
  set the name of the first window to "apace_access.log"
 
  -- creat working terminal
  make new terminal
  tell the last terminal
    activate current session
    launch session "Default Session"
    tell the last session
      write text "welcome user, start now"
    end tell
  end tell
  set the bounds of the first window to {0, 0, 840, 660}
  set the name of the first window to "workspace"
 
end tell
So kann man ganze Fenster Setups zusammencoden und erspart sich so hoffentlich einen Haufen Zeit.

Nachtrag:
Man kann dann das Apple-Script auch automatisch bei jedem Start von iTerm ausführen lassen indem man es in ~/Library/iTerm/AutoLaunch.scpt ablegt. Weitere Beispiele gibt es auf der Scripting Seite von iTerm.

#456

04.06.2009 10:07
0 Kommentare
Share
  • code
  • php
  • file
  • upload
  • error
  • handling
Viele von den PHP Codern da draussen kennen es wahrscheinlich schon, aber ich will trotzdem mal darauf hinweisen. Datei Uploads in PHP sind ja manchmal etwas verwirrend, vor allem was verschiedene Fehlerquellen angeht. Um schon im Vorhinein Fehler abzufangen bietet sich folgendes Code-Snippet an:
// test if a file was uploaded
$formFieldName = 'myFile';
if (isset($_FILES[$formFieldName])) {
  switch(@$_FILES[$formFieldName]['error']) {
    case UPLOAD_ERR_OK: // 0
      // everything is ok with the upload for php
      break;
    case UPLOAD_ERR_INI_SIZE:
      // file is larger than the size set in php.ini
      // upload_max_filesize
      break;
    case UPLOAD_ERR_FORM_SIZE:
      // file exceeds size set in form
      break;
    case UPLOAD_ERR_PARTIAL:
      // file upload
      break;
    case UPLOAD_ERR_NO_FILE:
      // no file was specified (empty form field)
      break;
    case UPLOAD_ERR_NO_TMP_DIR:
      // no tmp dir specified in php.ini
      break;
    case UPLOAD_ERR_CANT_WRITE:
      // tmp dir from php.ini is not writable for php
      break;
    default:
      // unknown error code
      break;
  }
}
Wie man sieht bietet PHP weit aus mehr Möglichkeiten fehlgeschlagene Datei-Uploads zu erkennen als manchen bewusst ist. Vor allem wenn große Dateien hochgeladen werden die zu groß sind (upload_max_filesize) gibt PHP direkt einen Fehler aus, ohne riesen Dateien anzunehmen.
marceleichner HTML5 Harrison Theme (Validate Source), © 2010 by Ephigenia M. Eichner, Impressum