Category: web

htaccess redirects

Posted by – August 31, 2009

I recently had someone ask me if they could set the priority of some pages. In this specific case they wanted index.php to take precedence over index.html. Well, on shared hosting you can’t change the server config to do this (which by the way is in the httpd.conf for Apache). So the next best thing to do is .htaccess redirects.

htaccess will only work on Apache webservers, if in doubt consult your webhost. To use these, you will need to create a text file and call it htaccess. Input code from below into it to suit your needs, and upload to your site via ftp. You will then need to rename the file to .htaccess

There are a couple different codes that are returned to the browser on redirects.

301 – Permanent Redirect. Good for moving files forever. This tells the search engine and clients that it has moved permanently and should use the new URI.

302 – Temporary Redirect. It tells the search engine and client that it’s redirecting and should continue to use the requested URI.

Here are some sample redirect usages:

301 Moving a single page

Redirect 301 /oldpage.html http://www.example.com/newpage.html

301 Move entire site

Redirect 301 / http://www.example.com/

To do 302 redirects, just replace 301 with 302.

write on images with php

Posted by – March 23, 2009

I recently had a client that wanted to generate those dynamic images with text on them. I know you’ve seen them all over. The ones with the nude chicks holding signs saying “Alberto wuz here” or whatever. Truth is, these fan signs are really easy to make.

For this example I’m gonna use this image:

sign.jpg

In order to write on images, you’re gonna need the GD library installed in your server. Most of the time this is already done if you’re using shared hosting or if you’ve already got a working webserver.

Save and upload that image into a dir on your server, and we’ll begin working.

We want to be able to input any string of text so we’re going to make a form for submission. Create a form using:

<form action="img.php" method="get">
<input type="text" name="line1" />
<input type="submit" name="submit" value="Do it!" />
</form>

As you can see, it’s not fancy, but it works.

Lets start off with the “Hello World” example. Save this as img.php

<?php
$string = $_GET['line1']; // lets get the text from the form
// define what image we want to write on
$im = imagecreatefromjpeg("sign.jpg");
// define the text color, red, green, blue
$textcolor = imagecolorallocate($im, 61, 61, 61);
$font = imageloadfont('Brigitte.gdf'); // tells it what font to use
imagestring($im, $font, 175, 395, $string, $textcolor); // line o'text
// header to tell the browser it's an image
header("Content-type: image/jpeg");
imagejpeg($im); // creates the image with text
imagedestroy($im); // destroys the image in the memory
?>

Let’s take a look at whats going on here. From the comments you can tell whats going on for the most part, but I can expand.

Line 1 of the code is using the GET method from the form we created to store the text in a variable called $string.

imagecreatefromjpeg() PHP function tells PHP that we want to use a JPEG image. PHP has functions for GIF, PNG and strings, BMP, XBM, XPM, and from GD itself.

We then allocate the colors we want to use for the text. imagecolorallocate() uses RGB values to do this.

imageloadfont() loads our font for writing. It can be a bit tricky to find a font suitable for writing on images and I’ve included a few in the download at the end of this article with the rest of the code. There is, however, a great program that allows you to take any Windows font and turn it into a GD font. You can get this program from here: http://www.wedwick.com/wftopf.exe

The fifth line of code is where PHP does the writing. It takes the image, font, X and Y coordinates, the string (your text), and finally the text color and writes on the image.

The header() is basic PHP to tell the browser what kind of content it is viewing, in this case an image.

PHP then creates the image, and destroys the temp image in memory. You should now have something like this:

Afterthoughts: You can create a form that does POST instead of GET for a bit more “security” through obsecurity.

You could use one page for all the code, but I used two to help newbies.

Download archive with project files here: php-gd.rar

Here is the script in action: http://riscit.info/dynamic_image/input.html