Web Hosting Talk







View Full Version : [PHP] Issues with dynamically generated images


latheesan
09-20-2009, 03:10 AM
Hello,

I use the following function to generate error images:

function generateErrorImage($msg)
{
// Set Error Message
$error_msg = (isset($msg) ? $msg : 'Unknown');

// Set Image Properties
$im_font_size = 2;
$im_padding = 5;
$im_width = (imagefontwidth($im_font_size) * strlen($error_msg) + ($im_padding * 2));
$im_height = (imagefontheight($im_font_size) + ($im_padding * 2));

// Generate Image Object
$im = imagecreatetruecolor($im_width, $im_height);
$bgc = imagecolorallocate($im, 255, 236, 236);
$tc = imagecolorallocate($im, 204, 0, 0);
imagefilledrectangle($im, 0, 0, $im_width, $im_height, $bgc);
imagestring($im, $im_font_size, $im_padding, $im_padding, $error_msg, $tc);

// Add Border To Image
$bc = imagecolorallocate($im, 204, 0, 0);
$x = 0;
$y = 0;
$w = imagesx($im) - 1;
$h = imagesy($im) - 1;
imageline($im, $x,$y,$x,$y+$h,$bc);
imageline($im, $x,$y,$x+$w,$y,$bc);
imageline($im, $x+$w,$y,$x+$w,$y+$h,$bc);
imageline($im, $x,$y+$h,$x+$w,$y+$h,$bc);

// Output Error Image To Browser
header("Content-Type: image/png");
header('Last-Modified: '. gmdate('D, d M Y H:i:s', strtotime("7 days ago")) .' GMT');
ob_start();
imagepng($im);
$image_data = ob_get_contents();
$image_data_length = ob_get_length();
ob_end_clean();
header('Content-Length: '. $image_data_length);
echo $image_data;
imagedestroy($im);
exit();
}

This works fine when i run it on my local web server (XAMPP). But, when i upload the script to the live server, i get the following errors instead:

<br />
<b>Notice</b>: Undefined variable: im in <b>/home/username/domain.com/image.php</b> on line <b>38</b><br />
<br />
<b>Warning</b>: imagecolorallocate(): supplied argument is not a valid Image resource in <b>/home/username/domain.com/image.php</b> on line <b>38</b><br />
<br />
<b>Notice</b>: Undefined variable: im in <b>/home/username/domain.com/image.php</b> on line <b>39</b><br />
<br />
<b>Warning</b>: imagecolorallocate(): supplied argument is not a valid Image resource in <b>/home/username/domain.com/image.php</b> on line <b>39</b><br />
<br />
<b>Notice</b>: Undefined variable: im in <b>/home/username/domain.com/image.php</b> on line <b>40</b><br />
<br />
<b>Warning</b>: imagefilledrectangle(): supplied argument is not a valid Image resource in <b>/home/username/domain.com/image.php</b> on line <b>40</b><br />
<br />
<b>Notice</b>: Undefined variable: im in <b>/home/username/domain.com/image.php</b> on line <b>41</b><br />
<br />
<b>Warning</b>: imagestring(): supplied argument is not a valid Image resource in <b>/home/username/domain.com/image.php</b> on line <b>41</b><br />
<br />
<b>Notice</b>: Undefined variable: im in <b>/home/username/domain.com/image.php</b> on line <b>44</b><br />
<br />
<b>Warning</b>: imagecolorallocate(): supplied argument is not a valid Image resource in <b>/home/username/domain.com/image.php</b> on line <b>44</b><br />
<br />
<b>Notice</b>: Undefined variable: im in <b>/home/username/domain.com/image.php</b> on line <b>47</b><br />
<br />
<b>Warning</b>: imagesx(): supplied argument is not a valid Image resource in <b>/home/username/domain.com/image.php</b> on line <b>47</b><br />
<br />
<b>Notice</b>: Undefined variable: im in <b>/home/username/domain.com/image.php</b> on line <b>48</b><br />
<br />
<b>Warning</b>: imagesy(): supplied argument is not a valid Image resource in <b>/home/username/domain.com/image.php</b> on line <b>48</b><br />
<br />
<b>Notice</b>: Undefined variable: im in <b>/home/username/domain.com/image.php</b> on line <b>49</b><br />
<br />
<b>Warning</b>: imageline(): supplied argument is not a valid Image resource in <b>/home/username/domain.com/image.php</b> on line <b>49</b><br />
<br />
<b>Notice</b>: Undefined variable: im in <b>/home/username/domain.com/image.php</b> on line <b>50</b><br />
<br />
<b>Warning</b>: imageline(): supplied argument is not a valid Image resource in <b>/home/username/domain.com/image.php</b> on line <b>50</b><br />
<br />
<b>Notice</b>: Undefined variable: im in <b>/home/username/domain.com/image.php</b> on line <b>51</b><br />
<br />
<b>Warning</b>: imageline(): supplied argument is not a valid Image resource in <b>/home/username/domain.com/image.php</b> on line <b>51</b><br />
<br />
<b>Notice</b>: Undefined variable: im in <b>/home/username/domain.com/image.php</b> on line <b>52</b><br />
<br />
<b>Warning</b>: imageline(): supplied argument is not a valid Image resource in <b>/home/username/domain.com/image.php</b> on line <b>52</b><br />
<br />
<b>Notice</b>: Undefined variable: im in <b>/home/username/domain.com/image.php</b> on line <b>58</b><br />
<br />
<b>Warning</b>: imagepng(): supplied argument is not a valid Image resource in <b>/home/username/domain.com/image.php</b> on line <b>58</b><br />
<br />
<b>Notice</b>: Undefined variable: im in <b>/home/username/domain.com/image.php</b> on line <b>64</b><br />
<br />
<b>Warning</b>: imagedestroy(): supplied argument is not a valid Image resource in <b>/home/username/domain.com/image.php</b> on line <b>64</b><br />

This behaviour was only found in Internet Explorer 8 (don't know about older version of the IE).

On firefox, it says the following:

The image “http://domain.com/image.php?ID=1bH58Yy42PHR2eiBvenJhcX93e2g2e2tuOWpvfSctOyorOjomIC4kNiA7elYlNDszITM9PjZydGQ_3-&W=100&H=100” cannot be displayed, because it contains errors.

What am i doing wrong here?

fabin
09-20-2009, 04:57 AM
I have got this error myself in one of my previous project..

The problem seems to be with the function imagecreatetruecolor(), when there is gd2 installed on server ( not sure about this )

Read this page http://us.php.net/manual/en/function.imagecreatetruecolor.php. Also, compare the php configuration of both machines and find whats missing with GD.

latheesan
09-20-2009, 11:41 AM
My local test server (XAMPP) is running PHP Version 5.2.9

and it's configured like so:

cscript /nologo configure.js "--enable-snapshot-build" "--enable-debug-pack" "--with-snapshot-template=d:\php-sdk\snap_5_2\vc6\x86\template" "--with-php-build=d:\php-sdk\snap_5_2\vc6\x86\php_build" "--with-pdo-oci=D:\php-sdk\oracle\instantclient10\sdk,shared" "--with-oci8=D:\php-sdk\oracle\instantclient10\sdk,shared"

with GD: http://i33.tinypic.com/27ww32x.png

------------------

My live server is running PHP Version 5.2.6

and it's configured like so:

'./configure' '--build=i686-redhat-linux-gnu' '--host=i686-redhat-linux-gnu' '--target=i386-redhat-linux-gnu' '--program-prefix=' '--prefix=/usr' '--exec-prefix=/usr' '--bindir=/usr/bin' '--sbindir=/usr/sbin' '--sysconfdir=/etc' '--datadir=/usr/share' '--includedir=/usr/include' '--libdir=/usr/lib' '--libexecdir=/usr/libexec' '--localstatedir=/var' '--sharedstatedir=/usr/com' '--mandir=/usr/share/man' '--infodir=/usr/share/info' '--cache-file=../config.cache' '--with-libdir=lib' '--with-config-file-path=/etc' '--with-config-file-scan-dir=/etc/php.d' '--disable-debug' '--with-pic' '--disable-rpath' '--without-pear' '--with-bz2' '--with-curl' '--with-exec-dir=/usr/bin' '--with-freetype-dir=/usr' '--with-png-dir=/usr' '--with-xpm-dir=/usr' '--enable-gd-native-ttf' '--with-t1lib=/usr' '--without-gdbm' '--with-gettext' '--with-gmp' '--with-iconv' '--with-jpeg-dir=/usr' '--with-openssl' '--with-png' '--with-expat-dir=/usr' '--with-pcre-regex=/usr' '--with-zlib' '--with-layout=GNU' '--enable-exif' '--enable-ftp' '--enable-magic-quotes' '--enable-sockets' '--enable-sysvsem' '--enable-sysvshm' '--enable-sysvmsg' '--enable-track-vars' '--enable-trans-sid' '--enable-yp' '--enable-wddx' '--with-kerberos' '--enable-ucd-snmp-hack' '--with-unixODBC=shared,/usr' '--enable-memory-limit' '--enable-shmop' '--enable-calendar' '--enable-dbx' '--enable-dio' '--without-mime-magic' '--without-sqlite' '--with-libxml-dir=/usr' '--with-xml' '--with-system-tzdata' '--with-apxs2=/usr/sbin/apxs' '--without-mysql' '--without-gd' '--without-odbc' '--disable-dom' '--disable-dba' '--without-unixODBC' '--disable-pdo' '--disable-xmlreader' '--disable-xmlwriter' '--disable-json' '--without-pspell'

with GD: http://i33.tinypic.com/16c98p3.png

------------------

so, what's my live server missing?

fabin
09-20-2009, 12:20 PM
apxs2=/usr/sbin/apxs' '--without-mysql' '--without-gd' '--without-odbc' '--disable-dom' '--disable-dba'

live server missing gd itself?

fabin
09-20-2009, 12:23 PM
Try a small script like this in your live server and check how it goes..

<?php
header ('Content-type: image/png');
$im = @imagecreatetruecolor(120, 20)
or die('Cannot Initialize new GD image stream');
$text_color = imagecolorallocate($im, 233, 14, 91);
imagestring($im, 1, 5, 5, 'A Simple Text String', $text_color);
imagepng($im);
imagedestroy($im);
?>

latheesan
09-20-2009, 04:39 PM
GD is installed and working fine though -> http://infinite-studio.com/dev/test/gd.php

fabin
09-20-2009, 04:59 PM
$im = imagecreatetruecolor($im_width, $im_height);

is it returning false at this line?

fabin
09-20-2009, 05:06 PM
you might be having "display_errors" setting of PHP turned on in the live server. Try turning it off.

cselzer
09-20-2009, 06:08 PM
error_reporting(0); and it will work, your php.ini on your live server has display warnings