[SOLVED] Dynamic signature

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
pendulum
Forum Newbie
Posts: 6
Joined: Tue Apr 19, 2005 9:30 pm

[SOLVED] Dynamic signature

Post by pendulum »

The following code works perfectly fine:

Code: Select all

<?php
      header( "Content-type: image/gif" );
      $im = ImageCreate( 483, 20 ); // width, height
      $red   = ImageColorAllocate( $im, 255, 0, 0 );
      $white = ImageColorAllocate( $im, 255, 255, 255 );
      $upt = exec('uptime');
      ImageString($im,3,3,3,$upt,$white);
      ImageGif( $im );
      ImageDestroy( $im ); // Free memory
?>
The above code is stored in thefile.php. When I request http://example.com/thefile.php my uptime is displayed like it should be. I want to be able to link to my file from within a forum and the forum doesn't allow linking to .php files.

I gathered the work around was to create a .htaccess file with the following:

Code: Select all

&lt;FilesMatch &quote;^.*\.gif&quote;&gt;
   SetHandler application/x-httpd-php
&lt;/FilesMatch&gt;
That should make all .gif files in that directory be treated as .php files. So now all I have to do is rename thefile.php to thefile.gif and I should be able to link to it and it should work as a gif... but it doesn't :(

Anyone know what's wrong
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post by nigma »

With phpbb you just use the image tags to link to the url.
pendulum
Forum Newbie
Posts: 6
Joined: Tue Apr 19, 2005 9:30 pm

Post by pendulum »

But when I put

Image

It doesn't work, the whole thing just appears as plain text in the signature space. It's as if it knows it's not a valid extension (.php) for an image. I tried to trick it with thefile.php?trick=yes.jpg or something but it still won't work :(
pendulum
Forum Newbie
Posts: 6
Joined: Tue Apr 19, 2005 9:30 pm

Post by pendulum »

As you can see from my sig below
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

SetHandler is for CGI, is your PHP being run as a CGI script?

AddType is the general way: http://httpd.apache.org/docs/mod/mod_mime.html#addtype
pendulum
Forum Newbie
Posts: 6
Joined: Tue Apr 19, 2005 9:30 pm

Post by pendulum »

I've tried with AddType just now but no go :(

When I request the .gif it just says "The image cannot be loaded because it contains errors" or something, but when I rename the .gif back to .php it works fine :(

I followed the tutorial at http://www.phpmix.com/index.php?page=21 ... quirements but get the same error so maybe it's something to do with my GD?
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

pendulum wrote:I've tried with AddType just now but no go :(

When I request the .gif it just says "The image cannot be loaded because it contains errors" or something, but when I rename the .gif back to .php it works fine :(

I followed the tutorial at http://www.phpmix.com/index.php?page=21 ... quirements but get the same error so maybe it's something to do with my GD?
Try commenting the header()-call on the top of the script and check if PHP generates any errors.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it works perfectly for me..

Code: Select all

AddType application/x-httpd-php .gif .png .jpg .jpeg

Code: Select all

&lt;?php

$image = 'example.jpg';

$im = imagecreatefromjpeg( $image );

ob_start();
imagejpeg($im, '', 100);
$len = ob_get_length();
$imagedata = ob_get_clean();

header( 'Content-type: image/jpeg' );
header( 'Content-length: ' . $len );

echo $imagedata;

?&gt;
(named that way to more easily identify that it's a script, and not an image.)

outputs:
Image
pendulum
Forum Newbie
Posts: 6
Joined: Tue Apr 19, 2005 9:30 pm

Post by pendulum »

Thanks it's working now :):):)

I configured php with jpeg support to try your example, but still couldn't get the .jpg / .gif / .png (I tried them all) to get parsed as PHP

In the end I put what you told me (the AddType thing) inside the actual httpd.conf itself and restarted Apache. It's an ok workaround but still don't really get why the .htaccess didn't work, if anyone has ideas I'm still listening :)

Thanks again
User avatar
vigge89
Forum Regular
Posts: 875
Joined: Wed Jul 30, 2003 3:29 am
Location: Sweden

Post by vigge89 »

pendulum wrote:Thanks it's working now :):):)

I configured php with jpeg support to try your example, but still couldn't get the .jpg / .gif / .png (I tried them all) to get parsed as PHP

In the end I put what you told me (the AddType thing) inside the actual httpd.conf itself and restarted Apache. It's an ok workaround but still don't really get why the .htaccess didn't work, if anyone has ideas I'm still listening :)

Thanks again
.htaccess-files may be disabled, check out the AllowOverride directive; if it's set to "None", .htaccess -files are skipped. (documentation)
pendulum
Forum Newbie
Posts: 6
Joined: Tue Apr 19, 2005 9:30 pm

Post by pendulum »

Brilliant :) It now works perfectly, and it was indeed set to None :):):)
Thanks
Post Reply