PDA

View Full Version : running scripts



davidjmorin
12-07-2007, 10:40 PM
ok last thing on my todo list this week LOL

i have www.mysite.com/gallery
inside that i have the following:
-gallery.html /shows the gallery of pics
-index.php /checks email for pics
-createthumb.php / creates thums of pics
-folder: images
-folder: thumbs

what i want is if someone goes to mysite.com/gallery it auto runs the createthumb.php script and the index.php script.
how do i go about doing this?

Master_script_maker
12-07-2007, 11:22 PM
please show code of index.php and createthumbs.php

davidjmorin
12-07-2007, 11:40 PM
INDEX.PHP:




<?php
$cfg = array();
######## CONFIG #######
$cfg['upload_dir'] = 'images'; //what directory to upload images to
$cfg['mail_server'] = 'mail.portlandoldport.com'; //email server
$cfg['mail_port'] = '110'; //email server port

/* email server services as described on http://php.net/imap_open */
$cfg['mail_services'] = '/pop3';

$cfg['mail_username'] = 'USERNAME'; //username for the email address to check
$cfg['mail_password'] = 'PASS'; //password for above user $cfg['debug'] = false; //true or false for debugging

/* Accepted File Types: if less than four characters, use a dot as the first char. */
$cfg['accepted'] = array('.3g2','.png','.jpg','.jpe','jpeg','.gif');
######## END CONFIG #########

$pid=2;

$type = array('text', 'multipart', 'message', 'application', 'audio', 'image', 'video', 'other'); $encoding = array('7bit', '8bit', 'binary', 'base64', 'quoted-printable', 'other');

if(!is_dir($cfg['upload_dir'])) {
@mkdir($cfg['upload_dir']) or die('Cannot create directory:
'.$cfg['upload_dir'].'! Make sure the parent folder has write permissions'); }

// open POP connection
$inbox = @imap_open
('{'.$cfg['mail_server'].':'.$cfg['mail_port'].$cfg['mail_services'].'}',
$cfg['mail_username'], $cfg['mail_password']) or die('Connection to server failed.');

// parse message body
function parse($structure) {
global $type;
global $encoding;

// create an array to hold message sections $ret = array();

// split structure into parts
$parts = $structure->parts;

for($x=0; $x<sizeof($parts); $x++) {
$ret[$x]['pid'] = ($x+1);
$that = $parts[$x];

// default to text
if ($that->type == '') {
$that->type = 0;
}
$ret[$x]['type'] = $type[$that->type] . '/' . strtolower($that->subtype);

// default to 7bit
if ($that->encoding == '') {
$that->encoding = 0;
}

$ret[$x]['encoding'] = $encoding[$that->encoding];
$ret[$x]['size'] = strtolower($that->bytes);
$ret[$x]['disposition'] = strtolower($that->disposition);

if ($that->ifparameters == 1) {
$params = $that->parameters;

foreach ($params as $p) {
if($p->attribute == 'NAME') {
$ret[$x]['name'] = $p->value;
break;
}
}
}
}
return $ret;
}

function get_attachments($arr) {
for($x=0; $x < sizeof($arr); $x++) {
if($arr[$x]['disposition'] == 'attachment') {
$ret[] = $arr[$x];
}
}
return $ret;
}

$count = @imap_num_msg($inbox) or die('No Messages in mailbox!'); //echo 'count: ' . $count;

// get message headers and structure
for ($c = 1; $c <= $count; $c++) {
$id = $c;
$headers = imap_header($inbox, $id);
$structure = imap_fetchstructure($inbox, $id);

// if multipart, parse
if(sizeof($structure->parts) > 1) {
$sections = parse($structure);
$attachments = get_attachments($sections); }

if ($cfg['debug']) {
echo 'Structure of message: ' . $id . '<BR><pre>'; print_r($structure); echo '</pre><BR>'; echo 'Sections of message: ' . $id . '<BR><pre>'; print_r($sections); echo '</pre><BR>'; }

// look for specified part
for($x=0; $x<sizeof($sections); $x++) {
if($sections[$x]['pid'] == $pid) {
$dtype = $sections[$x]['type'];
$dencoding = $sections[$x]['encoding'];
$filename = $sections[$x]['name'];
}
}

if ($cfg['debug']) {
echo ' type: ' . $dtype . '<BR>';
echo 'encoding: ' . $dencoding . '<BR>'; echo 'filename: ' . $filename . '<BR>'; echo ' id: ' . $id . '<BR><BR>'; }

$attachment = imap_fetchbody($inbox, $id, $pid);

if (!$cfg['debug']) {

//if (in_array(substr($filename, -4), $cfg['accepted'])) {
if ($dencoding == 'base64') {
// Decode and save attachment to a file
list($usec, $sec) = explode(' ', microtime());
$usec = substr($usec,2,3);
$name = date('Ymd.His');
$fp=fopen($cfg['upload_dir'].'/'.$name.'_'.$filename,'w');
fwrite($fp,imap_base64($attachment));
fclose($fp);
}
//}
}

}

if (!$cfg['debug']) {
// delete all emails
for ($i = 1; $i <= $count; $i++) {
imap_delete($inbox, $i);
}
imap_expunge($inbox);
}

imap_close($inbox);

if (!$cfg['debug']) {
header('Location: '.$cfg['upload_dir']); } ?>


CREATTHUMB.PHP CODE:



<?php


function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )
{
// open the directory
$dir = opendir( $pathToImages );

// loop through it, looking for any/all JPG files:
while (false !== ($fname = readdir( $dir ))) {
// parse path for the extension
$info = pathinfo($pathToImages . $fname);
// continue only if this is a JPEG image
if ( strtolower($info['extension']) == 'jpg' )
{
echo "Creating thumbnail for {$fname} <br />";

// load image and get image size
$img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
$width = imagesx( $img );
$height = imagesy( $img );

// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );

// create a new tempopary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );

// copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );

// save thumbnail into a file
imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
}
}
// close the directory
closedir( $dir );
}

function createGallery( $pathToImages, $pathToThumbs )
{
echo "Creating gallery.html <br />";

$output = "<html>";
$output .= "<head><title>Thumbnails</title></head>";
$output .= "<body>";
$output .= "<table cellspacing=\"0\" cellpadding=\"2\" width=\"500\">";
$output .= "<tr>";

// open the directory
$dir = opendir( $pathToThumbs );

$counter = 0;
// loop through the directory
while (false !== ($fname = readdir($dir)))
{
// strip the . and .. entries out
if ($fname != '.' && $fname != '..')
{
$output .= "<td valign=\"middle\" align=\"center\"><a href=\"{$pathToImages}{$fname}\">";
$output .= "<img src=\"{$pathToThumbs}{$fname}\" border=\"0\" />";
$output .= "</a></td>";

$counter += 1;
if ( $counter % 4 == 0 ) { $output .= "</tr><tr>"; }
}
}
// close the directory
closedir( $dir );

$output .= "</tr>";
$output .= "</table>";
$output .= "</body>";
$output .= "</html>";

// open the file
$fhandle = fopen( "gallery.html", "w" );
// write the contents of the $output variable to the file
fwrite( $fhandle, $output );
// close the file
fclose( $fhandle );
}

// call createThumb function and pass to it as parameters the path
// to the directory that contains images, the path to the directory
// in which thumbnails will be placed and the thumbnail's width.
// We are assuming that the path will be a relative path working
// both in the filesystem, and through the web for links
createThumbs("images/","thumbs/",100);
// call createGallery function and pass to it as parameters the path
// to the directory that contains images and the path to the directory
// in which thumbnails will be placed. We are assuming that
// the path will be a relative path working
// both in the filesystem, and through the web for links
createGallery("images/","thumbs/");
?>


GALLERY.HTML:


<html><head><title>Thumbnails</title></head><body><table cellspacing="0" cellpadding="2" width="500"><tr><td valign="middle" align="center"><a href="images/dsc01376.jpg"><img src="thumbs/dsc01376.jpg" border="0" /></a></td><td valign="middle" align="center"><a href="images/ethan christmas 3.jpg"><img src="thumbs/ethan christmas 3.jpg" border="0" /></a></td><td valign="middle" align="center"><a href="images/20071207.153126_ethans smiling santa pic people.jpg"><img src="thumbs/20071207.153126_ethans smiling santa pic people.jpg" border="0" /></a></td><td valign="middle" align="center"><a href="images/1111071014.jpg"><img src="thumbs/1111071014.jpg" border="0" /></a></td></tr><tr><td valign="middle" align="center"><a href="images/dsc01372.jpg"><img src="thumbs/dsc01372.jpg" border="0" /></a></td><td valign="middle" align="center"><a href="images/1111071051.jpg"><img src="thumbs/1111071051.jpg" border="0" /></a></td><td valign="middle" align="center"><a href="images/1111071057.jpg"><img src="thumbs/1111071057.jpg" border="0" /></a></td><td valign="middle" align="center"><a href="images/scottande.jpg"><img src="thumbs/scottande.jpg" border="0" /></a></td></tr><tr></tr></table></body></html>

Master_script_maker
12-08-2007, 01:13 PM
since when people go to www.mysite.com/gallery it will load index.php
so to run createthumbs.php add this to index.php:

include ("createthumbs.php");
createthumbs($pathToImages, $pathToThumbs, $thumbWidth);

davidjmorin
12-10-2007, 02:52 AM
Hey thanks alot. works like a charm.