NineChime forum

Furry stuff, oekaki stuff, and other stuff.

You are not logged in.

#1 09-04-2008 06:23:18

rainbow
Member

Experimental: Displaying total size of images in kilobytes.

I have one more question to ask and I need a bit of help here, but I'm almost done...

The other day while I was tinkering around with the ldelpics.php, delpics.php file and the recover.php file, I found a interesting variable that displays the size of each image in kilobytes:

Code:

$size = filesize($p_pic.$main_row['PIC_ID'].'.'.$main_row['ptype']);
$size = round(($size / 1024), 1);

However, I ran into problems when I attempted to calculate the total size of the images in kilobytes in the Local Picture Database (ldelpics.php), Global Picture Database (delpics.php) and the Picture Recovery Database (recover.php).

When I inserted the code that attempts to calculate the total size of the images with this snippet sample from the ldelpics.php file:

Code:

<div style="text-align: right; width: 50%; float: left;">
    <? $x = 0; ?>
    <? while ($x < $pictures) { ?>
    <? $x++; ?>
        <? $totalsize = filesize($cfg['op_pics'].'/'.$main_row['PIC_ID'].'.'.$main_row['ptype']);?>
    <? } ?>
    <? $totalsize = round(($totalsize / 1024), 1); ?>
            <? if ($pictures > 1) { ?>
    <?=$pictures ?> images (<?=$totalsize ?> KB)
            <? } elseif ($pictures = 1) { ?>
    1 image (<?=$totalsize ?> KB)
<? } ?>

The output on the size of images read as "0.1 KB" and not "0.7 KB"

And as for this snippet from the delpics.php file:

Code:

<? $x = 0; ?>
        <? while ($x < $pictures) { ?>
        <? $x++; ?>
    <? $totalsize = filesize($cfg['op_pics'].'/'.$rows['PIC_ID'].'.'.$rows['ptype']);?>
        <? } ?>
    <? $totalsize = number_format($totalsize / 1024); ?>
        <? if ($pictures > 1) { ?>
    <?=$pictures ?> images (<?=$totalsize ?> KB)
        <? } elseif ($pictures = 1) { ?>
        1 image (<?=$totalsize ?> KB)
    <? } ?>

The output on size of the two images read as 0 KB and not 1.3 KB even though I got the total size the pictures in the database to read as 1 KB.

Wac, how can I get the total size of the images (in kilobytes) to be properly displayed in from the Local Picture Database, the Global Picture Database and the Picture Recovery section?

I've looked up on the PHP commands in other websites to calculate the total size of the images in a proper manner, but it isn't much of a help.

Offline

#2 09-05-2008 03:48:04

Waccoon
Administrator

Re: Experimental: Displaying total size of images in kilobytes.

OK, you've got a bunch of problems.  You have no SQL query or iterator to get each picture's filename (PIC_ID and ptype).  You are using the assignment operator (=) when you need the additive operator (+=), which will add to the value, instead of replacing it.  You are assigning a value to $pictures, instead of testing for equality with "==".  Also, this code may cause an error if the picture is corrupt or missing, and using the at symbol (@filename()) is a hack to get around this without using too much additional code.

Here is a functional (though not perfect) way of doing it:

Code:

<?php
    // Get total space used by a single member's pictures
    $size_sql = "SELECT PIC_ID, ptype FROM {$OekakiPoteto_Prefix}oekakidta WHERE usrname='$OekakiU' AND postlock='1'";
    $size_result = mysql_query ($size_sql);
    $pictures = intval (mysql_num_rows ($size_result));

    $totalsize = 0;
    for ($x = 0; $x < $pictures; $x++) {
        $size_row = mysql_fetch_array ($size_result);
        $totalsize += @filesize ($p_pic.$size_row['PIC_ID'].'.'.$size_row['ptype']);
    }
    $totalsize = round (($totalsize / 1024), 1);

?>
<div style="text-align: right; width: 50%; float: left;">
    <?php
    if ($pictures != 1) {
        echo $pictures.' images ('.$totalsize.' KB)';
    } elseif ($pictures == 1) {
        echo '1 image ('.$totalsize.' KB)';
    }
    ?>
    (more HTML here)
</div>

With this code in place, you should have no trouble getting number_format() in there properly.

Now, you do have to have an SQL statement in there to get the appropriate pictures, and you cannot copy the SQL from the top of each PHP file, because those database calls use the "LIMIT" clause.

For ldelpics, use this:
"SELECT PIC_ID, ptype FROM {$OekakiPoteto_Prefix}oekakidta WHERE usrname='$OekakiU' AND postlock='1'"

...for delpics:
"SELECT PIC_ID, ptype FROM {$OekakiPoteto_Prefix}oekakidta WHERE postlock='1'"

...for recover:
"SELECT PIC_ID, ptype FROM {$OekakiPoteto_Prefix}oekakidta where postlock='0' {$where_clause}"

Offline

#3 09-05-2008 05:44:40

rainbow
Member

Re: Experimental: Displaying total size of images in kilobytes.

Waccoon wrote:

OK, you've got a bunch of problems.  You have no SQL query or iterator to get each picture's filename (PIC_ID and ptype).  You are using the assignment operator (=) when you need the additive operator (=+), which will add to the value, instead of replacing it.  You are assigning a value to $pictures, instead of testing for equality with "==".  Also, this code may cause an error if the picture is corrupt or missing, and using the at symbol (@filename()) is a hack to get around this without using too much additional code.

Here is a functional (though not perfect) way of doing it:

Code:

<?php
    // Get total space used by a single member's pictures
    $size_sql = "SELECT PIC_ID, ptype FROM {$OekakiPoteto_Prefix}oekakidta WHERE usrname='$OekakiU' AND postlock='1'";
    $size_result = mysql_query ($size_sql);
    $pictures = intval (mysql_num_rows ($size_result));

    $totalsize = 0;
    for ($x = 0; $x < $pictures; $x++) {
        $size_row = mysql_fetch_array ($size_result);
        $totalsize =+ @filesize ($p_pic.$size_row['PIC_ID'].'.'.$size_row['ptype']);
    }
    $totalsize = round (($totalsize / 1024), 1);

?>
<div style="text-align: right; width: 50%; float: left;">
    <?php
    if ($pictures != 1) {
        echo $pictures.' images ('.$totalsize.' KB)';
    } elseif ($pictures == 1) {
        echo '1 image ('.$totalsize.' KB)';
    }
    ?>
    (more HTML here)
</div>

With this code in place, you should have no trouble getting number_format() in there properly.

Now, you do have to have an SQL statement in there to get the appropriate pictures, and you cannot copy the SQL from the top of each PHP file, because those database calls use the "LIMIT" clause.

For ldelpics, use this:
"SELECT PIC_ID, ptype FROM {$OekakiPoteto_Prefix}oekakidta WHERE usrname='$OekakiU' AND postlock='1'"

...for delpics:
"SELECT PIC_ID, ptype FROM {$OekakiPoteto_Prefix}oekakidta WHERE postlock='1'"

...for recover:
"SELECT PIC_ID, ptype FROM {$OekakiPoteto_Prefix}oekakidta where postlock='0' {$where_clause}"

Thanks for the tip. However as from the ldelpics file, it's only getting the size of the recent image, which is 0.3 KB and not calculating the total space in which it is 1 KB as there are two drawings in the Local Picture Database. As for the @filename tag, how would I use it without causing any sort of error if the picture is missing or corrupt?

Offline

#4 09-06-2008 20:27:59

Waccoon
Administrator

Re: Experimental: Displaying total size of images in kilobytes.

Argh.  I got the accumulator backwards.  It should be $totalsize += @filesize.

As for the @filename tag, how would I use it without causing any sort of error if the picture is missing or corrupt?

You need to make sure the file exists before you test it, and make sure filesize() always returns a number (not FALSE).

Code:

    $totalsize = 0;
    for ($x = 0; $x < $pictures; $x++) {
        $size_row = mysql_fetch_array ($size_result);

        $my_file = $p_pic.$size_row['PIC_ID'].'.'.$size_row['ptype'];
        if (file_exists ($my_file)) {
            $totalsize += intval (filesize ($my_file));
        } else {
            $pictures--;
        }
    }
    $totalsize = round (($totalsize / 1024), 1);

Offline

#5 09-06-2008 21:17:06

rainbow
Member

Re: Experimental: Displaying total size of images in kilobytes.

Waccoon wrote:

Argh.  I got the accumulator backwards.  It should be $totalsize += @filesize.

As for the @filename tag, how would I use it without causing any sort of error if the picture is missing or corrupt?

You need to make sure the file exists before you test it, and make sure filesize() always returns a number (not FALSE).

Code:

    $totalsize = 0;
    for ($x = 0; $x < $pictures; $x++) {
        $size_row = mysql_fetch_array ($size_result);

        $my_file = $p_pic.$size_row['PIC_ID'].'.'.$size_row['ptype'];
        if (file_exists ($my_file)) {
            $totalsize += intval (filesize ($my_file));
        } else {
            $pictures--;
        }
    }
    $totalsize = round (($totalsize / 1024), 1);

Thank you very much for the correction, Wac. The total image sizes are displaying correct values now. smile

Offline

Board footer

Yep, still running PunBB
© Copyright 2002–2008 PunBB