Go Back   CodingForums.com > :: Server side development > Apache configuration

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-03-2011, 09:33 PM   PM User | #1
MoreBloodWine
Regular Coder

 
Join Date: Nov 2010
Posts: 204
Thanks: 10
Thanked 0 Times in 0 Posts
MoreBloodWine is an unknown quantity at this point
Using .htaccess to "ping" a file on an image call...

What would need to be written into my sites .htaccess file to "ping" a php file when an image is called ?

So if I type mysite.com/image.jpg into my browser, a file at mysite.com/image.php is ran.

Basically when image.jpg is called image.php should be ran to update the displayed images contents.

Edit: The below bit half works, it gets the images updated by hitting the associated php files when the image(s) are called but when I try to call the images they don't show (red x box as seen below), what gives ?

RewriteEngine on
RewriteRule Nintendo3DS4free.jpg$ Nintendo3DS4free.php [L]

http://www.eojmarket.com/NonForumStu...do3DS4free.jpg


Edit 2: For arguments sake, this is the contents of my entire .htaccess file.

Code:
Options +FollowSymLinks -Indexes

RewriteEngine on
RewriteRule Nintendo3DS4free.jpg$ Nintendo3DS4free.php [L]

<Files 403.shtml>
order allow,deny
allow from all
</Files>
MoreBloodWine is offline   Reply With Quote
Old 03-06-2011, 06:24 AM   PM User | #2
ThePHPSolution
New to the CF scene

 
Join Date: Dec 2010
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
ThePHPSolution is an unknown quantity at this point
Apologies for any incoherency in this late night post, but just thought I'd reply to this before stumbling off to bed.

When you use RewriteRule, you aren't just pinging that script, you are redirecting the requested file from the first one to the second one. So, when they request the .jpg file, the server is actually serving the .php one.

If you want to accomplish the "pinging" aspect in the PHP file, but still display the file to the browser, you must serve the image to the browser with the PHP file. There are a number of ways to do this, such as (from php.net) :

PHP Code:
<?php

// open the file in a binary mode
$name '/path/to/nintendoimg.jpg';
$fp fopen($name'rb');

// send the right headers
header("Content-Type: image/jpeg");
header("Content-Length: " filesize($name));

// dump the picture and stop the script
fpassthru($fp);
exit;

?>
ThePHPSolution is offline   Reply With Quote
Old 03-06-2011, 12:42 PM   PM User | #3
MoreBloodWine
Regular Coder

 
Join Date: Nov 2010
Posts: 204
Thanks: 10
Thanked 0 Times in 0 Posts
MoreBloodWine is an unknown quantity at this point
Quote:
Originally Posted by ThePHPSolution View Post
Apologies for any incoherency in this late night post, but just thought I'd reply to this before stumbling off to bed.

When you use RewriteRule, you aren't just pinging that script, you are redirecting the requested file from the first one to the second one. So, when they request the .jpg file, the server is actually serving the .php one.

If you want to accomplish the "pinging" aspect in the PHP file, but still display the file to the browser, you must serve the image to the browser with the PHP file. There are a number of ways to do this, such as (from php.net) :

PHP Code:
<?php

// open the file in a binary mode
$name '/path/to/nintendoimg.jpg';
$fp fopen($name'rb');

// send the right headers
header("Content-Type: image/jpeg");
header("Content-Length: " filesize($name));

// dump the picture and stop the script
fpassthru($fp);
exit;

?>
I've since changed a few small things around to use less files since I have more than one image to play with. In any case, here is now what's in my .htaccess file.

RewriteEngine on
RewriteRule ^(.*).jpg$ ImageResize.php [L]

Now here is what's in the PHP file to be pinged when any image is called. The four things at the bottom is what controls the image stuff for the images I call. So, here's my question... to make your solution work for all images regardless of name as I may add more what would need to be done andd where in the PHP file, I'll assume I can leave my .htaccess stuff the way it is.

Code:
<?php
class SimpleImage {
   
   var $image;
   var $image_type;
 
   function load($filename) {
      $image_info = getimagesize($filename);
      $this->image_type = $image_info[2];
      if( $this->image_type == IMAGETYPE_JPEG ) {
         $this->image = imagecreatefromjpeg($filename);
      } elseif( $this->image_type == IMAGETYPE_GIF ) {
         $this->image = imagecreatefromgif($filename);
      } elseif( $this->image_type == IMAGETYPE_PNG ) {
         $this->image = imagecreatefrompng($filename);
      }
   }
   function save($filename, $image_type=IMAGETYPE_JPEG, $compression=75, $permissions=null) {
      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image,$filename,$compression);
      } elseif( $image_type == IMAGETYPE_GIF ) {
         imagegif($this->image,$filename);         
      } elseif( $image_type == IMAGETYPE_PNG ) {
         imagepng($this->image,$filename);
      }   
      if( $permissions != null) {
         chmod($filename,$permissions);
      }
   }
   function output($image_type=IMAGETYPE_JPEG) {
      if( $image_type == IMAGETYPE_JPEG ) {
         imagejpeg($this->image);
      } elseif( $image_type == IMAGETYPE_GIF ) {
         imagegif($this->image);         
      } elseif( $image_type == IMAGETYPE_PNG ) {
         imagepng($this->image);
      }   
   }
   function getWidth() {
      return imagesx($this->image);
   }
   function getHeight() {
      return imagesy($this->image);
   }
   function resizeToHeight($height) {
      $ratio = $height / $this->getHeight();
      $width = $this->getWidth() * $ratio;
      $this->resize($width,$height);
   }
   function resizeToWidth($width) {
      $ratio = $width / $this->getWidth();
      $height = $this->getheight() * $ratio;
      $this->resize($width,$height);
   }
   function scale($scale) {
      $width = $this->getWidth() * $scale/100;
      $height = $this->getheight() * $scale/100; 
      $this->resize($width,$height);
   }
   function resize($width,$height) {
      $new_image = imagecreatetruecolor($width, $height);
      imagecopyresampled($new_image, $this->image, 0, 0, 0, 0, $width, $height, $this->getWidth(), $this->getHeight());
      $this->image = $new_image;   
   }      
}

	$image1 = new SimpleImage();
	$image1->load('http://www.YourFreeNetbook.com/goods/16824548.png');
	$image1->resize(340,85);
	$image1->save('images/YourFreeNetbook.jpg');

	$image2 = new SimpleImage();
	$image2->load('http://www.Nintendo3DS4free.com/goods/16824176.png');
	$image2->resize(340,85);
	$image2->save('images/Nintendo3DS4free.jpg');

	$image3 = new SimpleImage();
	$image3->load('http://www.YouriPad4free.com/goods/16824532.png');
	$image3->resize(340,85);
	$image3->save('images/YouriPad4free.jpg');

	$image4 = new SimpleImage();
	$image4->load('http://www.YourNintendoWii4Free.com/goods/14904127.png');
	$image4->resize(340,85);
	$image4->save('images/YourNintendoWii4Free.jpg');
?>
MoreBloodWine is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:10 AM.


Advertisement
Log in to turn off these ads.