News:

Want to get involved in developing SMF? Why not lend a hand on our GitHub!

Main Menu

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...
Holder of controversial views, all of which my own.


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();

All Colours Sam

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.
Oh, wouldn't it be great if I *was* crazy? ...then the world would be okay
Suki

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

All Colours Sam

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.
Oh, wouldn't it be great if I *was* crazy? ...then the world would be okay
Suki

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.
Holder of controversial views, all of which my own.


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?
Holder of controversial views, all of which my own.


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.
Holder of controversial views, all of which my own.


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.
Holder of controversial views, all of which my own.


Advertisement: