1
votes

Im facing a very annoying problem in PHP when Im trying to watermark the full size images on my website. The watermark was working fine for weeks but all the sudden it started showing this error : the image "bla bla" cannot be displayed because it contains errors

I checked everything and everything seems to be working just fine! I'm stuck and I dont know what to do.

Here is my watermarking php file:

require_once "maincore.php";

if (isset($_GET['photo_id']) && isnum($_GET['photo_id'])) {

    $result = dbquery("SELECT td.*, tc.* FROM ".DB_PRODUCTS." td LEFT JOIN ".DB_PRODUCT_CATS." tc ON td.product_cat=tc.cat_id WHERE product_id='".$_GET['photo_id']."'");

    $data = dbarray($result);

        define("PHOTODIR", BASEDIR."downloads/images/");

        $parts = explode(".", $data['product_image']);

        $wm_file1 = $parts[0]."_w1.".$parts[1];

        $wm_file2 = $parts[0]."_w2.".$parts[1];

        if (!isset($_GET['full'])) {

            $wm_file = PHOTODIR.$wm_file1;

        } else {

            $wm_file = PHOTODIR.$wm_file2;

        }

        header("Content-type: image/jpg");

        $img = PHOTODIR.$data['product_image'];

        $cop = BASEDIR.$settings['photo_watermark_image'];

        if (preg_match("/.jpg/i", strtolower($img)) || preg_match("/.jpeg/i", strtolower($img))) {

            $image = imagecreatefromjpeg($img);

        } else if (preg_match("/.png/i", strtolower($img))) {

            $image = imagecreatefrompng($img);

        } else if (preg_match("/.gif/i", strtolower($img))) {

            $image = imagecreatefromgif($img);

            $sizeX = imagesx($image);

            $sizeY = imagesy($image);

            $image_tmp = imagecreatetruecolor($sizeX, $sizeY);

            $ica = imagecolorallocate($image_tmp, 255, 255, 255);

            imagefill($image_tmp, 0, 0, $ica);

            if ($settings['thumb_compression'] == "gd2") {

                imagecopyresampled($image_tmp, $image, 0, 0, 0, 0, $sizeX, $sizeY, $sizeX, $sizeY);

            } else {

                imagecopyresized($image_tmp, $image, 0, 0, 0, 0, $sizeX, $sizeY, $sizeX, $sizeY);

            }

            $tmp = PHOTODIR.md5(time().$img).'.tmp';

            imagejpeg($image_tmp, $tmp);

            imagedestroy($image_tmp);

            $image = imagecreatefromjpeg($tmp);

            unlink($tmp);

        }

        if (file_exists($cop) && preg_match("/.png/i", strtolower($cop)) && $settings['photo_watermark']) {

            $image2 = false;

            $image_dim_x = imagesx($image);

            $image_dim_y = imagesy($image);

            $copyright = imagecreatefrompng($cop);

            $copyright_dim_x = imagesx($copyright);

            $copyright_dim_y = imagesy($copyright);

            $where_x = $image_dim_x - $copyright_dim_x - 5;

            $where_y = $image_dim_y - $copyright_dim_y - 5;

            imagecopy ($image, $copyright, $where_x, $where_y, 0, 0, $copyright_dim_x, $copyright_dim_y);

            $thumb_w = 0; $thumb_h = 0;

            if (!isset($_GET['full'])) {

                if ($image_dim_x > $settings['photo_w'] || $image_dim_y > $settings['photo_h']) {

                    if ($image_dim_x  $image_dim_y) {

                        $thumb_w = $settings['photo_w'];

                        $thumb_h = round(($image_dim_y * $settings['photo_w']) / $image_dim_x);

                    } else {

                        $thumb_w = $settings['photo_w'];

                        $thumb_h = $settings['photo_h'];

                    }

                } else {

                    $thumb_w = $image_dim_x;

                    $thumb_h = $image_dim_y;

                }

                $image2 = imagecreatetruecolor($thumb_w, $thumb_h);

                if ($settings['thumb_compression'] == "gd2") {

                    imagecopyresampled($image2, $image, 0, 0, 0, 0, $thumb_w, $thumb_h, $image_dim_x, $image_dim_y);

                } else {

                    imagecopyresized($image2, $image, 0, 0, 0, 0, $thumb_w, $thumb_h, $image_dim_x, $image_dim_y);

                }

                imagedestroy($image);

            }

        }

        //create image

        if ($settings['photo_watermark_save']) { imagejpeg(($image2 ? $image2 : $image), $wm_file); }

        imagejpeg((isset($image2) && $image2 ? $image2 : $image));

        imagedestroy((isset($image2) && $image2 ? $image2 : $image));

        if (isset($copyright) && is_resource($copyright)) {  ImageDestroy($copyright); }

} else {

    redirect("index.php");

}

Please Help ME! Thank you!

1

1 Answers

0
votes

Is php.ini set with display_errors turned on? If so, your code may be throwing a warning or notice which is getting incorporated into the binary image data and corrupting it. Check your PHP error log for new entries.

Another possibility is that an include file is outputting whitespace by having multiple blank lines in the bottom below the closing PHP tag.