Author Topic: PHP for cropping WASP2 images down  (Read 2325 times)

0 Members and 1 Guest are viewing this topic.

Offline SLOweather

  • Global Moderator
  • Forecaster
  • *****
  • Posts: 3456
    • Weatherelement Moline IL
PHP for cropping WASP2 images down
« on: April 13, 2007, 10:42:52 AM »
For some time, I've been wanting to crop out just the map from my SLOweather WASP2 image, cutting off the sidebar and the top and bottom.

Stock WASP2 image

I looked around at other cropping scripts and finally decided to try my hand at my own.

After I got comfortable with how the imagecopy command worked for cropping, I added code to move the time stamp from the original image onto the top right corner of the map.

Cropped WASP2 image

While I've become halfway adept at modifying pre-written PHP scripts, this is my first "from scratch" PHP script. If anyone has any comments about anything I did wrong, PHP or style-wise, please let me know.

It's very simple and hard-coded for cropping WASP2 images. It would be easy to modify for other ones, though. I didn't yet document the imagecopy parameters, but checking the manual page shows what they all are.

The one problem I haven't whupped yet is directly returning the image to the browser ala Anole's sticker script, so right now the code is in an html wrapper and writes a jpg the server and returns it instead.

Next, I might add some identifying text onto the image to discourage another site from using it.  
---

<html>

<body>
<b>


<?php

// Create a blank image the right size
$dimg=imagecreatetruecolor(540,540);

//URL of the source image
$im=("http://www.sloweather.com/lightning/wasp2.png");

//Get the WASP2 image for cropping
$simg=imagecreatefrompng($im);

//Crop the map out of the image onto the blank
imagecopy ($dimg, $simg, 1, 1, 116, 20, 540, 540);

//Crop the time stamp out of the image onto the new map
imagecopy ($dimg, $simg, 420, 5, 540, 5, 120, 15);

// Turn it into a jpg.
imagejpeg($dimg,'wasp2.jpg',100);

///Clean up...
// destroy the old image
imagedestroy($simg);
// destroy the old image
 imagedestroy($dimg);

//display the image
?>


<img alt="SLOweather RADAR" title="SLOweather Radar"
 src="wasp2.jpg">

</b>

</body>
</html>

Offline carterlake

  • Senior Contributor
  • ****
  • Posts: 243
    • CarterLake.org
Re: PHP for cropping WASP2 images down
« Reply #1 on: April 13, 2007, 06:56:58 PM »
Quote from: "SLOweather"


Next, I might add some identifying text onto the image to discourage another site from using it.  


Looks fine to me.  8)

Code: [Select]

$white = imagecolorallocate ($dimg, 255,255,255);
imagestring($dimg,2,14,0,"MY TEXT HERE",$white);

Davis VP2 6153; Weather Display (LIVE w/ Ajax); Quickcam for Notebooks Pro; Boltek w/ Nexstorm; GRLevel3; live NOAA Radio

Offline SLOweather

  • Global Moderator
  • Forecaster
  • *****
  • Posts: 3456
    • Weatherelement Moline IL
Re: PHP for cropping WASP2 images down
« Reply #2 on: April 13, 2007, 08:01:07 PM »
Cool! Thanks! I'll add that in a bit.

 

anything