Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    Regular Coder
    Join Date
    Nov 2010
    Posts
    204
    Thanks
    10
    Thanked 0 Times in 0 Posts

    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>

  • #2
    New to the CF scene
    Join Date
    Dec 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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;

    ?>

  • #3
    Regular Coder
    Join Date
    Nov 2010
    Posts
    204
    Thanks
    10
    Thanked 0 Times in 0 Posts
    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');
    ?>


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •