Created
February 4, 2010 15:19
-
-
Save treffynnon/294723 to your computer and use it in GitHub Desktop.
Rotating images
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Original script written by | |
Simon Holywell (simonholywell.com) | |
17/3/2006 | |
You may use this script for any purpose. You can | |
make changes to the code. You may NOT redistribute | |
the code, link back to my site. All derivatives | |
must be released and opensource. | |
THIS NOTICE MUST ALWAYS APPEAR AT THE VERY TOP | |
OF THE SCRIPT NO MATTER HOW MODIFIED IT IS. | |
*/ | |
/* Setup vars below | |
------------------------------------------ */ | |
define('BASE_DIR', '/path/to/base/'); //base absolute path to this script | |
define('FILE_NAME', 'alfa'); //filename w/o the extension or number | |
define('IMAGE_PATH', BASE_DIR.'images/'.FILE_NAME); //where images are stored | |
define('FILE_SUFFIX', '.jpg'); //suffix (usually the extension of the images) | |
define('IMAGE_COUNT', 20); //number of images | |
define('DATA_FILE', BASE_DIR.'storage.dat'); //the file where the data is stored | |
/* File management | |
------------------------------------------ */ | |
if(file_exists(DATA_FILE) && is_readable(DATA_FILE) && is_writable(DATA_FILE)) | |
$storage = fopen(DATA_FILE, "r+"); | |
else | |
$storage = @fopen(DATA_FILE, "w+"); | |
if(!$storage) { | |
print 'Error: You must make the data file readable (chmod 777) and it must exist!'; | |
exit(); | |
} | |
// get the serialized array | |
$image_data = array(); | |
$data = implode("", @file(DATA_FILE)); | |
$image_data = unserialize($data); | |
// add one to move to the next image | |
if($image_data['position']++ >= IMAGE_COUNT) | |
$image_data['position'] = 1; | |
// hit counter | |
$image_data['hit_counter']++; | |
// wipe files contents | |
@fseek($storage, 0); // move back to the beginning of the file | |
@ftruncate($storage, 0); //leave 0 bytes in the file from this point | |
// stick the data back in the file | |
$data = serialize($image_data); | |
$data = @fputs($storage, $data); | |
@fclose($storage); | |
if(!$data){ | |
print 'Error: Could not write the data back to the file. Please make sure that it exists and is chmod\'d to 777.'; | |
exit(); | |
} | |
// verify the required image exists | |
if(!file_exists(IMAGE_PATH.$image_data['position'].FILE_SUFFIX)) { | |
print 'Error: Image ('.IMAGE_PATH.$image_data['position'].FILE_SUFFIX.') cannot be found. Please ensure that you have set the correct number of images and that image directory is readable.'; | |
exit(); | |
} | |
// tell the user's browser that it is an image | |
header("Content-type: image/jpeg"); | |
/* Image Manipulation | |
------------------------------------------ */ | |
// load the image from your selection of images | |
$image = imagecreatefromjpeg(IMAGE_PATH.$image_data['position'].FILE_SUFFIX); | |
$size = getimagesize(IMAGE_PATH.$image_data['position'].FILE_SUFFIX); // Read the size | |
// allocate the text colours | |
$white = imagecolorallocate($image, 255, 255, 255); | |
$black = imagecolorallocate($image, 0, 0, 0); | |
// define the font, x position, y postition | |
$font = 2; | |
$x = 5; | |
$y = $size[1] - 17; // equivalent to 17px from the base of the image | |
// write hit counter (shadow first and then the white) | |
imagestring($image, $font, $x + 2, $y + 2, 'Views: '.$image_data['hit_counter'], $black); | |
imagestring($image, $font, $x, $y, 'Views: '.$image_data['hit_counter'], $white); | |
// write the current position of the slideshow to the image | |
// obviously you will have to take more off of x if you have more | |
// than 100 images other wise the zero will be off "screen" | |
imagestring($image, $font, $size[0] - 50, $y + 2, $image_data['position'].' of '.IMAGE_COUNT, $black); | |
imagestring($image, $font, $size[0] - 52, $y, $image_data['position'].' of '.IMAGE_COUNT, $white); | |
/* Output image | |
------------------------------------------ */ | |
imagejpeg($image); | |
imagedestroy($image); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment