News:

Join the Facebook Fan Page.

Main Menu

Function problem

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

Previous topic - Next topic

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: