Function problem

Started by The Wizard, April 03, 2013, 11:09:05 AM

Previous topic - Next topic

The Wizard

Does anyone see the problem why this function is not working?


Function createPng()
{
// Create an Image from it so we can do the resize
$src = imagecreatefrompng($uploadedfile);

// Capture the original size of the uploaded image
list($width, $height) = getimagesize($uploadedfile);

//
$tmp = imagecreatetruecolor($newwidth, $newheight);

// This line actually does the image resizing, copying from the original image into the $tmp image
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Write the resized image to image/small/ Dir
$filename = $thumbnail_dir . $image;
imagepng($tmp, $filename, 9);

// Clean up the temp file it created when the request has completed.
imagedestroy($src);
imagedestroy($tmp);
}

$call = 'create'. ucfirst($extension);

// Create the thumbnail
$call();

emanuele

First line: where do you get the variable $uploadedfile?


Take a peek at what I'm doing! ;D




Hai bisogno di supporto in Italiano?

Aiutateci ad aiutarvi: spiegate bene il vostro problema: no, "non funziona" non è una spiegazione!!
1) Cosa fai,
2) cosa ti aspetti,
3) cosa ottieni.

The Wizard


Function BannerAdd()
{
global $context, $txt, $settings;

loadTemplate('banner_add');

// File path of where the uploaded images go.
$uploaddir = $settings['default_theme_dir'] . '/images/banners/banner_images/big/';

// File path of where the uploaded images go.
$url_path = $settings['default_theme_url'] . '/images/banners/banner_images/big/';

// File path of where the Thumbnail images go
$thumbnail_dir = $settings['default_theme_dir'] . '/images/banners/banner_images/small/';

// The set width the banner needs to be.
// WARNING this will affect how your template looks. Do not change unless you fix your template code.
$max_width = '1024';

// The set height the banner needs to be.
// WARNING this will affect how your template looks.Do not change unless you fix your template code.
$max_height = '200';

// The max file size is in bites.
$max_size = '500000';

// Allowed image file extensions
$allowed_files = array(
'jpeg',
'jpg',
'png',
'gif'
);

//This variable is used as a flag.
//If the error occurs the file will not be uploaded.
$errors = array();

if (isset($_POST['submit']))
{

// Protect the forum. Checks to make sure data is coming form the banner_add_template.php
checkSession('post');

// Name of uploaded file
$image = $_FILES['image']['name'];

// Temporary name assigned to the uploaded file
$temp_name = $_FILES['image']['tmp_name'];

if(empty($image) or empty($temp_name) or empty($_FILES))
fatal_lang_error('banner_file_check_error', false);

// All this code below is used to get the file name and file extension separate.
$parts = explode(".", $image);
$extension = end($parts);
$extension = strtolower($extension);
$name = str_replace("." . $extension, "", $image);

// Uploaded the image
if (move_uploaded_file($temp_name, $uploaddir . $image))
{
// 1. Check to see if image size is 1024 by 200
if (list($width, $height) = getimagesize($uploaddir . $image))
{
if ($width != $max_width && $height != $max_height)
$errors[] = $txt['banner_error_1'];
}
else
// 2. Check to see if the uploaded file is a image or not
$errors[] = $txt['banner_error_2'];

// 3. Check to see if file size is under the set max limit
if ($_FILES['image']['size'] > $max_size)
$errors[] = $txt['banner_error_3'];

// 4. Check to see if the monthly file name is spelled correct
$month = array(
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
);

if (!in_array($name, $month))
$errors[] = $txt['banner_error_4'];

// 5. Check to see if the allowed image extension is correct
if (!in_array($extension, $allowed_files))
$errors[] = $txt['banner_error_5'];
}
else
// 6. If the system is unable to upload image for any reason print Unable to upload file
$errors[] = $txt['banner_error_6'];

//Display errors and print Copy unsuccessful message, and remove image.
if (!empty($errors))
{
$context['banner_errors'] = $errors;
unlink($uploaddir . $image);
$context['banner_error_general'] = $txt['banner_error_general'];
}
else
{

// Start of thumbnail creation

// This is the temporary file created by PHP
$uploadedfile = $uploaddir . $image;

// Width and Height of thumbnail to be created
$newwidth  = 400;
$newheight = 78;

Function createJpg()
{
//if ($extension == "jpeg" or $extension == "jpg")
// Create an Image from it so we can do the resize
$src = imagecreatefromjpeg($uploadedfile);

// Capture the original size of the uploaded image
list($width, $height) = getimagesize($uploadedfile);

//
$tmp = imagecreatetruecolor($newwidth, $newheight);

// This line actually does the image resizing, copying from the original image into the $tmp image
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Write the resized image to image/small/ Dir
$filename = $thumbnail_dir . $image;
imagejpeg($tmp, $filename, 100);

// Clean up the temp file it created when the request has completed.
imagedestroy($src);
imagedestroy($tmp);
}

Function createGif()
{
// Create an Image from it so we can do the resize
$src = imagecreatefromgif($uploadedfile);

// Capture the original size of the uploaded image
list($width, $height) = getimagesize($uploadedfile);

//
$tmp = imagecreatetruecolor($newwidth, $newheight);

// This line actually does the image resizing, copying from the original image into the $tmp image
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Write the resized image to image/small/ Dir
$filename = $thumbnail_dir . $image;
imagegif($tmp, $filename);

// Clean up the temp file it created when the request has completed.
imagedestroy($src);
imagedestroy($tmp);
}

// Notes: imagepng() takes a quality value range of 0 to 9,
// while imagejpeg() takes a range of 0 to 100 and
// imagegif() doesn't take any such parameter.
//you will get a error if you try to save a PNG with a quality of 100.

Function createPng()
{
// Create an Image from it so we can do the resize
$src = imagecreatefrompng($uploadedfile);

// Capture the original size of the uploaded image
list($width, $height) = getimagesize($uploadedfile);

//
$tmp = imagecreatetruecolor($newwidth, $newheight);

// This line actually does the image resizing, copying from the original image into the $tmp image
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Write the resized image to image/small/ Dir
$filename = $thumbnail_dir . $image;
imagepng($tmp, $filename, 9);

// Clean up the temp file it created when the request has completed.
imagedestroy($src);
imagedestroy($tmp);
}

$call = 'create'. ucfirst($extension);

// Create the thumbnail
$call();

// End of thumbnail creation

//If no errors registered, print the success message
echo '<meta http-equiv="refresh" content="0">';
redirectexit('action=admin;area=banner_add');
$context['banner_ok'] = $txt['banner_successful'];

}
}

// Show all Monthly Banner Images

$d = dir($thumbnail_dir);
while (false !== ($entry = $d->read()))
{
if ($entry != '.' && $entry != '..' && !is_dir($url_path . $entry))
$Images[] = $entry;
}
$d->close();

if (!empty($d) && is_object($d))
$context['banner_file_check_error'] = $txt['banner_file_check_error'];

$months_with_extension = array();

$months = array(
'january' => 1,
'february' => 2,
'march' => 3,
'april' => 4,
'may' => 5,
'june' => 6,
'july' => 7,
'august' => 8,
'september' => 9,
'october' => 10,
'november' => 11,
'december' => 12
);

foreach ($Images as $image_name)
{
$month_pieces = explode('.', strtolower($image_name));

// I want to be sure that:
//   1) the month has the correct number of dots in the name (1),
//   2) the month name is valid,
//   3) the extension is allowed.
if (count($month_pieces) == 2 && isset($months[$month_pieces[0]]) && in_array($month_pieces[1], $allowed_files))
$months_with_extension[$months[$month_pieces[0]]] = $image_name;
else
{
// This is a file that if not a month
}
}

ksort($months_with_extension);

// Output only January to June Banners
$output_one = array_intersect_key($months_with_extension, array(
'1' => 1,
'2' => 1,
'3' => 1,
'4' => 1,
'5' => 1,
'6' => 1
));

// Send January to June Banner data to template
$context['banner_show_images_one'] = $output_one;

// Output only July to December Banners
$output_two = array_intersect_key($months_with_extension, array(
'7' => 1,
'8' => 1,
'9' => 1,
'10' => 1,
'11' => 1,
'12' => 1
));

// Send July to December Banner data to template
$context['banner_show_images_two'] = $output_two;

// End Show all Monthly Banner Images
}

// End of Script

Arantor

You have this magical variable $uploadedfile but you never pass it from outside into the createPng() function...

The Wizard

So I did not include some variables. Should have thouhgt of that myself. So I created this new function to solve the issue, but the thing still does not work.



Function thumbnailSettings()
{

// Name of uploaded file
$image = $_FILES['image']['name'];

// File path of where the uploaded images go.
$uploaddir = $settings['default_theme_dir'] . '/images/banners/banner_images/big/';

// This is the temporary file created by PHP
$uploadedfile = $uploaddir . $image;

// Width and Height of thumbnail to be created
$newwidth  = 400;
$newheight = 78;

// File path of where the Thumbnail images go
$thumbnail_dir = $settings['default_theme_dir'] . '/images/banners/banner_images/small/';
}

Function createPng()
{
// Get settings needed to create thumbnails
thumbnailSettings();

                                // Create an Image from it so we can do the resize
$src = imagecreatefrompng($uploadedfile);

// Capture the original size of the uploaded image
list($width, $height) = getimagesize($uploadedfile);

//
$tmp = imagecreatetruecolor($newwidth, $newheight);

// This line actually does the image resizing, copying from the original image into the $tmp image
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Write the resized image to image/small/ Dir
$filename = $thumbnail_dir . $image;
imagepng($tmp, $filename, 9);

// Clean up the temp file it created when the request has completed.
imagedestroy($src);
imagedestroy($tmp);
}

$call = 'create'. ucfirst($extension);

// Create the thumbnail
$call();

Suki

I see you are using my "$call" suggestion, if thats the way you wish to follow then you need to create separate functions for each extension case and pass the values you need to said functions.

functions does not load other functions local vars on their-selfs, you need to either use globals or pass the vars to the functions or use oop to use properties.

The call to $call(); must be done inside your main function where you are manipulating your data. The external functions needs to be separated from your main function.


$call = 'create'. ucfirst($extension);

// Create the thumbnail
$call();


Makes no sense as it is put outside any function scope, meaning $extension isn't even declared, you need to put this on whatever function has $extension var declared.
Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

The Wizard

Hello:

This is as far as I have gotten on rewriting the script to use functions. I could use some help on how to finish it off so it will work.

Wiz




Function BannerAdd()
{
global $context, $txt, $settings;

loadTemplate('banner_add');

// File path of where the uploaded images go.
$uploaddir = $settings['default_theme_dir'] . '/images/banners/banner_images/big/';

// File path of where the uploaded images go.
$url_path = $settings['default_theme_url'] . '/images/banners/banner_images/big/';

// File path of where the Thumbnail images go
$thumbnail_dir = $settings['default_theme_dir'] . '/images/banners/banner_images/small/';

// The set width the banner needs to be.
// WARNING this will affect how your template looks. Do not change unless you fix your template code.
$max_width = '1024';

// The set height the banner needs to be.
// WARNING this will affect how your template looks.Do not change unless you fix your template code.
$max_height = '200';

// The max file size is in bites.
$max_size = '500000';

// Allowed image file extensions
$allowed_files = array(
'jpeg',
'jpg',
'png',
'gif'
);

// Name of uploaded file
$image = $_FILES['image']['name'];

// This is the temporary file created by PHP
$uploadedfile = $uploaddir . $image;

// Width and Height of thumbnail to be created
$newwidth  = 400;
$newheight = 78;

// All this code below is used to get the file name and file extension separate.
$parts = explode(".", $image);
$extension = end($parts);
$extension = strtolower($extension);
$name = str_replace("." . $extension, "", $image);

// Temporary name assigned to the uploaded file
$temp_name = $_FILES['image']['tmp_name'];

// Creates a array that has all the banner settings to use in functions
$banner_settings = array(
$uploaddir,
$url_path,
$thumbnail_dir,
$max_width,
$max_height,
$max_size,
$allowed_files,
$image,
$uploadedfile,
$newwidth,
$newheight,
$extension,
$name,
$temp_name,
);

//This variable is used as a flag.
//If the error occurs the file will not be uploaded.
$errors = array();

// When createJpg function is call it will create a thumbnail image of the banner and store it in the small dir

Function createJpg($banner_settings)
{
if ($extension == "jpeg" or $extension == "jpg")
{
// Create an Image from it so we can do the resize
$src = imagecreatefromjpeg($uploadedfile);

// Capture the original size of the uploaded image
list($width, $height) = getimagesize($uploadedfile);

//
$tmp = imagecreatetruecolor($newwidth, $newheight);

// This line actually does the image resizing, copying from the original image into the $tmp image
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Write the resized image to image/small/ Dir
$filename = $thumbnail_dir . $image;
imagejpeg($tmp, $filename, 100);

// Clean up the temp file it created when the request has completed.
imagedestroy($src);
imagedestroy($tmp);
}
}

// When createGif function is call it will create a thumbnail image of the banner and store it in the small dir

Function createGif($banner_settings)
{
// Create an Image from it so we can do the resize
$src = imagecreatefromgif($uploadedfile);

// Capture the original size of the uploaded image
list($width, $height) = getimagesize($uploadedfile);

//
$tmp = imagecreatetruecolor($newwidth, $newheight);

// This line actually does the image resizing, copying from the original image into the $tmp image
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Write the resized image to image/small/ Dir
$filename = $thumbnail_dir . $image;
imagegif($tmp, $filename);

// Clean up the temp file it created when the request has completed.
imagedestroy($src);
imagedestroy($tmp);
}

// Notes: imagepng() takes a quality value range of 0 to 9,
// while imagejpeg() takes a range of 0 to 100 and
// imagegif() doesn't take any such parameter.
//you will get a error if you try to save a PNG with a quality of 100.

// When createPng function is call it will create a thumbnail image of the banner and store it in the small dir

Function createPng($banner_settings)
{
// Create an Image from it so we can do the resize
$src = imagecreatefrompng($uploadedfile);

// Capture the original size of the uploaded image
list($width, $height) = getimagesize($uploadedfile);

//
$tmp = imagecreatetruecolor($newwidth, $newheight);

// This line actually does the image resizing, copying from the original image into the $tmp image
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Write the resized image to image/small/ Dir
$filename = $thumbnail_dir . $image;
imagepng($tmp, $filename, 9);

// Clean up the temp file it created when the request has completed.
imagedestroy($src);
imagedestroy($tmp);
}

// ===============================================================================================

if (isset($_POST['submit']))
{

// Protect the forum. Checks to make sure data is coming form the banner_add_template.php
checkSession('post');

if(empty($image) or empty($temp_name) or empty($_FILES))
fatal_lang_error('banner_file_check_error', false);

// Uploaded the image
if (move_uploaded_file($temp_name, $uploaddir . $image))
{
// 1. Check to see if image size is 1024 by 200
if (list($width, $height) = getimagesize($uploaddir . $image))
{
if ($width != $max_width && $height != $max_height)
$errors[] = $txt['banner_error_1'];
}
else
// 2. Check to see if the uploaded file is a image or not
$errors[] = $txt['banner_error_2'];

// 3. Check to see if file size is under the set max limit
if ($_FILES['image']['size'] > $max_size)
$errors[] = $txt['banner_error_3'];

// 4. Check to see if the monthly file name is spelled correct
$month = array(
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
);

if (!in_array($name, $month))
$errors[] = $txt['banner_error_4'];

// 5. Check to see if the allowed image extension is correct
if (!in_array($extension, $allowed_files))
$errors[] = $txt['banner_error_5'];
}
else
// 6. If the system is unable to upload image for any reason print Unable to upload file
$errors[] = $txt['banner_error_6'];

//Display errors and print Copy unsuccessful message, and remove image.
if (!empty($errors))
{
$context['banner_errors'] = $errors;
unlink($uploaddir . $image);
$context['banner_error_general'] = $txt['banner_error_general'];
}
else
{

// Start of thumbnail creation
$call = 'create'. ucfirst($extension);

// Create the thumbnail
$call();

// End of thumbnail creation

//If no errors registered, print the success message
echo '<meta http-equiv="refresh" content="0">';
redirectexit('action=admin;area=banner_add');
$context['banner_ok'] = $txt['banner_successful'];
}
}

// Show all Monthly Banner Images

$d = dir($thumbnail_dir);
while (false !== ($entry = $d->read()))
{
if ($entry != '.' && $entry != '..' && !is_dir($url_path . $entry))
$Images[] = $entry;
}
$d->close();

if (!empty($d) && is_object($d))
$context['banner_file_check_error'] = $txt['banner_file_check_error'];

$months_with_extension = array();

$months = array(
'january' => 1,
'february' => 2,
'march' => 3,
'april' => 4,
'may' => 5,
'june' => 6,
'july' => 7,
'august' => 8,
'september' => 9,
'october' => 10,
'november' => 11,
'december' => 12
);

foreach ($Images as $image_name)
{
$month_pieces = explode('.', strtolower($image_name));

// I want to be sure that:
//   1) the month has the correct number of dots in the name (1),
//   2) the month name is valid,
//   3) the extension is allowed.
if (count($month_pieces) == 2 && isset($months[$month_pieces[0]]) && in_array($month_pieces[1], $allowed_files))
$months_with_extension[$months[$month_pieces[0]]] = $image_name;
else
{
// This is a file that if not a month
}
}

ksort($months_with_extension);

// Output only January to June Banners
$output_one = array_intersect_key($months_with_extension, array(
'1' => 1,
'2' => 1,
'3' => 1,
'4' => 1,
'5' => 1,
'6' => 1
));

// Send January to June Banner data to template
$context['banner_show_images_one'] = $output_one;

// Output only July to December Banners
$output_two = array_intersect_key($months_with_extension, array(
'7' => 1,
'8' => 1,
'9' => 1,
'10' => 1,
'11' => 1,
'12' => 1
));

// Send July to December Banner data to template
$context['banner_show_images_two'] = $output_two;

// End Show all Monthly Banner Images

}

// End of Script

Suki

While valid, it is not recommended to have nested functions, that is, functions inside functions, all you have to do is put your functions outside your BannerAdd() function.


there is no need for this:


if ($extension == "jpeg" or $extension == "jpg")
{


or the other similar checks, if the function is executed it means the image is a jpg, this check is redundant, separating the checks into functions already does take care of checking.

You have a lot of undefined functions on your create functions, I already told you you need to globalize all your vars or pass them as parameters to your functions.

Having a separate functions means isolation, your createExtension() functions are completely separate from your main function.

Instead of passing a var that you don't even use:  function createGif($banner_settings)  pass the vars that you does use like $uploadedfile, $image and $thumbnail_dir.
Disclaimer: unless otherwise stated, all my posts are personal and does not represent any views or opinions held by Simple Machines.

The Wizard

Hello:

I have removed the nested functions, and have added required variables need into the functions, but I'm still missing something and I'm just not seeing it.

Wiz



if (!defined('SMF'))
die('Hacking attempt...');

Function BannerAdd()
{
global $context, $txt, $settings;

loadTemplate('banner_add');

// File path of where the uploaded images go.
$uploaddir = $settings['default_theme_dir'] . '/images/banners/banner_images/big/';

// File path of where the uploaded images go.
$url_path = $settings['default_theme_url'] . '/images/banners/banner_images/big/';

// File path of where the Thumbnail images go
$thumbnail_dir = $settings['default_theme_dir'] . '/images/banners/banner_images/small/';

// The set width the banner needs to be.
// WARNING this will affect how your template looks. Do not change unless you fix your template code.
$max_width = '1024';

// The set height the banner needs to be.
// WARNING this will affect how your template looks.Do not change unless you fix your template code.
$max_height = '200';

// The max file size is in bites.
$max_size = '500000';

// Allowed image file extensions
$allowed_files = array(
'jpeg',
'jpg',
'png',
'gif'
);

// Name of uploaded file
$image = $_FILES['image']['name'];

// This is the temporary file created by PHP
$uploadedfile = $uploaddir . $image;

// Width and Height of thumbnail to be created
$newwidth  = 400;
$newheight = 78;

// All this code below is used to get the file name and file extension separate.
$parts = explode(".", $image);
$extension = end($parts);
$extension = strtolower($extension);
$name = str_replace("." . $extension, "", $image);

// Temporary name assigned to the uploaded file
$temp_name = $_FILES['image']['tmp_name'];

//This variable is used as a flag.
//If the error occurs the file will not be uploaded.
$errors = array();

// ===============================================================================================

if (isset($_POST['submit']))
{

// Protect the forum. Checks to make sure data is coming form the banner_add_template.php
checkSession('post');

if(empty($image) or empty($temp_name) or empty($_FILES))
fatal_lang_error('banner_file_check_error', false);

// Uploaded the image
if (move_uploaded_file($temp_name, $uploaddir . $image))
{
// 1. Check to see if image size is 1024 by 200
if (list($width, $height) = getimagesize($uploaddir . $image))
{
if ($width != $max_width && $height != $max_height)
$errors[] = $txt['banner_error_1'];
}
else
// 2. Check to see if the uploaded file is a image or not
$errors[] = $txt['banner_error_2'];

// 3. Check to see if file size is under the set max limit
if ($_FILES['image']['size'] > $max_size)
$errors[] = $txt['banner_error_3'];

// 4. Check to see if the monthly file name is spelled correct
$month = array(
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
);

if (!in_array($name, $month))
$errors[] = $txt['banner_error_4'];

// 5. Check to see if the allowed image extension is correct
if (!in_array($extension, $allowed_files))
$errors[] = $txt['banner_error_5'];
}
else
// 6. If the system is unable to upload image for any reason print Unable to upload file
$errors[] = $txt['banner_error_6'];

//Display errors and print Copy unsuccessful message, and remove image.
if (!empty($errors))
{
$context['banner_errors'] = $errors;
unlink($uploaddir . $image);
$context['banner_error_general'] = $txt['banner_error_general'];
}
else
{

// Start of thumbnail creation
$call = 'create'. ucfirst($extension);

// Create the thumbnail
$call();

// End of thumbnail creation

//If no errors registered, print the success message
echo '<meta http-equiv="refresh" content="0">';
redirectexit('action=admin;area=banner_add');
$context['banner_ok'] = $txt['banner_successful'];
}
}

// Show all Monthly Banner Images

$d = dir($thumbnail_dir);
while (false !== ($entry = $d->read()))
{
if ($entry != '.' && $entry != '..' && !is_dir($url_path . $entry))
$Images[] = $entry;
}
$d->close();

if (!empty($d) && is_object($d))
$context['banner_file_check_error'] = $txt['banner_file_check_error'];

$months_with_extension = array();

$months = array(
'january' => 1,
'february' => 2,
'march' => 3,
'april' => 4,
'may' => 5,
'june' => 6,
'july' => 7,
'august' => 8,
'september' => 9,
'october' => 10,
'november' => 11,
'december' => 12
);

foreach ($Images as $image_name)
{
$month_pieces = explode('.', strtolower($image_name));

// I want to be sure that:
//   1) the month has the correct number of dots in the name (1),
//   2) the month name is valid,
//   3) the extension is allowed.
if (count($month_pieces) == 2 && isset($months[$month_pieces[0]]) && in_array($month_pieces[1], $allowed_files))
$months_with_extension[$months[$month_pieces[0]]] = $image_name;
else
{
// This is a file that if not a month
}
}

ksort($months_with_extension);

// Output only January to June Banners
$output_one = array_intersect_key($months_with_extension, array(
'1' => 1,
'2' => 1,
'3' => 1,
'4' => 1,
'5' => 1,
'6' => 1
));

// Send January to June Banner data to template
$context['banner_show_images_one'] = $output_one;

// Output only July to December Banners
$output_two = array_intersect_key($months_with_extension, array(
'7' => 1,
'8' => 1,
'9' => 1,
'10' => 1,
'11' => 1,
'12' => 1
));

// Send July to December Banner data to template
$context['banner_show_images_two'] = $output_two;

// End Show all Monthly Banner Images

}

// When createJpg function is call it will create a thumbnail image of the banner and store it in the small dir

Function createJpg($uploadedfile, $newwidth, $newheight, $thumbnail_dir, $image)
{

// Create an Image from it so we can do the resize
$src = imagecreatefromjpeg($uploadedfile);

// Capture the original size of the uploaded image
list($width, $height) = getimagesize($uploadedfile);

//
$tmp = imagecreatetruecolor($newwidth, $newheight);

// This line actually does the image resizing, copying from the original image into the $tmp image
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Write the resized image to image/small/ Dir
$filename = $thumbnail_dir . $image;
imagejpeg($tmp, $filename, 100);

// Clean up the temp file it created when the request has completed.
imagedestroy($src);
imagedestroy($tmp);
}

// When createGif function is call it will create a thumbnail image of the banner and store it in the small dir

Function createGif($uploadedfile, $newwidth, $newheight, $thumbnail_dir, $image)
{
// Create an Image from it so we can do the resize
$src = imagecreatefromgif($uploadedfile);

// Capture the original size of the uploaded image
list($width, $height) = getimagesize($uploadedfile);

//
$tmp = imagecreatetruecolor($newwidth, $newheight);

// This line actually does the image resizing, copying from the original image into the $tmp image
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Write the resized image to image/small/ Dir
$filename = $thumbnail_dir . $image;
imagegif($tmp, $filename);

// Clean up the temp file it created when the request has completed.
imagedestroy($src);
imagedestroy($tmp);
}

// Notes: imagepng() takes a quality value range of 0 to 9,
// while imagejpeg() takes a range of 0 to 100 and
// imagegif() doesn't take any such parameter.
//you will get a error if you try to save a PNG with a quality of 100.

// When createPng function is call it will create a thumbnail image of the banner and store it in the small dir

Function createPng($uploadedfile, $newwidth, $newheight, $thumbnail_dir, $image)
{
// Create an Image from it so we can do the resize
$src = imagecreatefrompng($uploadedfile);

// Capture the original size of the uploaded image
list($width, $height) = getimagesize($uploadedfile);

//
$tmp = imagecreatetruecolor($newwidth, $newheight);

// This line actually does the image resizing, copying from the original image into the $tmp image
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Write the resized image to image/small/ Dir
$filename = $thumbnail_dir . $image;
imagepng($tmp, $filename, 9);

// Clean up the temp file it created when the request has completed.
imagedestroy($src);
imagedestroy($tmp);
}

// End of Script

The Wizard

Hello:

The code below works! I would like to know why it's better to have used functions instead of the "if" statements I was originally using. My guess is because its quicker?

Wiz


Function BannerAdd()
{

global $context, $txt, $settings;
global $uploaddir, $url_path, $thumbnail_dir, $max_width, $max_height, $max_size,
$allowed_files, $image, $uploadedfile, $newwidth, $newheight, $extension,
$name, $temp_name, $errors;

loadTemplate('banner_add');

// File path of where the uploaded images go.
$uploaddir = $settings['default_theme_dir'] . '/images/banners/banner_images/big/';

// File path of where the uploaded images go.
$url_path = $settings['default_theme_url'] . '/images/banners/banner_images/big/';

// File path of where the Thumbnail images go
$thumbnail_dir = $settings['default_theme_dir'] . '/images/banners/banner_images/small/';

// The set width the banner needs to be.
// WARNING this will affect how your template looks. Do not change unless you fix your template code.
$max_width = '1024';

// The set height the banner needs to be.
// WARNING this will affect how your template looks.Do not change unless you fix your template code.
$max_height = '200';

// The max file size is in bites.
$max_size = '500000';

// Allowed image file extensions
$allowed_files = array(
'jpeg',
'jpg',
'png',
'gif'
);

// Name of uploaded file
$image = $_FILES['image']['name'];

// This is the temporary file created by PHP
$uploadedfile = $uploaddir . $image;

// Width and Height of thumbnail to be created
$newwidth  = 400;
$newheight = 78;

// All this code below is used to get the file name and file extension separate.
$parts = explode(".", $image);
$extension = end($parts);
$extension = strtolower($extension);
$name = str_replace("." . $extension, "", $image);

// Temporary name assigned to the uploaded file
$temp_name = $_FILES['image']['tmp_name'];

//This variable is used as a flag.
//If the error occurs the file will not be uploaded.
$errors = array();

// ===============================================================================================

if (isset($_POST['submit']))
{

// Protect the forum. Checks to make sure data is coming form the banner_add_template.php
checkSession('post');

if(empty($image) or empty($temp_name) or empty($_FILES))
fatal_lang_error('banner_file_check_error', false);

// Uploaded the image
if (move_uploaded_file($temp_name, $uploaddir . $image))
{
// 1. Check to see if image size is 1024 by 200
if (list($width, $height) = getimagesize($uploaddir . $image))
{
if ($width != $max_width && $height != $max_height)
$errors[] = $txt['banner_error_1'];
}
else
// 2. Check to see if the uploaded file is a image or not
$errors[] = $txt['banner_error_2'];

// 3. Check to see if file size is under the set max limit
if ($_FILES['image']['size'] > $max_size)
$errors[] = $txt['banner_error_3'];

// 4. Check to see if the monthly file name is spelled correct
$month = array(
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
);

if (!in_array($name, $month))
$errors[] = $txt['banner_error_4'];

// 5. Check to see if the allowed image extension is correct
if (!in_array($extension, $allowed_files))
$errors[] = $txt['banner_error_5'];
}
else
// 6. If the system is unable to upload image for any reason print Unable to upload file
$errors[] = $txt['banner_error_6'];

//Display errors and print Copy unsuccessful message, and remove image.
if (!empty($errors))
{
$context['banner_errors'] = $errors;
unlink($uploaddir . $image);
$context['banner_error_general'] = $txt['banner_error_general'];
}
else
{

// Start of thumbnail creation
$call = 'create'. ucfirst($extension);

// Create the thumbnail
$call();

// End of thumbnail creation

//If no errors registered, print the success message
echo '<meta http-equiv="refresh" content="0">';
redirectexit('action=admin;area=banner_add');
$context['banner_ok'] = $txt['banner_successful'];
}
}

// Show all Monthly Banner Images

$d = dir($thumbnail_dir);
while (false !== ($entry = $d->read()))
{
if ($entry != '.' && $entry != '..' && !is_dir($url_path . $entry))
$Images[] = $entry;
}
$d->close();

if (!empty($d) && is_object($d))
$context['banner_file_check_error'] = $txt['banner_file_check_error'];

$months_with_extension = array();

$months = array(
'january' => 1,
'february' => 2,
'march' => 3,
'april' => 4,
'may' => 5,
'june' => 6,
'july' => 7,
'august' => 8,
'september' => 9,
'october' => 10,
'november' => 11,
'december' => 12
);

foreach ($Images as $image_name)
{
$month_pieces = explode('.', strtolower($image_name));

// I want to be sure that:
//   1) the month has the correct number of dots in the name (1),
//   2) the month name is valid,
//   3) the extension is allowed.
if (count($month_pieces) == 2 && isset($months[$month_pieces[0]]) && in_array($month_pieces[1], $allowed_files))
$months_with_extension[$months[$month_pieces[0]]] = $image_name;
else
{
// This is a file that if not a month
}
}

ksort($months_with_extension);

// Output only January to June Banners
$output_one = array_intersect_key($months_with_extension, array(
'1' => 1,
'2' => 1,
'3' => 1,
'4' => 1,
'5' => 1,
'6' => 1
));

// Send January to June Banner data to template
$context['banner_show_images_one'] = $output_one;

// Output only July to December Banners
$output_two = array_intersect_key($months_with_extension, array(
'7' => 1,
'8' => 1,
'9' => 1,
'10' => 1,
'11' => 1,
'12' => 1
));

// Send July to December Banner data to template
$context['banner_show_images_two'] = $output_two;

// End Show all Monthly Banner Images

}

// When createJpg function is call it will create a thumbnail image of the banner and store it in the small dir

Function createJpg()
{
global $uploadedfile, $newwidth, $newheight, $thumbnail_dir, $image;

// Create an Image from it so we can do the resize
$src = imagecreatefromjpeg($uploadedfile);

// Capture the original size of the uploaded image
list($width, $height) = getimagesize($uploadedfile);

//
$tmp = imagecreatetruecolor($newwidth, $newheight);

// This line actually does the image resizing, copying from the original image into the $tmp image
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Write the resized image to image/small/ Dir
$filename = $thumbnail_dir . $image;
imagejpeg($tmp, $filename, 100);

// Clean up the temp file it created when the request has completed.
imagedestroy($src);
imagedestroy($tmp);
}

// When createGif function is call it will create a thumbnail image of the banner and store it in the small dir

Function createGif()
{
global $uploadedfile, $newwidth, $newheight, $thumbnail_dir, $image;

// Create an Image from it so we can do the resize
$src = imagecreatefromgif($uploadedfile);

// Capture the original size of the uploaded image
list($width, $height) = getimagesize($uploadedfile);

//
$tmp = imagecreatetruecolor($newwidth, $newheight);

// This line actually does the image resizing, copying from the original image into the $tmp image
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Write the resized image to image/small/ Dir
$filename = $thumbnail_dir . $image;
imagegif($tmp, $filename);

// Clean up the temp file it created when the request has completed.
imagedestroy($src);
imagedestroy($tmp);
}

// Notes: imagepng() takes a quality value range of 0 to 9,
// while imagejpeg() takes a range of 0 to 100 and
// imagegif() doesn't take any such parameter.
//you will get a error if you try to save a PNG with a quality of 100.

// When createPng function is call it will create a thumbnail image of the banner and store it in the small dir

Function createPng()
{
global $uploadedfile, $newwidth, $newheight, $thumbnail_dir, $image;

// Create an Image from it so we can do the resize
$src = imagecreatefrompng($uploadedfile);

// Capture the original size of the uploaded image
list($width, $height) = getimagesize($uploadedfile);

//
$tmp = imagecreatetruecolor($newwidth, $newheight);

// This line actually does the image resizing, copying from the original image into the $tmp image
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Write the resized image to image/small/ Dir
$filename = $thumbnail_dir . $image;
imagepng($tmp, $filename, 9);

// Clean up the temp file it created when the request has completed.
imagedestroy($src);
imagedestroy($tmp);
}

// End of Script

Arantor

I think you've missed the point about making things into functions as well as why we don't put things in global scope unless we need to...

It isn't about being faster. It's about breaking reusable code into functions so that you can call them without having to have more code than you need.

The Wizard

Found a mistake here is corrected version of code:


Function BannerAdd()
{

global $context, $txt, $settings;
global $uploaddir, $url_path, $thumbnail_dir, $max_width, $max_height, $max_size,
$allowed_files, $image, $uploadedfile, $newwidth, $newheight, $extension,
$name, $temp_name, $errors;

loadTemplate('banner_add');

// File path of where the uploaded images go.
$uploaddir = $settings['default_theme_dir'] . '/images/banners/banner_images/big/';

// File path of where the uploaded images go.
$url_path = $settings['default_theme_url'] . '/images/banners/banner_images/big/';

// File path of where the Thumbnail images go
$thumbnail_dir = $settings['default_theme_dir'] . '/images/banners/banner_images/small/';

// The set width the banner needs to be.
// WARNING this will affect how your template looks. Do not change unless you fix your template code.
$max_width = '1024';

// The set height the banner needs to be.
// WARNING this will affect how your template looks.Do not change unless you fix your template code.
$max_height = '200';

// The max file size is in bites.
$max_size = '500000';

// Allowed image file extensions
$allowed_files = array(
'jpeg',
'jpg',
'png',
'gif'
);

// This is the temporary file created by PHP
$uploadedfile = $uploaddir . $image;

// Width and Height of thumbnail to be created
$newwidth  = 400;
$newheight = 78;

//This variable is used as a flag.
//If the error occurs the file will not be uploaded.
$errors = array();

// ===============================================================================================

if (isset($_POST['submit']))
{

// Protect the forum. Checks to make sure data is coming form the banner_add_template.php
checkSession('post');

// Name of uploaded file
$image = $_FILES['image']['name'];

// Temporary name assigned to the uploaded file
$temp_name = $_FILES['image']['tmp_name'];

if(empty($image) or empty($temp_name) or empty($_FILES))
fatal_lang_error('banner_file_check_error', false);

// All this code below is used to get the file name and file extension separate.
$parts = explode(".", $image);
$extension = end($parts);
$extension = strtolower($extension);
$name = str_replace("." . $extension, "", $image);

// Uploaded the image
if (move_uploaded_file($temp_name, $uploaddir . $image))
{
// 1. Check to see if image size is 1024 by 200
if (list($width, $height) = getimagesize($uploaddir . $image))
{
if ($width != $max_width && $height != $max_height)
$errors[] = $txt['banner_error_1'];
}
else
// 2. Check to see if the uploaded file is a image or not
$errors[] = $txt['banner_error_2'];

// 3. Check to see if file size is under the set max limit
if ($_FILES['image']['size'] > $max_size)
$errors[] = $txt['banner_error_3'];

// 4. Check to see if the monthly file name is spelled correct
$month = array(
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
);

if (!in_array($name, $month))
$errors[] = $txt['banner_error_4'];

// 5. Check to see if the allowed image extension is correct
if (!in_array($extension, $allowed_files))
$errors[] = $txt['banner_error_5'];
}
else
// 6. If the system is unable to upload image for any reason print Unable to upload file
$errors[] = $txt['banner_error_6'];

//Display errors and print Copy unsuccessful message, and remove image.
if (!empty($errors))
{
$context['banner_errors'] = $errors;
unlink($uploaddir . $image);
$context['banner_error_general'] = $txt['banner_error_general'];
}
else
{

// Start of thumbnail creation
$call = 'create'. ucfirst($extension);

// Create the thumbnail
$call();

// End of thumbnail creation

//If no errors registered, print the success message
echo '<meta http-equiv="refresh" content="0">';
redirectexit('action=admin;area=banner_add');
$context['banner_ok'] = $txt['banner_successful'];
}
}

// Show all Monthly Banner Images

$d = dir($thumbnail_dir);
while (false !== ($entry = $d->read()))
{
if ($entry != '.' && $entry != '..' && !is_dir($url_path . $entry))
$Images[] = $entry;
}
$d->close();

if (!empty($d) && is_object($d))
$context['banner_file_check_error'] = $txt['banner_file_check_error'];

$months_with_extension = array();

$months = array(
'january' => 1,
'february' => 2,
'march' => 3,
'april' => 4,
'may' => 5,
'june' => 6,
'july' => 7,
'august' => 8,
'september' => 9,
'october' => 10,
'november' => 11,
'december' => 12
);

foreach ($Images as $image_name)
{
$month_pieces = explode('.', strtolower($image_name));

// I want to be sure that:
//   1) the month has the correct number of dots in the name (1),
//   2) the month name is valid,
//   3) the extension is allowed.
if (count($month_pieces) == 2 && isset($months[$month_pieces[0]]) && in_array($month_pieces[1], $allowed_files))
$months_with_extension[$months[$month_pieces[0]]] = $image_name;
else
{
// This is a file that if not a month
}
}

ksort($months_with_extension);

// Output only January to June Banners
$output_one = array_intersect_key($months_with_extension, array(
'1' => 1,
'2' => 1,
'3' => 1,
'4' => 1,
'5' => 1,
'6' => 1
));

// Send January to June Banner data to template
$context['banner_show_images_one'] = $output_one;

// Output only July to December Banners
$output_two = array_intersect_key($months_with_extension, array(
'7' => 1,
'8' => 1,
'9' => 1,
'10' => 1,
'11' => 1,
'12' => 1
));

// Send July to December Banner data to template
$context['banner_show_images_two'] = $output_two;

// End Show all Monthly Banner Images

}

// When createJpg function is call it will create a thumbnail image of the banner and store it in the small dir

Function createJpg()
{
global $uploadedfile, $newwidth, $newheight, $thumbnail_dir, $image;

// Create an Image from it so we can do the resize
$src = imagecreatefromjpeg($uploadedfile);

// Capture the original size of the uploaded image
list($width, $height) = getimagesize($uploadedfile);

//
$tmp = imagecreatetruecolor($newwidth, $newheight);

// This line actually does the image resizing, copying from the original image into the $tmp image
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Write the resized image to image/small/ Dir
$filename = $thumbnail_dir . $image;
imagejpeg($tmp, $filename, 100);

// Clean up the temp file it created when the request has completed.
imagedestroy($src);
imagedestroy($tmp);
}

// When createGif function is call it will create a thumbnail image of the banner and store it in the small dir

Function createGif()
{
global $uploadedfile, $newwidth, $newheight, $thumbnail_dir, $image;

// Create an Image from it so we can do the resize
$src = imagecreatefromgif($uploadedfile);

// Capture the original size of the uploaded image
list($width, $height) = getimagesize($uploadedfile);

//
$tmp = imagecreatetruecolor($newwidth, $newheight);

// This line actually does the image resizing, copying from the original image into the $tmp image
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Write the resized image to image/small/ Dir
$filename = $thumbnail_dir . $image;
imagegif($tmp, $filename);

// Clean up the temp file it created when the request has completed.
imagedestroy($src);
imagedestroy($tmp);
}

// Notes: imagepng() takes a quality value range of 0 to 9,
// while imagejpeg() takes a range of 0 to 100 and
// imagegif() doesn't take any such parameter.
//you will get a error if you try to save a PNG with a quality of 100.

// When createPng function is call it will create a thumbnail image of the banner and store it in the small dir

Function createPng()
{
global $uploadedfile, $newwidth, $newheight, $thumbnail_dir, $image;

// Create an Image from it so we can do the resize
$src = imagecreatefrompng($uploadedfile);

// Capture the original size of the uploaded image
list($width, $height) = getimagesize($uploadedfile);

//
$tmp = imagecreatetruecolor($newwidth, $newheight);

// This line actually does the image resizing, copying from the original image into the $tmp image
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Write the resized image to image/small/ Dir
$filename = $thumbnail_dir . $image;
imagepng($tmp, $filename, 9);

// Clean up the temp file it created when the request has completed.
imagedestroy($src);
imagedestroy($tmp);
}

// End of Script

The Wizard

Quote from: Arantor on April 04, 2013, 11:27:25 AM
I think you've missed the point about making things into functions as well as why we don't put things in global scope unless we need to...

It isn't about being faster. It's about breaking reusable code into functions so that you can call them without having to have more code than you need.

Makes sense -  breaking reusable code into functions so that you can call them without having to have more code than you need.

So should I have put them into the global's?

Arantor

QuoteSo should I have put them into the global's?

Um... what?

The Wizard


global $context, $txt, $settings;
global $uploaddir, $url_path, $thumbnail_dir, $max_width, $max_height, $max_size,
$allowed_files, $image, $uploadedfile, $newwidth, $newheight, $extension,
$name, $temp_name, $errors;

Arantor

I'm not sure why almost any of those are global variables.

The Wizard

I just checked the error log and there are allot of warnings/errors. It's clear to me that by using functions in the script was a very bad idea and so I'm going back to the way it was. I don't see enough gain to warrent all this grief.

Wiz

emanuele

This is how you should use functions instead of globals:

Function BannerAdd()
{
global $context, $txt, $settings;

loadTemplate('banner_add');

// File path of where the uploaded images go.
$uploaddir = $settings['default_theme_dir'] . '/images/banners/banner_images/big/';

// File path of where the uploaded images go.
$url_path = $settings['default_theme_url'] . '/images/banners/banner_images/big/';

// File path of where the Thumbnail images go
$thumbnail_dir = $settings['default_theme_dir'] . '/images/banners/banner_images/small/';

// The set width the banner needs to be.
// WARNING this will affect how your template looks. Do not change unless you fix your template code.
$max_width = '1024';

// The set height the banner needs to be.
// WARNING this will affect how your template looks.Do not change unless you fix your template code.
$max_height = '200';

// The max file size is in bites.
$max_size = '500000';

// Allowed image file extensions
$allowed_files = array(
'jpeg',
'jpg',
'png',
'gif'
);

// This is the temporary file created by PHP
$uploadedfile = $uploaddir . $image;

// Width and Height of thumbnail to be created
$newwidth  = 400;
$newheight = 78;

//This variable is used as a flag.
//If the error occurs the file will not be uploaded.
$errors = array();

// ===============================================================================================

if (isset($_POST['submit']))
{

// Protect the forum. Checks to make sure data is coming form the banner_add_template.php
checkSession('post');

// Name of uploaded file
$image = $_FILES['image']['name'];

// Temporary name assigned to the uploaded file
$temp_name = $_FILES['image']['tmp_name'];

if(empty($image) or empty($temp_name) or empty($_FILES))
fatal_lang_error('banner_file_check_error', false);

// All this code below is used to get the file name and file extension separate.
$parts = explode(".", $image);
$extension = end($parts);
$extension = strtolower($extension);
$name = str_replace("." . $extension, "", $image);

// Uploaded the image
if (move_uploaded_file($temp_name, $uploaddir . $image))
{
// 1. Check to see if image size is 1024 by 200
if (list($width, $height) = getimagesize($uploaddir . $image))
{
if ($width != $max_width && $height != $max_height)
$errors[] = $txt['banner_error_1'];
}
else
// 2. Check to see if the uploaded file is a image or not
$errors[] = $txt['banner_error_2'];

// 3. Check to see if file size is under the set max limit
if ($_FILES['image']['size'] > $max_size)
$errors[] = $txt['banner_error_3'];

// 4. Check to see if the monthly file name is spelled correct
$month = array(
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
);

if (!in_array($name, $month))
$errors[] = $txt['banner_error_4'];

// 5. Check to see if the allowed image extension is correct
if (!in_array($extension, $allowed_files))
$errors[] = $txt['banner_error_5'];
}
else
// 6. If the system is unable to upload image for any reason print Unable to upload file
$errors[] = $txt['banner_error_6'];

//Display errors and print Copy unsuccessful message, and remove image.
if (!empty($errors))
{
$context['banner_errors'] = $errors;
unlink($uploaddir . $image);
$context['banner_error_general'] = $txt['banner_error_general'];
}
else
{

// Start of thumbnail creation
$call = 'create'. ucfirst($extension);

// Create the thumbnail
$call($thumbnail_dir, $image, $uploadedfile, $newwidth, $newheight);

// End of thumbnail creation

//If no errors registered, print the success message
echo '<meta http-equiv="refresh" content="0">';
redirectexit('action=admin;area=banner_add');
$context['banner_ok'] = $txt['banner_successful'];
}
}

// Show all Monthly Banner Images

$d = dir($thumbnail_dir);
while (false !== ($entry = $d->read()))
{
if ($entry != '.' && $entry != '..' && !is_dir($url_path . $entry))
$Images[] = $entry;
}
$d->close();

if (!empty($d) && is_object($d))
$context['banner_file_check_error'] = $txt['banner_file_check_error'];

$months_with_extension = array();

$months = array(
'january' => 1,
'february' => 2,
'march' => 3,
'april' => 4,
'may' => 5,
'june' => 6,
'july' => 7,
'august' => 8,
'september' => 9,
'october' => 10,
'november' => 11,
'december' => 12
);

foreach ($Images as $image_name)
{
$month_pieces = explode('.', strtolower($image_name));

// I want to be sure that:
//   1) the month has the correct number of dots in the name (1),
//   2) the month name is valid,
//   3) the extension is allowed.
if (count($month_pieces) == 2 && isset($months[$month_pieces[0]]) && in_array($month_pieces[1], $allowed_files))
$months_with_extension[$months[$month_pieces[0]]] = $image_name;
else
{
// This is a file that if not a month
}
}

ksort($months_with_extension);

// Output only January to June Banners
$output_one = array_intersect_key($months_with_extension, array(
'1' => 1,
'2' => 1,
'3' => 1,
'4' => 1,
'5' => 1,
'6' => 1
));

// Send January to June Banner data to template
$context['banner_show_images_one'] = $output_one;

// Output only July to December Banners
$output_two = array_intersect_key($months_with_extension, array(
'7' => 1,
'8' => 1,
'9' => 1,
'10' => 1,
'11' => 1,
'12' => 1
));

// Send July to December Banner data to template
$context['banner_show_images_two'] = $output_two;

// End Show all Monthly Banner Images

}

// When createJpg function is call it will create a thumbnail image of the banner and store it in the small dir

Function createJpg($thumbnail_dir, $image, $uploadedfile, $newwidth, $newheight)
{
// Create an Image from it so we can do the resize
$src = imagecreatefromjpeg($uploadedfile);

// Capture the original size of the uploaded image
list($width, $height) = getimagesize($uploadedfile);

//
$tmp = imagecreatetruecolor($newwidth, $newheight);

// This line actually does the image resizing, copying from the original image into the $tmp image
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Write the resized image to image/small/ Dir
$filename = $thumbnail_dir . $image;
imagejpeg($tmp, $filename, 100);

// Clean up the temp file it created when the request has completed.
imagedestroy($src);
imagedestroy($tmp);
}

// When createGif function is call it will create a thumbnail image of the banner and store it in the small dir

Function createGif($thumbnail_dir, $image, $uploadedfile, $newwidth, $newheight)
{
// Create an Image from it so we can do the resize
$src = imagecreatefromgif($uploadedfile);

// Capture the original size of the uploaded image
list($width, $height) = getimagesize($uploadedfile);

//
$tmp = imagecreatetruecolor($newwidth, $newheight);

// This line actually does the image resizing, copying from the original image into the $tmp image
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Write the resized image to image/small/ Dir
$filename = $thumbnail_dir . $image;
imagegif($tmp, $filename);

// Clean up the temp file it created when the request has completed.
imagedestroy($src);
imagedestroy($tmp);
}

// Notes: imagepng() takes a quality value range of 0 to 9,
// while imagejpeg() takes a range of 0 to 100 and
// imagegif() doesn't take any such parameter.
//you will get a error if you try to save a PNG with a quality of 100.

// When createPng function is call it will create a thumbnail image of the banner and store it in the small dir

Function createPng($thumbnail_dir, $image, $uploadedfile, $newwidth, $newheight)
{
// Create an Image from it so we can do the resize
$src = imagecreatefrompng($uploadedfile);

// Capture the original size of the uploaded image
list($width, $height) = getimagesize($uploadedfile);

//
$tmp = imagecreatetruecolor($newwidth, $newheight);

// This line actually does the image resizing, copying from the original image into the $tmp image
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Write the resized image to image/small/ Dir
$filename = $thumbnail_dir . $image;
imagepng($tmp, $filename, 9);

// Clean up the temp file it created when the request has completed.
imagedestroy($src);
imagedestroy($tmp);
}

// End of Script


The idea of function is to "pass" them the informations (aka variables) then need and nothing else.
Globals are just for specific cases.


Take a peek at what I'm doing! ;D




Hai bisogno di supporto in Italiano?

Aiutateci ad aiutarvi: spiegate bene il vostro problema: no, "non funziona" non è una spiegazione!!
1) Cosa fai,
2) cosa ti aspetti,
3) cosa ottieni.

The Wizard

The code above caused this error in the error log.

Quote

http://www.tribeuniverse3.com/SMF-Mod-Test-Area/index.php?action=admin;area=banner_add;cf1a619411aa=a21a338554fef155d384a96bbf67fdb5

8: Undefined variable: image

File: /home/content/05/9199905/html/SMF-Mod-Test-Area/Sources/banner/BannerAdd.php
Line: 59

And this is line 59 -

// This is the temporary file created by PHP
$uploadedfile = $uploaddir . $image;


and for some reason the first thumbnail image in each column is now a black box.

Attached is the latest version of the mod.


Arantor

That would appear to be because $image is not passed into the functions but it's hard to actually read the code because of the indentation being off.

emanuele

In the file you attached, at line 59 I have a comment:
//If the error occurs the file will not be uploaded


Take a peek at what I'm doing! ;D




Hai bisogno di supporto in Italiano?

Aiutateci ad aiutarvi: spiegate bene il vostro problema: no, "non funziona" non è una spiegazione!!
1) Cosa fai,
2) cosa ti aspetti,
3) cosa ottieni.

The Wizard

Hello All:

Please forget about everything in this thread up till this post = fresh start on the problem.

below is the code I would like to talk about -



Function BannerAdd()
{
global $context, $txt, $settings;

loadTemplate('banner_add');

show_Banners();
}

Function settings_Banners()
{
global $settings;

// File path of where the Thumbnail images go
$thumbnail_dir = $settings['default_theme_dir'] . '/images/banners/banner_images/small/';

// Allowed image file extensions
$allowed_files = array(
'jpeg',
'jpg',
'png',
'gif'
);
}

Function show_Banners()
{
global $context, $txt, $settings;

settings_Banners();

$handle = opendir($thumbnail_dir);
while($entry = readdir($handle))
{
if($entry != '.' && $entry != '..')
{
$Images[] = $entry;
}
}
closedir($handle);

if (!empty($handle) && is_object($handle))
$context['banner_file_check_error'] = $txt['banner_file_check_error'];

$months_with_extension = array();

$months = array(
'january' => 1,
'february' => 2,
'march' => 3,
'april' => 4,
'may' => 5,
'june' => 6,
'july' => 7,
'august' => 8,
'september' => 9,
'october' => 10,
'november' => 11,
'december' => 12
);

foreach ($Images as $image_name)
{
$month_pieces = explode('.', strtolower($image_name));

// I want to be sure that:
//   1) the month has the correct number of dots in the name (1),
//   2) the month name is valid,
//   3) the extension is allowed.
if (count($month_pieces) == 2 && isset($months[$month_pieces[0]]) && in_array($month_pieces[1], $allowed_files))
$months_with_extension[$months[$month_pieces[0]]] = $image_name;
else
{
// This is a file that if not a month
}
}

ksort($months_with_extension);

// Output only January to June Banners
$output_one = array_intersect_key($months_with_extension, array(
'1' => 1,
'2' => 1,
'3' => 1,
'4' => 1,
'5' => 1,
'6' => 1
));

// Send January to June Banner data to template
$context['banner_show_images_one'] = $output_one;

// Output only July to December Banners
$output_two = array_intersect_key($months_with_extension, array(
'7' => 1,
'8' => 1,
'9' => 1,
'10' => 1,
'11' => 1,
'12' => 1
));

// Send July to December Banner data to template
$context['banner_show_images_two'] = $output_two;

} // End Function


The code above does not work, and I need to understand why this is.
If I take the contents of "Function settings_Banners()" and add them to "show_Banners()" the script will work fine.
So can someone explane in terms I will understand.

Thanks

Wiz


emanuele

I assume that this is *all* the code that has to "work" together.
Sorry if I write something obvious to you, but I'm not exactly sure where to start, so I started from the very beginning. :)

What a "function" is?
Function (in programming) is a way to group code and execute it when needed "calling" the function.
Let's try with some examples.
<?php
$variable_1 
0;

if (
$variable_1 == 0)
{
    echo 
'The variable is 0';
}
else
{
    echo 
'The variable is not 0';
}

No functions. Just plain code.
If you put it into a file and execute it you will get a message.

<?php

function my_func_1()
{
    
$variable_1 0;

    if (
$variable_1 == 0)
    {
        echo 
'The variable is 0';
    }
    else
    {
        echo 
'The variable is not 0';
    }
}

Similar to the code above, but everything is "included" in a function. If you put that code into a file and execute it you will not get any message. Nothing. Nada. White page.
Why that? Because the function is declared, but not called.
How do you "call" a function?
With something like this:
my_func_1();

So, again the code above:
<?php

function my_func_1()
{
    
$variable_1 0;

    if (
$variable_1 == 0)
    {
        echo 
'The variable is 0';
    }
    else
    {
        echo 
'The variable is not 0';
    }
}

my_func_1();

If you put it into a file and execute it, you will get again the message. Why?
See the last line, you have called the function. So php goes through the script, finds the "call" to the function and executes the code into the function.

Why is organize code in functions a Good Thing?
Because you can re-use that code without having to write everything again, you just need to "call" the function as many times as you want.
<?php

function my_func_1()
{
    
$variable_1 0;

    if (
$variable_1 == 0)
    {
        echo 
'The variable is 0';
    }
    else
    {
        echo 
'The variable is not 0';
    }
}

my_func_1();
my_func_1();
my_func_1();
my_func_1();
my_func_1();

Running the above code you will see the message repeated 5 times.



Now, why the code you posted doesn't work as you want?
Let's have a look at the first function:
Function BannerAdd()
{
global $context, $txt, $settings;

loadTemplate('banner_add');

show_Banners();
}

BannerAdd is executed because you instructed SMF to do so somewhere in some hook (not important at the moment.
when BannerAdd is executed, what does it happen?
  • First line some variables are put in "global" (not important now).
  • Third line the SMF internal function loadTemplate is "called" (executed), that function "loads" your template file banner_add.template.php (not important now).
  • Fifth line the function show_Banners is executed! <== This *is* important now.

    What do you have in show_Banners?
    Some code.

    So, the question is: why do you need settings_Banners?



    Variables scope

    Each variable exists in a certain place.
    Usually each variable exists only in the function it is initialized. For example
    <?php
    function my_func1()
    {
        
    $var_1 0;
    }

    echo 
    $var_1;

    This code doesn't give you anything (unless you set error_reporting to E_ALL, then it will show a "var_1 does not exist" or something similar).
    Why?
    Because $var_1 exists only inside my_func_1.

    <?php
    function my_func_1()
    {
        
    $var_1 0;
    }

    my_func_1();

    echo 
    $var_1;

    What if we call my_func_1?
    Nothing, because the variable still exists only inside my_func_1. So, how can we find the value of $var_1? Depends. There are many ways, depending on what we want to achieve.

    <?php
    function my_func_1()
    {
        
    $var_1 0;
        echo 
    $var_1;
    }

    my_func_1();

    That works. Now we have moved the echo inside the function.
    But it may not be what we want.

    <?php
    function my_func_1()
    {
        
    $var_1 0;
        return 
    $var_1;
    }

    $var_2 my_func_1();
    echo 
    $var_2;

    That's another way: from inside my_func_1 we "return" the value of $var_1 and we assign it to $var_2 and then we "echo" $var_2.

    <?php
    global $var_1;

    function 
    my_func_1()
    {
        global 
    $var_1;
        
    $var_1 0;
    }

    my_func_1();

    echo 
    $var_1;

    And that's another way: we instruct php to put the variable $var_1 in the "global" scope, we call the function my_func_1, that changes the value of $var_1 and then we "echo" its value.

    This last method should be used *only* if there is a real need to do so.


Take a peek at what I'm doing! ;D




Hai bisogno di supporto in Italiano?

Aiutateci ad aiutarvi: spiegate bene il vostro problema: no, "non funziona" non è una spiegazione!!
1) Cosa fai,
2) cosa ti aspetti,
3) cosa ottieni.

The Wizard

Hello:

Thank you for that very insightful explanation it helped allot.

I'm trying to figure a direction, and I have I idea on what way to go, but I would like to hear opinions on this direction -

What if I created a MasterSettings.php page with all the settings for the entire mod, and used require_once($sourcedir . '/MasterSettings.php'); in the functions?

Wiz


Arantor

What benefit would it give you? What hassle would it save you? Why do you need to add another file when by definition you're already adding a file as it is?

The Wizard

Quote from: Arantor on April 05, 2013, 10:49:42 AM
What benefit would it give you? What hassle would it save you? Why do you need to add another file when by definition you're already adding a file as it is?

benefit - There are several places in my code where I use the same variable for example - Allowed image file extensions. This way if I wanted to add or remove a extension it would only be one change. Instead of changing all the places the variable would have to be put in the functions. Plus I would use less code as I would not have to wright all the variables more then once. Combined with the functions I could consolidate a few of the php pages.

Wiz

Arantor

*shrug* I don't understand the logic by which you arrive at the conclusions you do about how any of this stuff works.

emanuele

I'm not sure how you have organized your code and if you want to use the same code across multiple mods (I wouldn't do that unless in very particular strange cases) or what, so you should elaborate what I'm going to say according to your needs.

You can have a single function that "validates" the image, for example:

function wiz_is_valid_image($image)
{
$extension = substr($image, stripos($image, '.') + 1);

// Allowed image file extensions
$allowed_files = array(
'jpeg',
'jpg',
'png',
'gif'
);

return in_array($extension, $allowed_files);
}


This function should return true if the extension is "valid" and false if not, so you could use it like that:

if (!wiz_is_valid_image($uploaded_image_name))
fatal_lang_error('not_a_valid_image', false);


I'm not sure if this is what you are asking, though it may resemble.


Take a peek at what I'm doing! ;D




Hai bisogno di supporto in Italiano?

Aiutateci ad aiutarvi: spiegate bene il vostro problema: no, "non funziona" non è una spiegazione!!
1) Cosa fai,
2) cosa ti aspetti,
3) cosa ottieni.

The Wizard

Hello:

I finally have gotten the hang of functions and I think you will be pleased with the direction I decided to go in.
Below is my latest version of the code. It does show the banners, and it does upload the big banners, However it does not create the thumbnails. Could you please look over this new code and help me find the problem. I expect it's a variable I forgot to include.

Wiz


Function BannerAdd()
{
global $context, $txt, $settings;

loadTemplate('banner_add');

// File path of where the uploaded images go.
$uploaddir = $settings['default_theme_dir'] . '/images/banners/banner_images/big/';

// File path of where the uploaded images go.
$url_path = $settings['default_theme_url'] . '/images/banners/banner_images/big/';

// File path of where the Thumbnail images go
$thumbnail_dir = $settings['default_theme_dir'] . '/images/banners/banner_images/small/';

// The set width the banner needs to be.
// WARNING this will affect how your template looks. Do not change unless you fix your template code.
$max_width = '1024';

// The set height the banner needs to be.
// WARNING this will affect how your template looks.Do not change unless you fix your template code.
$max_height = '200';

// The max file size is in bites.
$max_size = '500000';

// Allowed image file extensions
$allowed_files = array(
'jpeg',
'jpg',
'png',
'gif'
);

// Width and Height of thumbnail to be created
$newwidth  = 400;
$newheight = 78;

// Call the function show_banners
show_banners($thumbnail_dir, $allowed_files);

// Call the function upload_banners
upload_banners($uploaddir, $max_width, $max_height, $max_size, $allowed_files);

}

// When show_banners function is call it will show all the banners stored in the big dir by showing a thumbnail version.

Function show_banners($thumbnail_dir, $allowed_files)
{
global $context, $txt, $settings;

$handle = opendir($thumbnail_dir);
while($entry = readdir($handle))
{
if($entry != '.' && $entry != '..')
{
$Images[] = $entry;
}
}
closedir($handle);

if (!empty($handle) && is_object($handle))
$context['banner_file_check_error'] = $txt['banner_file_check_error'];

$months_with_extension = array();

$months = array(
'january' => 1,
'february' => 2,
'march' => 3,
'april' => 4,
'may' => 5,
'june' => 6,
'july' => 7,
'august' => 8,
'september' => 9,
'october' => 10,
'november' => 11,
'december' => 12
);

foreach ($Images as $image_name)
{
$month_pieces = explode('.', strtolower($image_name));

// I want to be sure that:
//   1) the month has the correct number of dots in the name (1),
//   2) the month name is valid,
//   3) the extension is allowed.
if (count($month_pieces) == 2 && isset($months[$month_pieces[0]]) && in_array($month_pieces[1], $allowed_files))
$months_with_extension[$months[$month_pieces[0]]] = $image_name;
else
{
// This is a file that if not a month
}
}

ksort($months_with_extension);

// Output only January to June Banners
$output_one = array_intersect_key($months_with_extension, array(
'1' => 1,
'2' => 1,
'3' => 1,
'4' => 1,
'5' => 1,
'6' => 1
));

// Send January to June Banner data to template
$context['banner_show_images_one'] = $output_one;

// Output only July to December Banners
$output_two = array_intersect_key($months_with_extension, array(
'7' => 1,
'8' => 1,
'9' => 1,
'10' => 1,
'11' => 1,
'12' => 1
));

// Send July to December Banner data to template
$context['banner_show_images_two'] = $output_two;
}

// When upload_banners function is call it will upload the banner and store it in the big dir.

Function upload_banners($uploaddir, $max_width, $max_height, $max_size, $allowed_files)
{
global $context, $txt, $settings;

//This variable is used as a flag.
//If the error occurs the file will not be uploaded.
$errors = array();

if (isset($_POST['submit']))
{

// Protect the forum. Checks to make sure data is coming form the banner_add_template.php
checkSession('post');

// Name of uploaded file
$image = $_FILES['image']['name'];

// Temporary name assigned to the uploaded file
$temp_name = $_FILES['image']['tmp_name'];

if(empty($image) or empty($temp_name) or empty($_FILES))
fatal_lang_error('banner_file_check_error', false);

// All this code below is used to get the file name and file extension separate.
$parts = explode(".", $image);
$extension = end($parts);
$extension = strtolower($extension);
$name = str_replace("." . $extension, "", $image);

// Uploaded the image
if (move_uploaded_file($temp_name, $uploaddir . $image))
{
// 1. Check to see if image size is 1024 by 200
if (list($width, $height) = getimagesize($uploaddir . $image))
{
if ($width != $max_width && $height != $max_height)
$errors[] = $txt['banner_error_1'];
}
else
// 2. Check to see if the uploaded file is a image or not
$errors[] = $txt['banner_error_2'];

// 3. Check to see if file size is under the set max limit
if ($_FILES['image']['size'] > $max_size)
$errors[] = $txt['banner_error_3'];

// 4. Check to see if the monthly file name is spelled correct
$month = array(
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
);

if (!in_array($name, $month))
$errors[] = $txt['banner_error_4'];

// 5. Check to see if the allowed image extension is correct
if (!in_array($extension, $allowed_files))
$errors[] = $txt['banner_error_5'];
}
else
// 6. If the system is unable to upload image for any reason print Unable to upload file
$errors[] = $txt['banner_error_6'];

//Display errors and print Copy unsuccessful message, and remove image.
if (!empty($errors))
{
// Start of thumbnail creation

// This is the temporary file created by PHP
$uploadedfile = $uploaddir . $image;

$call = 'create'. ucfirst($extension);

// Create the thumbnail
$call($thumbnail_dir, $image, $uploadedfile, $newwidth, $newheight, $uploaddir);

// End of thumbnail creation

$context['banner_errors'] = $errors;
unlink($uploaddir . $image);
$context['banner_error_general'] = $txt['banner_error_general'];
}
}
}

// Notes: imagepng() takes a quality value range of 0 to 9,
// while imagejpeg() takes a range of 0 to 100 and
// imagegif() doesn't take any such parameter.
//you will get a error if you try to save a PNG with a quality of 100.


// When createJpeg function is call it will create a thumbnail image of the banner and store it in the small dir.

Function createJpeg($uploaddir, $newwidth, $newheight, $thumbnail_dir, $image)
{
// This is the temporary file created by PHP
$uploadedfile = $uploaddir . $image;

// Create an Image from it so we can do the resize
$src = imagecreatefromjpeg($uploadedfile);

// Capture the original size of the uploaded image
list($width, $height) = getimagesize($uploadedfile);

//
$tmp = imagecreatetruecolor($newwidth, $newheight);

// This line actually does the image resizing, copying from the original image into the $tmp image
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Write the resized image to image/small/ Dir
$filename = $thumbnail_dir . $image;
imagejpeg($tmp, $filename, 100);

// Clean up the temp file it created when the request has completed.
imagedestroy($src);
imagedestroy($tmp);
}


// When createJpg function is call it will create a thumbnail image of the banner and store it in the small dir.

Function createJpg($uploaddir, $newwidth, $newheight, $thumbnail_dir, $image)
{
// This is the temporary file created by PHP
$uploadedfile = $uploaddir . $image;

// Create an Image from it so we can do the resize
$src = imagecreatefromjpeg($uploadedfile);

// Capture the original size of the uploaded image
list($width, $height) = getimagesize($uploadedfile);

//
$tmp = imagecreatetruecolor($newwidth, $newheight);

// This line actually does the image resizing, copying from the original image into the $tmp image
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Write the resized image to image/small/ Dir
$filename = $thumbnail_dir . $image;
imagejpeg($tmp, $filename, 100);

// Clean up the temp file it created when the request has completed.
imagedestroy($src);
imagedestroy($tmp);
}


// When createGif function is call it will create a thumbnail image of the banner and store it in the small dir.

Function createGif($uploaddir, $newwidth, $newheight, $thumbnail_dir, $image)
{
// This is the temporary file created by PHP
$uploadedfile = $uploaddir . $image;

// Create an Image from it so we can do the resize
$src = imagecreatefromgif($uploadedfile);

// Capture the original size of the uploaded image
list($width, $height) = getimagesize($uploadedfile);

//
$tmp = imagecreatetruecolor($newwidth, $newheight);

// This line actually does the image resizing, copying from the original image into the $tmp image
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Write the resized image to image/small/ Dir
$filename = $thumbnail_dir . $image;
imagegif($tmp, $filename);

// Clean up the temp file it created when the request has completed.
imagedestroy($src);
imagedestroy($tmp);
}

// When createPng function is call it will create a thumbnail image of the banner and store it in the small dir.

Function createPng($uploaddir, $newwidth, $newheight, $thumbnail_dir, $image)
{
// This is the temporary file created by PHP
$uploadedfile = $uploaddir . $image;

// Create an Image from it so we can do the resize
$src = imagecreatefrompng($uploadedfile);

// Capture the original size of the uploaded image
list($width, $height) = getimagesize($uploadedfile);

//
$tmp = imagecreatetruecolor($newwidth, $newheight);

// This line actually does the image resizing, copying from the original image into the $tmp image
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Write the resized image to image/small/ Dir
$filename = $thumbnail_dir . $image;
imagepng($tmp, $filename, 9);

// Clean up the temp file it created when the request has completed.
imagedestroy($src);
imagedestroy($tmp);
}

// End of Script

The Wizard

Quote from: Arantor on April 05, 2013, 01:22:01 PM
*shrug* I don't understand the logic by which you arrive at the conclusions you do about how any of this stuff works.

Logic is a figment of my imagination.

emanuele

Could it be because of that piece of code:
//Display errors and print Copy unsuccessful message, and remove image.
if (!empty($errors))
{
// Start of thumbnail creation

// This is the temporary file created by PHP
$uploadedfile = $uploaddir . $image;

$call = 'create'. ucfirst($extension);

// Create the thumbnail
$call($thumbnail_dir, $image, $uploadedfile, $newwidth, $newheight, $uploaddir);

// End of thumbnail creation

$context['banner_errors'] = $errors;
unlink($uploaddir . $image);
$context['banner_error_general'] = $txt['banner_error_general'];
}

Apparently you are creating the image only if there are errors... (!empty($errors)).

Also, indentation: when you open a curly bracket indent, when you close it "unindent", e.g.
function my_func()
{ //<= curly bracket opened, indent next line
TAB-> // code goes here indented
if (some_condition)
{ // <= curly bracket opened, indent next line
TAB-> // code goes here
// code again
} // <= curly bracket closed, removed 1 indentation
// some other code here
} // <= curly bracket closed, removed 1 indentation

Hope that helps.


Take a peek at what I'm doing! ;D




Hai bisogno di supporto in Italiano?

Aiutateci ad aiutarvi: spiegate bene il vostro problema: no, "non funziona" non è una spiegazione!!
1) Cosa fai,
2) cosa ti aspetti,
3) cosa ottieni.

The Wizard

Hello:

Below is the latest version of my script. Everything works and I believe the indentation is correct.
Is this code acceptable now?

Wiz



Function BannerAdd()
{
loadTemplate('banner_add');

// Call the function show_banners
show_banners();

// Call the function upload_banners
upload_banners();
}

// When show_banners function is call it will show all the banners stored in the big dir by showing a thumbnail version.


Function show_banners()
{
global $context, $txt, $settings;

// File path of where the Thumbnail images go
$thumbnail_dir = $settings['default_theme_dir'] . '/images/banners/banner_images/small/';

// Allowed image file extensions
$allowed_files = array(
'jpeg',
'jpg',
'png',
'gif'
);

$handle = opendir($thumbnail_dir);
while($entry = readdir($handle))
{
if($entry != '.' && $entry != '..')
{
$Images[] = $entry;
}
}
closedir($handle);

if (!empty($handle) && is_object($handle))
$context['banner_file_check_error'] = $txt['banner_file_check_error'];

$months_with_extension = array();

$months = array(
'january' => 1,
'february' => 2,
'march' => 3,
'april' => 4,
'may' => 5,
'june' => 6,
'july' => 7,
'august' => 8,
'september' => 9,
'october' => 10,
'november' => 11,
'december' => 12
);

foreach ($Images as $image_name)
{
$month_pieces = explode('.', strtolower($image_name));

// I want to be sure that:
//   1) the month has the correct number of dots in the name (1),
//   2) the month name is valid,
//   3) the extension is allowed.
if (count($month_pieces) == 2 && isset($months[$month_pieces[0]]) && in_array($month_pieces[1], $allowed_files))
$months_with_extension[$months[$month_pieces[0]]] = $image_name;
else
{
// This is a file that if not a month
}
}

ksort($months_with_extension);

// Output only January to June Banners
$output_one = array_intersect_key($months_with_extension, array(
'1' => 1,
'2' => 1,
'3' => 1,
'4' => 1,
'5' => 1,
'6' => 1
));

// Send January to June Banner data to template
$context['banner_show_images_one'] = $output_one;

// Output only July to December Banners
$output_two = array_intersect_key($months_with_extension, array(
'7' => 1,
'8' => 1,
'9' => 1,
'10' => 1,
'11' => 1,
'12' => 1
));

// Send July to December Banner data to template
$context['banner_show_images_two'] = $output_two;
}


// When show_banners function is call it will show all the banners stored in the big dir by showing a thumbnail version.


Function upload_banners()
{
global $context, $txt, $settings;

// File path of where the uploaded images go.
$uploaddir = $settings['default_theme_dir'] . '/images/banners/banner_images/big/';

// The set width the banner needs to be.
// WARNING this will affect how your template looks. Do not change unless you fix your template code.
$max_width = '1024';

// The set height the banner needs to be.
// WARNING this will affect how your template looks.Do not change unless you fix your template code.
$max_height = '200';

// The max file size is in bites.
$max_size = '500000';

// Allowed image file extensions
$allowed_files = array(
'jpeg',
'jpg',
'png',
'gif'
);

//This variable is used as a flag.
//If the error occurs the file will not be uploaded.
$errors = array();

if (isset($_POST['submit']))
{

// Protect the forum. Checks to make sure data is coming form the banner_add_template.php
checkSession('post');

// Name of uploaded file
$image = $_FILES['image']['name'];

// Temporary name assigned to the uploaded file
$temp_name = $_FILES['image']['tmp_name'];

if(empty($image) or empty($temp_name) or empty($_FILES))
fatal_lang_error('banner_file_check_error', false);

// All this code below is used to get the file name and file extension separate.
$parts = explode(".", $image);
$extension = end($parts);
$extension = strtolower($extension);
$name = str_replace("." . $extension, "", $image);

// Uploaded the image
if (move_uploaded_file($temp_name, $uploaddir . $image))
{
// 1. Check to see if image size is 1024 by 200
if (list($width, $height) = getimagesize($uploaddir . $image))
{
if ($width != $max_width && $height != $max_height)
$errors[] = $txt['banner_error_1'];
}
else
// 2. Check to see if the uploaded file is a image or not
$errors[] = $txt['banner_error_2'];

// 3. Check to see if file size is under the set max limit
if ($_FILES['image']['size'] > $max_size)
$errors[] = $txt['banner_error_3'];

// 4. Check to see if the monthly file name is spelled correct
$month = array(
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
);

if (!in_array($name, $month))
$errors[] = $txt['banner_error_4'];

// 5. Check to see if the allowed image extension is correct
if (!in_array($extension, $allowed_files))
$errors[] = $txt['banner_error_5'];
}
else
// 6. If the system is unable to upload image for any reason print Unable to upload file
$errors[] = $txt['banner_error_6'];

//Display errors and print Copy unsuccessful message, and remove image.
if (!empty($errors))
{
$context['banner_errors'] = $errors;
unlink($uploaddir . $image);
$context['banner_error_general'] = $txt['banner_error_general'];
}
else
{
// Start of thumbnail creation
$call = 'create'. ucfirst($extension);

// Create the thumbnail
$call();

// End of thumbnail creation

//If no errors registered, print the success message
echo '<meta http-equiv="refresh" content="0">';
redirectexit('action=admin;area=banner_add');
$context['banner_ok'] = $txt['banner_successful'];
}
}
} // End Function upload_banners


// When createJpeg function is call it will create a thumbnail image of the banner and store it in the small dir


Function createJpeg()
{
global $settings;

// File path of where the uploaded images go.
$uploaddir = $settings['default_theme_dir'] . '/images/banners/banner_images/big/';

// File path of where the Thumbnail images go
$thumbnail_dir = $settings['default_theme_dir'] . '/images/banners/banner_images/small/';

// Name of uploaded file
$image = $_FILES['image']['name'];

// This is the temporary file created by PHP
$uploadedfile = $uploaddir . $image;

// Width and Height of thumbnail to be created
$newwidth  = 400;
$newheight = 78;

// Create an Image from it so we can do the resize
$src = imagecreatefromjpeg($uploadedfile);

// Capture the original size of the uploaded image
list($width, $height) = getimagesize($uploadedfile);

//
$tmp = imagecreatetruecolor($newwidth, $newheight);

// This line actually does the image resizing, copying from the original image into the $tmp image
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Write the resized image to image/small/ Dir
$filename = $thumbnail_dir . $image;
imagejpeg($tmp, $filename, 100);

// Clean up the temp file it created when the request has completed.
imagedestroy($src);
imagedestroy($tmp);
}


// When createJpg function is call it will create a thumbnail image of the banner and store it in the small dir


Function createJpg()
{
global $settings;

// File path of where the uploaded images go.
$uploaddir = $settings['default_theme_dir'] . '/images/banners/banner_images/big/';

// File path of where the Thumbnail images go
$thumbnail_dir = $settings['default_theme_dir'] . '/images/banners/banner_images/small/';

// Name of uploaded file
$image = $_FILES['image']['name'];

// This is the temporary file created by PHP
$uploadedfile = $uploaddir . $image;

// Width and Height of thumbnail to be created
$newwidth  = 400;
$newheight = 78;

// Create an Image from it so we can do the resize
$src = imagecreatefromjpeg($uploadedfile);

// Capture the original size of the uploaded image
list($width, $height) = getimagesize($uploadedfile);

//
$tmp = imagecreatetruecolor($newwidth, $newheight);

// This line actually does the image resizing, copying from the original image into the $tmp image
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Write the resized image to image/small/ Dir
$filename = $thumbnail_dir . $image;
imagejpeg($tmp, $filename, 100);

// Clean up the temp file it created when the request has completed.
imagedestroy($src);
imagedestroy($tmp);
}


// When createGif function is call it will create a thumbnail image of the banner and store it in the small dir


Function createGif()
{
global $settings;

// File path of where the uploaded images go.
$uploaddir = $settings['default_theme_dir'] . '/images/banners/banner_images/big/';

// File path of where the Thumbnail images go
$thumbnail_dir = $settings['default_theme_dir'] . '/images/banners/banner_images/small/';

// Name of uploaded file
$image = $_FILES['image']['name'];

// This is the temporary file created by PHP
$uploadedfile = $uploaddir . $image;

// Width and Height of thumbnail to be created
$newwidth  = 400;
$newheight = 78;

// Create an Image from it so we can do the resize
$src = imagecreatefromgif($uploadedfile);

// Capture the original size of the uploaded image
list($width, $height) = getimagesize($uploadedfile);

//
$tmp = imagecreatetruecolor($newwidth, $newheight);

// This line actually does the image resizing, copying from the original image into the $tmp image
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Write the resized image to image/small/ Dir
$filename = $thumbnail_dir . $image;
imagegif($tmp, $filename);

// Clean up the temp file it created when the request has completed.
imagedestroy($src);
imagedestroy($tmp);
}


// When createPng function is call it will create a thumbnail image of the banner and store it in the small dir


Function createPng()
{
global $settings;

// File path of where the uploaded images go.
$uploaddir = $settings['default_theme_dir'] . '/images/banners/banner_images/big/';

// File path of where the Thumbnail images go
$thumbnail_dir = $settings['default_theme_dir'] . '/images/banners/banner_images/small/';

// Name of uploaded file
$image = $_FILES['image']['name'];

// This is the temporary file created by PHP
$uploadedfile = $uploaddir . $image;

// Width and Height of thumbnail to be created
$newwidth  = 400;
$newheight = 78;

// Create an Image from it so we can do the resize
$src = imagecreatefrompng($uploadedfile);

// Capture the original size of the uploaded image
list($width, $height) = getimagesize($uploadedfile);

//
$tmp = imagecreatetruecolor($newwidth, $newheight);

// This line actually does the image resizing, copying from the original image into the $tmp image
imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Write the resized image to image/small/ Dir
$filename = $thumbnail_dir . $image;
imagepng($tmp, $filename, 9);

// Clean up the temp file it created when the request has completed.
imagedestroy($src);
imagedestroy($tmp);
}

// End of Script

emanuele

The indentation looks much better now! :D

I don't know the code itself, but if it does what you expect from it, then it works! :P ;)


Take a peek at what I'm doing! ;D




Hai bisogno di supporto in Italiano?

Aiutateci ad aiutarvi: spiegate bene il vostro problema: no, "non funziona" non è una spiegazione!!
1) Cosa fai,
2) cosa ti aspetti,
3) cosa ottieni.

Advertisement: