News:

Want to get involved in developing SMF, then why not lend a hand on our github!

Main Menu

template / Image Uploader problem

Started by The Wizard, December 12, 2012, 01:28:36 PM

Previous topic - Next topic

The Wizard

Hello:

The following is a 1st rough draft so please be kind. I'm working on a image up-loader that uploads the big image and saves it in the big file and then creates a thumb nail image and saves it in the small file. I have created the form to upload the image and put it in my template design. Now here is the part I'm having trouble with - every time I try uploading a image the error messages do not show up in my template the show up in the upper left hand corner.

I welcome all suggestions on how to solve my problem and make this code better.

Thanks

Wiz





wizard_add_item_step1.template.php 


function template_main ()
{    

global $context, $txt, $scripturl, $boardurl, $modSettings;

// Load the SMFShop language strings
loadLanguage('Shop');

   echo '
    <center>
      <table width="919" border="1" height="498">
        <tbody>
          <tr align="center">
<td colspan="3">
<img src="', $boardurl, '/Sources/shop/Wizards_Gallery/buttons/Wizards-Gallery.png" alt="" /><br />
<img src="', $boardurl, '/Sources/shop/Wizards_Gallery/buttons/Add-A-Item.png" alt="" /><br />
            </td>
          </tr>
          <tr align="center">
            <td>
<img src="', $boardurl, '/Sources/shop/Wizards_Gallery/buttons/Step1.png" alt="" /><br />
            </td>
            <td>                                                               
                               
                    <form name="newad" method="post" enctype="multipart/form-data"  action="">
                        <table>
                        <tr><td><input type="file" name="image"></td></tr>
                        <tr><td><input name="Submit" type="submit" value="Upload image">
                        </td></tr> </table>
                    </form> 


            </td>
            <td>
<img src="', $boardurl, '/Sources/shop/Wizards_Gallery/buttons/wizard2.gif" alt="" /><br />
            </td>
          </tr>
        </tbody>
      </table>
  ';

}


Subs-Wizards_Add_Item.php 


Function wizard_add_item ()
{
global $smcFunc, $boardurl, $context;

loadTemplate('wizard_add_item_step1');

$upload_dir = "shop/Wizards_Gallery/big/";

$enable_thumbnails = 1 ;

define ("MAX_SIZE","1048576");

function getExtension($str) {
        $i = strrpos($str,".");
        if (!$i) { return ""; }
$l = strlen($str) - $i;
        $ext = substr($str,$i+1,$l);
return $ext;
}

$errors=0;

if(isset($_POST['Submit'])) {
        $image=$_FILES['image']['name'];

if ($image) {
    $filename = stripslashes($_FILES['image']['name']);
    $extension = getExtension($filename);
    $extension = strtolower($extension);

if (($extension != "png")) {
    echo '<h1>You used a Image File extension that is not a png. Unable to proceed.</h1>';
$errors=1;
$enable_thumbnails = 0 ;

} else {

$size=filesize($_FILES['image']['tmp_name']);

if ($size > MAX_SIZE*1048576){
        echo '<h1>You have exceeded the size limit!</h1>';
    $errors=1;
}

$newname= $upload_dir.$image;

$copied = copy($_FILES['image']['tmp_name'], $newname);

if (!$copied) {
        echo '<h1>Copy unsuccessfull!</h1>';
    $errors=1;
}}}}

// Thumbnail Creator

$img_rname = $image;

if(isset($_POST['Submit']) && !$errors) {

function make_thumbnails($updir, $img){

        $thumbnail_width = 45;
$thumbnail_height = 45;
$thumb_preword = "-small";

$arr_image_details = GetImageSize("$updir"."$img");
$original_width = $arr_image_details[0];
$original_height = $arr_image_details[1];

if( $original_width > $original_height ){
$new_width = $thumbnail_width;
$new_height = intval($original_height*$new_width/$original_width);
} else {
$new_height = $thumbnail_height;
$new_width = intval($original_width*$new_height/$original_height);
}

$dest_x = intval(($thumbnail_width - $new_width) / 2);
$dest_y = intval(($thumbnail_height - $new_height) / 2);

if($arr_image_details[2]==3) { $imgt = "ImagePNG"; $imgcreatefrom = "ImageCreateFromPNG";  }

if( $imgt ) {
$old_image = $imgcreatefrom("$updir"."$img");
$new_image = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
imageCopyResized($new_image,$old_image,$dest_x,
$dest_y,0,0,$new_width,$new_height,$original_width,$original_height);

// Sends created thumbnail to small file - change direction here!
$imgt($new_image,"shop/Wizards_Gallery/small/"."$img");
}

}
// End of Thumbnail Creator

if($enable_thumbnails = 1) make_thumbnails($upload_dir, $img_rname);
$feedback .= "thumbnail created $img_rname<br />";

if(isset($_POST['Submit']) && !$errors) {
        echo '<img src="http://www.tribeuniverse3.com/test7/small/' . $img_rname . '">';
        echo '<p><h1>'.$feedback.'</p>File Uploaded Successfully! Try again!</h1>';
}
}

} // End of Function wizard bracket

The Wizard


emanuele

Well, you have the answer in front of you. ;)
You are using "echo" in the "Subs" file, and you should not.
Store the error message somewhere in $context and then display it in the template.


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

@emanuele -

below is a image of the problem I'm having a issue with. Storing the error message in the language file was to be my next step on this draft. What I don't understand, or comprehend is how to display the error message in the box. Would you be so kind as to give me a example?

Thanks

Wiz



Hj Ahmad Rasyid Hj Ismail

Shouldn't this be in the template to achieve that?

if(isset($_POST['Submit']) && !$errors) {
        echo '<img src="http://www.tribeuniverse3.com/test7/small/' . $img_rname . '">';
        echo '<p><h1>'.$feedback.'</p>File Uploaded Successfully! Try again!</h1>';

The Wizard

So it's OK to have "if statements" in the template? Now I get it.

Thanks

Wiz

Hj Ahmad Rasyid Hj Ismail

They're all around in default curve template files. So I don't see why it cannot be there...

The Wizard

Now that I think about it your right. I'm still trying to learn all the rules.

Again thanks

Wiz

Hj Ahmad Rasyid Hj Ismail

No problem. I am also just another learner like you. So, please don't mind me if I made mistakes.

The Wizard

Hello:

I'm having trouble getting $context to work. Right now all the error messages show up even if I have not uploaded anything.
Below is a example of the code I'm working on. Can someone point out my mistake?

Thanks

Wiz


Subs-Wizards_Add_Item.php
 

$context['upload_success'] = 1;



wizard_add_item_step1.template.php


if ($context['upload_success'] = 1) {
        echo '<img src="http://www.tribeuniverse3.com/Sources/shop/Wizards_Gallery/small/' . $img_rname . '">';
        echo '<p>'.$feedback.'</p>File Uploaded Successfully!';
    }

emanuele

if ($context['upload_success'] = 1) {
should be at least:
if ($context['upload_success'] == 1) {
Using 1 equal means assign, using two means compare. ;)
In your code you are assigning 1 to $context['upload_success'] in the if that is then always true.

Though in SMF coding style, it would probably be even better:
if (!empty($context['upload_success'])) {


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

OK below is what I have so far, but it does not work - if you try and upload a image it does not get saved. I'm guessing it has something to do with the paths, but I'm not getting anywhere. Ideas? Also feel free to point out any mistakes I made, but not this is just a 2nd rough draft.

Thanks

Wiz


Subs-Wizards_Add_Item.php


Function wizard_add_item ()
{
global $smcFunc, $boardurl, $context;

loadTemplate('wizard_add_item_step1');


$upload_dir = "/Sources/shop/Wizards_Gallery/big/";

    $enable_thumbnails = 1 ;

    define ("MAX_SIZE","1048576");

function getExtension($str) {
        $i = strrpos($str,".");

    if (!$i) { return ""; }
    $l = strlen($str) - $i;
        $ext = substr($str,$i+1,$l);
return $ext;
}

    $errors=0;

    if(isset($_POST['Submit'])) {
            $image=$_FILES['image']['name'];

if ($image) {
    $filename = stripslashes($_FILES['image']['name']);
    $extension = getExtension($filename);
    $extension = strtolower($extension);

if (($extension != "png")) {
        $context['wrong_file_ext'] = 3;
    $errors=1;
    $enable_thumbnails = 0 ;

} else {

    $size=filesize($_FILES['image']['tmp_name']);

    if ($size > MAX_SIZE*1048576){
        $errors=1;
    $context['max_size_limit'] = 2;
}

    $newname= $upload_dir.$image;

    if(isset($_POST['Submit']) && !$errors) {

        $copied = copy($_FILES['image']['tmp_name'], $newname);

    } else {

    $errors=1;
}}}}

// Thumbnail Creator

    $img_rname = $image;

    if(isset($_POST['Submit']) && !$errors) {

function make_thumbnails($updir, $img){

    $thumbnail_width = 45;
$thumbnail_height = 45;
$thumb_preword = "-small";

$arr_image_details = GetImageSize("$updir"."$img");
$original_width = $arr_image_details[0];
$original_height = $arr_image_details[1];

if( $original_width > $original_height ){
$new_width = $thumbnail_width;
$new_height = intval($original_height*$new_width/$original_width);

} else {

$new_height = $thumbnail_height;
$new_width = intval($original_width*$new_height/$original_height);
}

$dest_x = intval(($thumbnail_width - $new_width) / 2);
$dest_y = intval(($thumbnail_height - $new_height) / 2);

if($arr_image_details[2]==3) { $imgt = "ImagePNG"; $imgcreatefrom = "ImageCreateFromPNG";  }

if( $imgt ) {
$old_image = $imgcreatefrom("$updir"."$img");
$new_image = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
imageCopyResized($new_image,$old_image,$dest_x,
$dest_y,0,0,$new_width,$new_height,$original_width,$original_height);

// Sends created thumbnail to small file - change direction here!
$imgt($new_image, "/Sources/shop/Wizards_Gallery/small/"."$img");

$context['upload_success'] = 1;
}

}
// End of Thumbnail Creator

    if($enable_thumbnails = 1) make_thumbnails($upload_dir, $img_rname);
$feedback .= "thumbnail created $img_rname<br />";

    $context['upload_unsuccessfull'] = $enable_thumbnails;

}   

} // End of Function wizard bracket



wizard_add_item_step1.template.php


function template_main ()
{    

global $context, $txt, $scripturl, $boardurl, $modSettings;

// Load the SMFShop language strings
loadLanguage('Shop');

  echo '
    <center>
      <table width="919" border="1" height="498">
        <tbody>
          <tr align="center">
<td colspan="3">
<img src="', $boardurl, '/Sources/shop/Wizards_Gallery/buttons/Wizards-Gallery.png" alt="" /><br />
<img src="', $boardurl, '/Sources/shop/Wizards_Gallery/buttons/Add-A-Item.png" alt="" /><br />
            </td>
          </tr>
          <tr align="center">
            <td>
<img src="', $boardurl, '/Sources/shop/Wizards_Gallery/buttons/Step1.png" alt="" /><br />
            </td>
            <td>                                                               
                               
                    <form name="newad" method="post" enctype="multipart/form-data"  action="">
                        <table>
                        <tr><td><input type="file" name="image"></td></tr>
                        <tr><td><input name="Submit" type="submit" value="Upload image">
                        </td></tr> </table>
                    </form>';

if ($context['upload_success'] == 1) {
        echo '<img src="http://www.tribeuniverse3.com/Sources/shop/Wizards_Gallery/small/' . $img_rname . '">';
        echo '<p>'.$feedback.'</p>File Uploaded Successfully!';
    }

    if ($context['wrong_file_ext'] == 3) {
    echo 'You used a Image File extension that is not a png.<br />Unable to proceed.';
}

    if ($context['max_size_limit'] == 2) {
        echo 'You have exceeded the size limit!<br />Copy unsuccessfull!';
    }

    if ($context['upload_unsuccessfull'] == 0) {
        echo 'Copy unsuccessfull!';
    }

   echo'
            </td>
            <td>
<img src="', $boardurl, '/Sources/shop/Wizards_Gallery/buttons/wizard2.gif" alt="" /><br />
            </td>
          </tr>
        </tbody>
      </table>
  ';

}

emanuele

$upload_dir = "/Sources/shop/Wizards_Gallery/big/";

Put in global $sourcedir, then replace the above line with:
$upload_dir = $sourcedir . "/shop/Wizards_Gallery/big/";


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

I tryed the code below as the created thumbnail path, but it does not seem to work. Any ideas?


// Sends created thumbnail to small file - change direction here!
$imgt($new_image, $sourcedir . "/shop/Wizards_Gallery/small/" . "$img");

emanuele

Did you pu $sourcedir in in the global list at the beginning of the function?
Does the directory exists?
Is it writable?
Is the code executed at all?


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

QuoteDid you put $sourcedir in in the global list at the beginning of the function?

hits head against desk.....

forgot to add -


function make_thumbnails($updir, $img){

    global $smcFunc, $sourcedir;


*********************************
I started running a few test runs and the big image is stored and the the thumb nail is created, but the message - "File Uploaded Successfully" does not show up any idea why? The "You used a Image File extension that is not a png. Unable to proceed." shows up when I try and upload a gif so I know the basics work.

Below is my latest draft -

Subs-Wizards_Add_Item.php 


Function wizard_add_item ()
{
global $smcFunc, $boardurl, $context, $sourcedir;

loadTemplate('wizard_add_item_step1');

    $upload_dir = $sourcedir . "/shop/Wizards_Gallery/big/";

    $enable_thumbnails = 1 ;

    define ("MAX_SIZE","1048576");

function getExtension($str) {
        $i = strrpos($str,".");

    if (!$i) { return ""; }
    $l = strlen($str) - $i;
        $ext = substr($str,$i+1,$l);
return $ext;
}

    $errors=0;

    if(isset($_POST['Submit'])) {
            $image=$_FILES['image']['name'];

if ($image) {
    $filename = stripslashes($_FILES['image']['name']);
    $extension = getExtension($filename);
    $extension = strtolower($extension);

if (($extension != "png")) {
        $context['wrong_file_ext'] = 3;
    $errors=1;
    $enable_thumbnails = 0 ;

} else {

    $size=filesize($_FILES['image']['tmp_name']);

    if ($size > MAX_SIZE*1048576){
        $errors=1;
    $context['max_size_limit'] = 2;
}

    $newname= $upload_dir.$image;

    if(isset($_POST['Submit']) && !$errors) {

        $copied = copy($_FILES['image']['tmp_name'], $newname);

    } else {

    $errors=1;
}}}}

// Thumbnail Creator

    $img_rname = $image;

    if(isset($_POST['Submit']) && !$errors) {

function make_thumbnails($updir, $img){

    global $smcFunc, $sourcedir;

    $thumbnail_width = 45;
$thumbnail_height = 45;
$thumb_preword = "-small";

$arr_image_details = GetImageSize("$updir"."$img");
$original_width = $arr_image_details[0];
$original_height = $arr_image_details[1];

if( $original_width > $original_height ){
$new_width = $thumbnail_width;
$new_height = intval($original_height*$new_width/$original_width);

} else {

$new_height = $thumbnail_height;
$new_width = intval($original_width*$new_height/$original_height);
}

$dest_x = intval(($thumbnail_width - $new_width) / 2);
$dest_y = intval(($thumbnail_height - $new_height) / 2);

if($arr_image_details[2]==3) { $imgt = "ImagePNG"; $imgcreatefrom = "ImageCreateFromPNG";  }

if( $imgt ) {
$old_image = $imgcreatefrom("$updir"."$img");
$new_image = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
imageCopyResized($new_image,$old_image,$dest_x,
$dest_y,0,0,$new_width,$new_height,$original_width,$original_height);

// Sends created thumbnail to small file - change direction here!
$imgt($new_image, $sourcedir . "/shop/Wizards_Gallery/small/" . "$img");

$context['upload_success'] = 1;
}

}
// End of Thumbnail Creator

    if($enable_thumbnails = 1) make_thumbnails($upload_dir, $img_rname);
$feedback .= "thumbnail created $img_rname<br />";

    $context['upload_unsuccessfull'] = $enable_thumbnails;

}   

} // End of Function wizard bracket


wizard_add_item_step1.template.php   


function template_main ()
{    

global $context, $txt, $scripturl, $boardurl, $modSettings;

// Load the SMFShop language strings
loadLanguage('Shop');

  echo '
    <center>
      <table width="919" border="1" height="498">
        <tbody>
          <tr align="center">
<td colspan="3">
<img src="', $boardurl, '/Sources/shop/Wizards_Gallery/buttons/Wizards-Gallery.png" alt="" /><br />
<img src="', $boardurl, '/Sources/shop/Wizards_Gallery/buttons/Add-A-Item.png" alt="" /><br />
            </td>
          </tr>
          <tr align="center">
            <td>
<img src="', $boardurl, '/Sources/shop/Wizards_Gallery/buttons/Step1.png" alt="" /><br />
            </td>
            <td>                                                               
                               
                    <form name="newad" method="post" enctype="multipart/form-data"  action="">
                        <table>
                        <tr><td><input type="file" name="image"></td></tr>
                        <tr><td><input name="Submit" type="submit" value="Upload image">
                        </td></tr> </table>
                    </form>';

if ($context['upload_success'] == 1) {
        echo '<img src="http://www.tribeuniverse3.com/Sources/shop/Wizards_Gallery/small/' . $img_rname . '">';
        echo '<p>'.$feedback.'</p>File Uploaded Successfully!';
    }

    if ($context['wrong_file_ext'] == 3) {
    echo 'You used a Image File extension that is not a png.<br />Unable to proceed.';
}

    if ($context['max_size_limit'] == 2) {
        echo 'You have exceeded the size limit!<br />Copy unsuccessfull!';
    }

   echo'
            </td>
            <td>
<img src="', $boardurl, '/Sources/shop/Wizards_Gallery/buttons/wizard2.gif" alt="" /><br />
            </td>
          </tr>
        </tbody>
      </table>
  ';

}


Hj Ahmad Rasyid Hj Ismail

Quote from: emanuele on December 14, 2012, 10:52:25 AM
if ($context['upload_success'] = 1) {
should be at least:
if ($context['upload_success'] == 1) {
Using 1 equal means assign, using two means compare. ;)
In your code you are assigning 1 to $context['upload_success'] in the if that is then always true.

Though in SMF coding style, it would probably be even better:
if (!empty($context['upload_success'])) {

Advertisement: