Help fixing lightbox code

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
The problem is in the image resizer PHP page; it's throwing the following errors instead of an image:

Strict Standards: Only variables should be passed by reference in /home/enforcer/public_html/enforcer/libraries/thumb.display.php on line 205

Warning: Cannot modify header information - headers already sent by (output started at /home/enforcer/public_html/enforcer/libraries/thumb.display.php:205) in /home/enforcer/public_html/enforcer/libraries/thumb.display.php on line 221

Warning: imagejpeg(): Filename cannot be empty in /home/enforcer/public_html/enforcer/libraries/thumb.display.php on line 222
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
I can't see the code, just the results of the code. The first error you can get rid of by setting the error reporting levels ("Strict standards" means that that part of the code is stylistically and idiomatically wrong and may break altogether in a future PHP version, but it's working for now); but the other two mean that the code is actually broken (the first warning means that output has begun before a header() call is made; the second warning is a less-subtle error). You would need to post the code if you want help.
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
I'd say it's /home/enforcer/public_html/enforcer/libraries/thumb.display.php, going by the error messages I posted above. (And I despair somewhat that you needed to ask. That doesn't bode well for any follow-on problems that may arise.)
 

ben reilly tribute

New Member
Messages
23
Reaction score
0
Points
1
I'm sure i'll be able to handle it.
PHP:
<?php

 /**
  * THUMBS DISPLAY
  */

    //// import init file ////
    require_once("general.bootstrap.php");
   
    error_reporting(E_ALL);
   
    //// debug mode
    $th_debug_flag = false;//$settings['thumbnail_debug'];
   
    $error_msg = '';
    $center_image = true;
   
    $img_types = array(
        "",
        "GIF",
        "JPG",
        "PNG",
        "SWF",
        "PSD",
        "BMP",
        "TIFF",
        "TIFF",
        "JPC",
        "JP2",
        "JPX",
        "JB2",
        "SWC",
        "IFF",
        "WBMP",
        "XBM"
    );
   
    // debug
    $th_debug = (isset($settings['thumbnail_debug']))
        ? $settings['thumbnail_debug']
        : false;
   
    // check if image path is encoded
    $en = (isset($_GET["en"]))
        ? true
        : false;
   
    //// receives the original image as [?img=relative/path/to/image.jpg] ////
    // get image path
    $img = false;
    if (isset($_GET["img"])) {
        if ($settings['set_double_encoding']) {
            $img = "../".Url_decode(htmlentities($_GET["img"]));
        } else {
            $img = "../".str_replace("\\","",htmlentities($_GET["img"]));
        }
    } else {
        $img = false;
    }
   
    // check if big image to cache
    $pic = (isset($_GET["pic"]))
        ? true
        : false;
   
    $_prefix = ($pic)
        ? $settings['image_prefix']
        : $settings['thumbnail_prefix'];
   
    // image is square?
    $sq = (isset($_GET["sq"]))
        ? htmlentities($_GET["sq"])
        : false;
   
    // image is preview thumbnail
    $pr = (isset($_GET["pr"]))
        ? htmlentities($_GET["pr"])
        : false;
   
    // image max size
    $img_maxsize = (isset($_GET["max"])) ? htmlentities($_GET["max"]) : 80;
   
    // image quality
    $img_quality = (isset($_GET["q"]))
        ? htmlentities($_GET["q"])
        : (($sq)
            ? (($pr)
                ? $settings['preview_thumbnail_quality']
                : $settings['thumbnail_quality'])
            : $settings['thumbnail_quality']);
   
    // cache thumbnail
    $cache_thumb = (isset($_GET["c"]))
        ? true
        : false;
   
    // delete existing thumbnail
    $unlink = (isset($_GET["u"]))
        ? true
        : false;
   
    $thumb = '';   
    $image = '';
   
    if ($img && file_exists($img)) {
       
        //// get image size ////
        $img_info = getimagesize($img);
       
        if ($img_info) {
           
            //// resize image ////
            $img_type = $img_types[$img_info[2]];
            $th_w = $img_info[0];
            $th_h = $img_info[1];
            $move_w = $move_h = 0;
            $w = $h = 0;
           
            if ($th_w >= $th_h) {
               
                //// Landscape Picture ////
                if ($sq) {
                    $h = $img_maxsize;
                    $w = (($th_w * $h) / $th_h);
                    $move_w = (($th_w - $th_h) / 2);
                    $w = $img_maxsize;
                    $th_w = $th_h;
                } else {
                    $w = $img_maxsize;
                    $h = (($th_h * $w) / $th_w);
                }
               
            } else {
               
                //// Portrait Picture ////
                if ($sq) {
                    $w = $img_maxsize;
                    $h = (($th_h * $w) / $th_w);
                    $move_h = (($th_h - $th_w) / 2);
                    $h = $img_maxsize;
                    $th_h = $th_w;
                } else {
                    $h = $img_maxsize;
                    $w = (($th_w * $h) / $th_h);
                }
            }
           
            //// create image ////
            $thumb = imagecreatetruecolor($w, $h);
            imagefill($thumb, 255, 255, 255);
           
            //// copy image ////
            if (($img_type == "JPG") && (imagetypes() & IMG_JPG)) {
                $image = imagecreatefromjpeg($img);
               
            } else if (($img_type == "GIF") && (imagetypes() & IMG_GIF)) {
                $image = imagecreatefromgif($img);
               
            } else if (($img_type == "PNG") && (imagetypes() & IMG_PNG)) {
                $image = imagecreatefrompng($img);
               
            }
        } else {
            $error_msg = "!! BAD IMG";
        }
    }
   
    //// if there's an error reading the original image
    //// output an error image
    // see if it failed
    if (!$th_debug_flag && (!$image | $image=='' | $error_msg!='')) {
       
        // do not cache
        $cache_thumb = '';
        $error_size = $img_maxsize;
        // create a white image
        $thumb  = imagecreatetruecolor($error_size, $error_size);
        $lightgrey = imagecolorallocate($thumb, 234, 234, 234);
        $grey = imagecolorallocate($thumb, 66, 66, 66);
        $black  = imagecolorallocate($thumb, 0, 0, 0);
        $white  = imagecolorallocate($thumb, 255, 255, 255);
        $orange  = imagecolorallocate($thumb, 255, 66, 0);
        $bg_color = $grey;
        $fg_color = $white;
        imagefilledrectangle($thumb, 0, 0, $error_size, $error_size, $bg_color);
           
        // output an errmsg
        $fnum = ($img_maxsize >= 70) ? 2 : 1;
        $msg_height = 12;
        $msg_array = explode(":",$error_msg);
        for ($i=0; $i<count($msg_array); $i++) {
            imagestring($thumb, $fnum, 2, 2+($msg_height*$i), $msg_array[$i], $fg_color);
        }
   
        /// up the image quality
        $img_quality = 100;
       
    } else {
        $created = imagecopyresampled($thumb, $image, 0, 0, $move_w, $move_h, $w, $h, $th_w, $th_h);
       
    }
   
    //// cache images (if enabled)
    //$current_gallery = array_pop(split("/", dirname($img)));
    $current_gallery = array_pop(explode("/", dirname($img)));
    $cache_thumb_dir = "../cache/"
        .$settings['gallery_prefix']
        .$current_gallery;
       
    $thumb_url = $cache_thumb_dir."/"
        .$_prefix
        .basename($img);
       
    if ($unlink) $delete = @unlink($thumb_url);
   
    if ($cache_thumb) {
        $thumbCreated = imagejpeg($thumb, $thumb_url, $img_quality);
    }
   
    //// display created image ////
    header("Content-type: image/jpeg");
    imagejpeg($thumb, '', $img_quality);
   
    //// destroy images (free memory)
    imagedestroy($image);
    imagedestroy($thumb);
   
    /* END */
?>
 
Last edited by a moderator:

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
Okay. The "headers already sent" is happening because there is whitespace before the opening <?php tag. That is already echoed to the browser as a literal string, including (at least) the line break with the default MIME type for a PHP page (which is likely text/html), so you cannot later try to change the headers/MIME type for the response.

The "Fileneame cannot be empty" is happening because the call to imagejpg() looks like this:

PHP:
   imagejpeg($thumb, '', $img_quality);

... with the filename parameter set to an empty string. The "Strict standards" warning is because you are using dirname($img) as an argument to the explode() function instead of setting a variable to the result of dirname($img) and feeding that to explode. Again, that's a stylistic matter, which you can safely suppress by setting the warning levels on line 10 (or 11, depending on how you count it) to:

PHP:
    error_reporting(E_ALL ^ E_STRICT);

(XOR-ing out E_STRICT from E_ALL will report all errors except the "strict standards" errors.)
 

Skizzerz

Contributors
Staff member
Contributors
Messages
2,928
Reaction score
118
Points
63
@essellar: The headers already sent was from array_pop throwing a warning due to being passed the return value of explode() instead of a variable containing an array, the whitespace above the <?php was a mistake I made when editing in [php] around the code so it was actually readable, fixed that up now.
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
That still leaves the other two.
 

ben reilly tribute

New Member
Messages
23
Reaction score
0
Points
1
Thanks for the thorough review... I'm not sure what changes I need to make outside of adding

PHP:
error_reporting(E_ALL ^ E_STRICT);

to line 10. I have added this code to the live site.

Will this address the problem or simply help figure out exactly what the issue is? I appreciate your thorough response and feedback, however I will require guidance on what exactly you'd like for me to do in order to proceed.
 

essellar

Community Advocate
Community Support
Messages
3,295
Reaction score
227
Points
63
You'll need to give the image a filename instead of the empty string in the line I noted above as well.
 

ben reilly tribute

New Member
Messages
23
Reaction score
0
Points
1
Should I be renaming the files to some sort of specific naming convention then? I think the string is empty because it's variable...?

It seems that the functionality has now gotten worse, with thumbnails in the top carousel no longer loading.
 

ben reilly tribute

New Member
Messages
23
Reaction score
0
Points
1
Thanks gomarc.

This didn't fix it. Not sure if I mentioned... this site all worked perfectly for a couple years and only recently started to have issues.
 
Top