Author Topic: PHP to resize nexstorm.jpg/wasp2.png images w/cacheing  (Read 7264 times)

0 Members and 1 Guest are viewing this topic.

Offline saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 9279
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
PHP to resize nexstorm.jpg/wasp2.png images w/cacheing
« on: July 26, 2006, 04:29:50 PM »
Just added a Boltek-PCI/NexStorm/StormVue to the site, and joined StrikeStarUS.

Lightning: http://saratoga-weather.org/lightning.php
StormVue: http://saratoga-weather.org/stormvue.php

My antenna is inside the house right now (too hot lately to venture on the roof for mounting), but should be relocated MUCH higher within the week.

Thanks to Tom (carterlake) and his tracreportshort.php program, there's a short summary above the image with the summary.  I also wrote a small PHP to resize the nexstorm.jpg from 794x552 to 640x445 so it fits in my page design (limit 800px).  Here it is if you'd like to use something similar.

resize-nexstorm-image.php
Code: [Select]
<?php
// resize nexstorm.jpg from 794 x 552 to 640 x 445 to fit on screen
// Ken True - 25-Jul-2006

    
$Graphic 'nexstorm.jpg';
$new_width 640;
$new_height 445;


    
$image loadJPEG&#40;$Graphic&#41;;  // fetch our map image
$MaxX imagesx&#40;$image&#41;;
$MaxY imagesy&#40;$image&#41;;
$image_p imagecreatetruecolor&#40;$new_width, $new_height&#41;;
    
imagecopyresampled&#40;$image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $MaxX, $MaxY&#41;;

header&#40;"Content-type&#58; image/jpeg"&#41;; // now send to browser
    
imagejpeg&#40;$image_p, null, 90&#41;; 
    
imagedestroy&#40;$image&#41;; 
    
imagedestroy&#40;$image_p&#41;; 
    
exit;




function 
loadJPEG &#40;$imgname&#41; &#123; 
   
$im = @imagecreatefromjpeg &#40;$imgname&#41;; /* Attempt to open */ 
   
if &#40;!$im&#41; &#123; /* See if it failed */ 
       
$im  imagecreate &#40;150, 30&#41;; /* Create a blank image */ 
       
$bgc imagecolorallocate &#40;$im, 255, 255, 255&#41;; 
       
$tc  imagecolorallocate &#40;$im, 0, 0, 0&#41;; 
       
imagefilledrectangle &#40;$im, 0, 0, 150, 30, $bgc&#41;; 
       /* Output an errmsg */ 
       
imagestring &#40;$im, 1, 5, 5, "Error loading $imgname", $tc&#41;; 
   
&#125; 
   
return $im
&
#125; 

?>


To use, just put
Code: [Select]

  <img src="resize-nexstorm-image.php" alt="NexStorm Display" width="640" height="445" />
where you want the resampled image to appear.
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis VP1+ FARS, Blitzortung RED, GRLevel3, WD, WL, VWS, Cumulus, Meteobridge
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

Offline neondesert

  • Forecaster
  • *****
  • Posts: 628
    • http://www.neondesertweather.com
PHP to resize nexstorm.jpg/wasp2.png images w/cacheing
« Reply #1 on: July 29, 2006, 09:55:53 AM »
Quote
Just added a Boltek-PCI/NexStorm/StormVue to the site, and joined StrikeStarUS.

Lightning: http://saratoga-weather.org/lightning.php
StormVue: http://saratoga-weather.org/stormvue.php


Nice to have you aboard StrikeStar  8) !


Quote
My antenna is inside the house right now (too hot lately to venture on the roof for mounting), but should be relocated MUCH higher within the week.


Too Hot :?:  :!: ...Come on Ken, you just need to build up some callouses on your hands and you'll be ready to go :lol:  :D .  

Thanks, for the outstanding PHP code as usual  :wink:
Larry
"But it's a DRY Heat!"


Offline saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 9279
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
PHP to resize nexstorm.jpg/wasp2.png images w/cacheing
« Reply #2 on: August 04, 2006, 02:02:43 AM »
Ah, we have such a mild climate (usually) that my hands are caloused only on the fingertips from typing code and entries to forums :-)

But, with the major help of my son, the 18yr old brother of his girlfriend, and my brother-in-law, the detector is now in an Sch-40 ABS housing about 5 feet above the roof-line on the north eave of the house.

Also, thanks to krelvinaz on the WD forum -- he suggested I add caching to the resize script to lessen the server load, so here's the new code for resize-nexstorm-image.php
Code: [Select]
<?php
// resize nexstorm.jpg from 794 x 552 to 640 x 445 to fit on screen
// Ken True - 25-Jul-2006
// modified for cacheing of the image, 3-Aug-2006

    
$Graphic 'nexstorm.jpg';
$new_width 640;
$new_height 445;

$Cache preg_replace&#40;'|\.jpg|','-cache.jpg',$Graphic&#41;;
$GraphicTime filectime&#40;$Graphic&#41;;
$CacheTime filectime&#40;$Cache&#41;;

    
if &#40;$GraphicTime > $CacheTime&#41; &#123;
      
$image loadJPEG&#40;$Graphic&#41;;  // fetch our map image
  $MaxX imagesx&#40;$image&#41;;
  $MaxY imagesy&#40;$image&#41;;
  $image_p imagecreatetruecolor&#40;$new_width, $new_height&#41;;
      
imagecopyresampled&#40;$image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $MaxX, $MaxY&#41;;
      
imagejpeg&#40;$image_p, $Cache, 90&#41;; 
      
imagedestroy&#40;$image&#41;; 
      
imagedestroy&#40;$image_p&#41;; 
&#125;

    if &
#40;file_exists&#40;$Cache&#41;&#41; &#123;
  header&#40;"Content-type&#58; image/jpeg"&#41;; // now send to browser
  readfile&#40;$Cache&#41;;
&#125;

    exit;




function 
loadJPEG &#40;$imgname&#41; &#123; 
   
$im = @imagecreatefromjpeg &#40;$imgname&#41;; /* Attempt to open */ 
   
if &#40;!$im&#41; &#123; /* See if it failed */ 
       
$im  imagecreate &#40;150, 30&#41;; /* Create a blank image */ 
       
$bgc imagecolorallocate &#40;$im, 255, 255, 255&#41;; 
       
$tc  imagecolorallocate &#40;$im, 0, 0, 0&#41;; 
       
imagefilledrectangle &#40;$im, 0, 0, 150, 30, $bgc&#41;; 
       /* Output an errmsg */ 
       
imagestring &#40;$im, 1, 5, 5, "Error loading $imgname", $tc&#41;; 
   
&#125; 
   
return $im
&
#125; 

?>


Ken
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis VP1+ FARS, Blitzortung RED, GRLevel3, WD, WL, VWS, Cumulus, Meteobridge
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP

Offline saratogaWX

  • Administrator
  • Forecaster
  • *****
  • Posts: 9279
  • Saratoga, CA, USA Weather - free PHP scripts
    • Saratoga-Weather.org
PHP to resize nexstorm.jpg/wasp2.png images w/cacheing
« Reply #3 on: August 07, 2006, 11:25:25 AM »
I discovered that the WASP2 png screen capture was also too wide (660px) to fit in the 640px allowed in my site design, so here's a companion script to handle resizing with caching of it too.

Both scripts, and Tom's (carterlake) excellent tracreportshort.php are used on my lightning page ( http://saratoga-weather.org/lightning.php )

usage:
Code: [Select]
<img src="resize-wasp2-image.php" alt="Wide Area Storm Probe 2" width="640" height="562" />
resize-wasp2-image.php
Code: [Select]
<?php
// resize wasp2.png from 660 x 580 to 640 x 562 to fit on screen
// Ken True - 3-Aug-2006 webmaster@saratoga-weather.org

    
$Graphic 'WASP2/wasp2.png';

$new_width 640;

$new_height 562;


$Cache preg_replace&#40;'|\.png|','-cache.png',$Graphic&#41;;

$GraphicTime filectime&#40;$Graphic&#41;;

$CacheTime filectime&#40;$Cache&#41;;

    
if &#40;$GraphicTime > $CacheTime&#41; &#123;
      
$image loadPNG&#40;$Graphic&#41;;  // fetch our map image

  
$MaxX imagesx&#40;$image&#41;;

  
$MaxY imagesy&#40;$image&#41;;

  
$image_p imagecreatetruecolor&#40;$new_width, $new_height&#41;;
      
imagecopyresampled&#40;$image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $MaxX, $MaxY&#41;;
      
imagejpeg&#40;$image_p, $Cache, 90&#41;; 
      
imagedestroy&#40;$image&#41;; 
      
imagedestroy&#40;$image_p&#41;; 

&#125;

    
if &#40;file_exists&#40;$Cache&#41;&#41; &#123;

  
header&#40;"Content-type&#58; image/png"&#41;; // now send to browser

  
readfile&#40;$Cache&#41;;

&#125;

    
exit;




function 
loadPNG &#40;$imgname&#41; &#123; 
   
$im = @imagecreatefrompng &#40;$imgname&#41;; /* Attempt to open */ 
   
if &#40;!$im&#41; &#123; /* See if it failed */ 
       
$im  imagecreate &#40;150, 30&#41;; /* Create a blank image */ 
       
$bgc imagecolorallocate &#40;$im, 255, 255, 255&#41;; 
       
$tc  imagecolorallocate &#40;$im, 0, 0, 0&#41;; 
       
imagefilledrectangle &#40;$im, 0, 0, 150, 30, $bgc&#41;; 
       /* Output an errmsg */ 
       
imagestring &#40;$im, 1, 5, 5, "Error loading $imgname", $tc&#41;; 
   
&#125; 
   
return $im
&
#125; 

?>
Ken True/Saratoga, CA, USA main site: saratoga-weather.org
Davis VP1+ FARS, Blitzortung RED, GRLevel3, WD, WL, VWS, Cumulus, Meteobridge
Free weather PHP scripts/website templates - update notifications on Twitter saratogaWXPHP