Image Rotation Script
| From: | dev at nkmo dot com | Date: | Sat, 15 Oct 2011 14:50:08 +0000 |
| Subject: | Image Rotation Script | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-315314@lists.php.net to get a copy of this message | ||
We have a simple script which rotates and image to a random value, saves
it to a cache directory and displays it. For some reason when I move the
script from a Debian box over to the production CentOS machine, it no
longer caches any of the images. the rest works, but not the cache. If you
could look at it and see if anything jumps out at you, please let me know..
install the code below to the directory /angles
.htaccess:
RewriteEngine on
RewriteRule ^rotate_(\d+)(?:_(?:\d+))?.png$ rotate.php?im=$1
rotate.php:
<?php
// Setup
if(isset($_GET['im']) && file_exists($_GET['im'].'.png')) {
header('Content-type: image/png');
$im = $_GET['im'].'.png';
$degrees = rand(0, 360);
$save = 'cache/'.$_GET['im'].'_'.$degrees.'.png';
if(!file_exists($save)) {
// Rotate via "command line" and cache it
exec('convert '.$im.' -filter \'Lanczos\' -resize \'150x150\'
-rotate
'.$degrees.' -black-threshold 40% '.$save, $out);
}
// Output out (newly?) cached file
echo file_get_contents($save);
} else {
die("Image not found");
}
?>
Use it by url:
http://www.servername.com/angles/rotate_019.png
Each time you reload page the angle should rotate to a new position.