php as cgi and GetImageSize() errors

Need help installing PHP, configuring a script, or configuring a server? Then come on in and post your questions! We'll try to help the best we can!

Moderator: General Moderators

Post Reply
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

php as cgi and GetImageSize() errors

Post by blacksnday »

Has anyone else had problems with using
GetImageSize($filename);
when running php as cgi?

I used a watermark script that worked perfectly
then when I moved to a new server that had php as cgi,
all of sudden it stopped working and returned the error:
PHP Warning: getimagesize(): Read error!

And now on my VPS the script still wont work and returns same error.
I have tried many other image scripts and all fail with above error.

I read up on php as cgi and did turn one option on that I thought would fix
the problem.. the option was: cgi.fix_pathinfo=1
however it did not fix the problem.

Any suggestions?

the full watermark script I am trying to get to work on new server is below

Code: Select all

/*

 activeWatermark

 Free script that places watermark on images in folder

 Copyright (C) 2005 ActiveUnit.com

 This program is free software; you can redistribute it and/or
 modify it under the terms of the GNU General Public License
 as published by the Free Software Foundation; either version 2
 of the License, or (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.

*/


 // IMAGE WATERMARK (comment line below if you do not want to use image watermark)
 //Define('WATERMARK_IMAGE', './wt.png'); // path to watermark image
 Define('WATERMARK_PERCENT', '60'); // Intensity of the transition (in percent)


 // TEXT WATERMARK (comment line below if you do not want to use text)
 Define('WATERMARK_TEXT', 'Copyright (c) 2006 http://www.bashmyex.com'); // text to place (image will not be used)
 Define('WATERMARK_TEXT_FONT', '3'); // font 1 / 2 / 3 / 4 / 5
 Define('TEXT_SHADOW', '1'); // 1 - yes / 0 - no
 Define('TEXT_COLOR', '#FFFFFF'); // text color 


 // GENERAL SETTINGS
 Define('WATERMARK_ALIGN_H', 'right'); // left / right / center
 Define('WATERMARK_ALIGN_V', 'bottom'); // top / bottom / center
 Define('WATERMARK_MARGIN', '10'); // margin


 $dr=preg_replace('/modify\.php.+/', '', $_SERVER['PHP_SELF']);
 $filename=str_replace($dr, './', $_SERVER['PATH_INFO']);

 $lst=GetImageSize($filename);
 $image_width=$lst[0];
 $image_height=$lst[1];
 $image_format=$lst[2];

 if ($image_format==1) {
   Header("Content-Type:image/gif");
   readfile($filename);
   exit;
 } elseif ($image_format==2) {
  $old_image=imagecreatefromjpeg($filename);
 } elseif ($image_format==3) {
  $old_image=imagecreatefrompng($filename);
 } else {
   readfile($filename);
   exit;
 }


 if (Defined('WATERMARK_TEXT') && WATERMARK_TEXT!='') {
 // text

  $color = eregi_replace("#","", TEXT_COLOR);
  $red = hexdec(substr($color,0,2));
  $green = hexdec(substr($color,2,2));
  $blue = hexdec(substr($color,4,2));

  $text_color = imagecolorallocate ($old_image, $red, $green, $blue); 

  $text_height=imagefontheight(WATERMARK_TEXT_FONT);
  $text_width=strlen(WATERMARK_TEXT)*imagefontwidth(WATERMARK_TEXT_FONT);
  $wt_y=WATERMARK_MARGIN;
  if (WATERMARK_ALIGN_V=='top') {
   $wt_y=WATERMARK_MARGIN;
  } elseif (WATERMARK_ALIGN_V=='bottom') {
   $wt_y=$image_height-$text_height-WATERMARK_MARGIN;
  } elseif (WATERMARK_ALIGN_V=='center') {
   $wt_y=(int)($image_height/2-$text_height/2);
  }

  $wt_x=WATERMARK_MARGIN;
  if (WATERMARK_ALIGN_H=='left') {
   $wt_x=WATERMARK_MARGIN;
  } elseif (WATERMARK_ALIGN_H=='right') {
   $wt_x=$image_width-$text_width-WATERMARK_MARGIN;
  } elseif (WATERMARK_ALIGN_H=='center') {
   $wt_x=(int)($image_width/2-$text_width/2);
  }

  if (TEXT_SHADOW=='1') {
   imagestring($old_image, WATERMARK_TEXT_FONT, $wt_x+1, $wt_y+1, WATERMARK_TEXT, 0);
  }
  imagestring($old_image, WATERMARK_TEXT_FONT, $wt_x, $wt_y, WATERMARK_TEXT, $text_color);

 } 
 
 if (Defined('WATERMARK_IMAGE') && WATERMARK_IMAGE!='' && file_exists(WATERMARK_IMAGE)) {
 // image


 $lst2=GetImageSize(WATERMARK_IMAGE);
 $image2_width=$lst2[0];
 $image2_height=$lst2[1];
 $image2_format=$lst2[2];

 if ($image2_format==2) {
  $wt_image=imagecreatefromjpeg(WATERMARK_IMAGE);
 } elseif ($image2_format==3) {
  $wt_image=imagecreatefrompng(WATERMARK_IMAGE);
 }

  if ($wt_image) {

   $wt_y=WATERMARK_MARGIN;
   if (WATERMARK_ALIGN_V=='top') {
    $wt_y=WATERMARK_MARGIN;
   } elseif (WATERMARK_ALIGN_V=='bottom') {
    $wt_y=$image_height-$image2_height-WATERMARK_MARGIN;
   } elseif (WATERMARK_ALIGN_V=='center') {
    $wt_y=(int)($image_height/2-$image2_height/2);
   }

   $wt_x=WATERMARK_MARGIN;
   if (WATERMARK_ALIGN_H=='left') {
    $wt_x=WATERMARK_MARGIN;
   } elseif (WATERMARK_ALIGN_H=='right') {
    $wt_x=$image_width-$image2_width-WATERMARK_MARGIN;
   } elseif (WATERMARK_ALIGN_H=='center') {
    $wt_x=(int)($image_width/2-$image2_width/2);
   }

   imagecopymerge($old_image, $wt_image, $wt_x, $wt_y, 0, 0, $image2_width, $image2_height, WATERMARK_PERCENT);
  }

 }

 if ($image_format==2) {
  imageJpeg($old_image);
 }
 if ($image_format==3) {
  imagePng($old_image);
 }
with .htaccess in the image folder being

Code: Select all

AddHandler wtmrk jpg, gif, png
Action wtmrk /ratings/modify.php
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Is the server running under a different userid with CGI? Are the read permissions ok on the file to allow this? Is safe mode turned on?
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

d11wtq wrote:Is the server running under a different userid with CGI?
Not sure what you mean.
Although I am the only account on my VPS install.
(meaning I am only hosting one webaccount which I own)

d11wtq wrote:Are the read permissions ok on the file to allow this? Is safe mode turned on?
Folder Permissions for the image folder and images does not seem to affect this.
I have tried all combos of permissions with no change in the status.

safe mode is not on.


I forgot to mention above besides the main error I am getting....
with the above script, and after turning on
cgi.fix_pathinfo=1
the above script does not produce errors anymore
however no request are directed towards the
modify.php file
It seems that the .htaccess file will correctly issue the
AddHandler wtmrk jpg, gif, png
but can care less about the second part
Action wtmrk /ratings/modify.php
I have tested this by changing the Action
to non-existent files and no errors are thrown.

My image upload code also works correctly when using
GetImageSize();


All other scripts I have tried still produce the error which I posted above.
This has baffled me for about 2 months which i why i finally asked for help :)
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Re: php as cgi and GetImageSize() errors

Post by redmonkey »

blacksnday wrote:

Code: Select all

$dr=preg_replace('/modify\.php.+/', '', $_SERVER['PHP_SELF']);
 $filename=str_replace($dr, './', $_SERVER['PATH_INFO']);

Code: Select all

AddHandler wtmrk jpg, gif, png
Action wtmrk /ratings/modify.php
Perhaps it's late and I'm missing something, I don't see where the watermark script could accurately pickup the filename of the image you are trying to work with?
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Re: php as cgi and GetImageSize() errors

Post by blacksnday »

redmonkey wrote:
blacksnday wrote:

Code: Select all

$dr=preg_replace('/modify\.php.+/', '', $_SERVER['PHP_SELF']);
 $filename=str_replace($dr, './', $_SERVER['PATH_INFO']);

Code: Select all

AddHandler wtmrk jpg, gif, png
Action wtmrk /ratings/modify.php
Perhaps it's late and I'm missing something, I don't see where the watermark script could accurately pickup the filename of the image you are trying to work with?
The script directs requests for any image in the folder.
Most watermark scripts work by specificly naming a file.
This one works by name the folder you want all images to
have the watermark written to when that image is loaded.
And the folder control is achieved by the htaccess file in that folder.

This script has worked for me before. Until I switched servers
and the only notable difference is that my new server has PHP as CGI.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

Ah, I see it now.

Have you attempted any form of debugging? i.e. are the vaules of $dr and $filename as expected?
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

redmonkey wrote:Ah, I see it now.

Have you attempted any form of debugging? i.e. are the vaules of $dr and $filename as expected?
the modify.php is a tad hard to debug considering only output
from direct access can be achieved by place values before the main script code starts.
Any code after or at the end of the file will not output anything.
Makes sense considering the modify.php is not intended for direct access
and instead is intended to silently add watermarks to any image request.

Error logs are showing nothing as of now with the change to the
php.ini value of cgi.fix_pathinfo=1


Right now with that script,
It seems that the .htaccess file will correctly issue the
AddHandler wtmrk jpg, gif, png
but can care less about the second part
Action wtmrk /ratings/modify.php
I have tested this by changing the Action
to non-existent files and no errors are thrown.

This problem has to be a server setting issue
and I have pulled out all my hair in past two months
trying to resolve it :P
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

Debugging in it's most simplist form, any reason why you can't add...

Code: Select all

echo '$dr = ' . $dr . "\x0a";
echo '$filename = ' . $filename . "\x0a";
exit;
just before the..

Code: Select all

$lst=GetImageSize($filename);
..line

You might want to also check which '$_SERVER' vars contain what.

If you can't for what ever reason generate any output from the script, you can also divert debug output to file.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

Do you know what. I'd never thought of using \x0a to avoid the cross-patform issues with returns. Handy :)
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

ok. that did work.
silly me....

it outputs:
$dr = /ratings/modify.php $filename =

so now we know $filename is not getting passed when using
$_SERVER['PATH_INFO']
i thought thats what the cgi.fix_pathinfo=1 was intended to fix?
EDIT--
Damn the above is stupid of me.
Of course there would be no $filename being passed when directly
accessing the modify.php file.......

EDIT--
If i do this:
$dr=preg_replace('/modify\.php.+/', '', $_SERVER['PHP_SELF']);
$filename=str_replace($dr, './', '12-24-2005-23-26-51_snap.jpg');


it will correctly watermark....
something else must be wrong with the htaccess not forwarding properly...
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

I'd throw those echo lines in then try to access the file by your normal method i.e. accessing an image file that is supposed to call the modify.php script.

Accessing the file directly achieves nothing in terms of problem solving.

In my limited exposure to PHP as CGI I've noted there are various configs all with their own little 'characteristics' you should probably just look at what server and environment variables are available to script, see if there is something else which is more reliable in a CGI situation.
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

redmonkey wrote:I'd throw those echo lines in then try to access the file by your normal method i.e. accessing an image file that is supposed to call the modify.php script.

Accessing the file directly achieves nothing in terms of problem solving.

In my limited exposure to PHP as CGI I've noted there are various configs all with their own little 'characteristics' you should probably just look at what server and environment variables are available to script, see if there is something else which is more reliable in a CGI situation.
I cant test that way for the reason i have said a few times already:

It seems that the .htaccess file will correctly issue the
AddHandler wtmrk jpg, gif, png
but can care less about the second part
Action wtmrk /ratings/modify.php
I have tested this by changing the Action
to non-existent files and no errors are thrown.


Basically I now know this script indeed works!
I also now know that the images are NOT being directed to
modify.php.
redmonkey
Forum Regular
Posts: 836
Joined: Thu Dec 18, 2003 3:58 pm

Post by redmonkey »

OK, so definately a case of not enough sleep for me. It seems I've found the title vs contents somewhat misleading. I was under the impression (wrongly obviously) when calling an image from the directory the script was falling over with the getimagesize warning.

Calling the script directly will obvoius present problems.

The current problem you have is not (as far as I can see) a PHP or CGI problem, it is that there appears to be problems with the .htaccess file (or Apache config). Not really having had to deal with the AddHandler directive much over the standard configs I'm not sure I can really help that much. One thing I did note though is that you have defined the extension as jpg, gif, png. Is that normal/acceptable syntax in this case? Most Apache configs are in the way of '.jpg .gif .png' style, could be wrong but worth a try?
User avatar
blacksnday
Forum Contributor
Posts: 252
Joined: Sat Jul 30, 2005 6:11 am
Location: bfe Ohio :(

Post by blacksnday »

Originally I was thinking that GetImageSize();
was the problem, because many image scripts I attempted to use
all threw that error.

After various fix attempts I started to realize that
at least with the Watermark script... GetImageSize();
is indeed not the problem.

I will check more into the htaccess file and see about the
Directives.

Anyways thanks for the help so far :)
Post Reply