Monthly Archives: January 2011

Create an image with PHP pixel by pixel

It’s pretty easy to create an image with PHP pixel by pixel. I wanted a simple good looking graphic to show how close I am to completing my swimming goal, but I didn’t want to edit and upload a new picture every day.  I decided I wanted a simple text file to edit. I made a text file with a 20×20 grid of ‘o’s and replace an o with an x for every quarter mile I swim. So really what we end up doing is making a PNG file with PHP from an ASCII file.

Here’s the grid at the moment

xxxxxxxooooooooooooo
oooooooooooooooooooo
oooooooooooooooooooo
oooooooooooooooooooo
oooooooooooooooooooo
oooooooooooooooooooo
oooooooooooooooooooo
oooooooooooooooooooo
oooooooooooooooooooo
oooooooooooooooooooo
oooooooooooooooooooo
oooooooooooooooooooo
oooooooooooooooooooo
oooooooooooooooooooo
oooooooooooooooooooo
oooooooooooooooooooo
oooooooooooooooooooo
oooooooooooooooooooo
oooooooooooooooooooo
oooooooooooooooooooo

Pretty simple, right? Now I just needed a way to use that to make a pretty bitmap. Fortunately PHP has got all the tools for this.

First we create an image object:

$img = imagecreatetruecolor($width,$height); 

Now we need to read in the text file (‘swim’) and iterate over each character to see what it is:

$log = file_get_contents('swim');
$rows = explode("\n",$log);
foreach($rows as $y => $row){
    $columns = str_split($row);
    foreach($columns as $x => $column){
        // process each letter here!
    }

Inside the inner loop we’ll use a switch statement to decide what to do based on what letter we find.

// Inner loop code
    switch($column){
        case 'x':
            $color =  imagecolorallocate($img,0,0,0);
            break;
        case 'o':
            $color = imagecolorallocate($img,0,255,255);
            break;
        default:
            $color = imagecolorallocate($img,125,125,125);
        }
        imagesetpixel($img,$x,$y,$color);

Finally we print out the image

header('Content-Type: image/png');
imagepng($img);
imagedestroy($img);

Now, the above code creates a usable image, but it’s only 20×20 pixels and the colors are kind of boring so I added the following two enhancements :

1) Enlarge the bitmap

I set the initial height and width with a variable instead of hard coding them, then re-use them here. $new_img will be $scale times larger than the original. Obviously, we will use $new_img in the imagepng() function to print it out instead of the smaller image.

$new_img = imagecreate($scale * $width,$scale * $height);
imagecopyresized($new_img,$img,0,0,0,0,$scale * $width,$scale * $height,$width,$height);

2) Add some color variation
I used the Gimp to find the RGB color ranges I wanted to use. I then put the minimum and maximum values into the rand function and every pixel will get a random different color within the range.

$color = imagecolorallocate($img,0,rand(216,255),rand(150,255));

My final script looks like this:

<?php

$width = 20;
$height = 20;
$scale = 10;
$img = imagecreatetruecolor($width,$height);

$black = imagecolorallocate($img,255,255,255);

$log = file_get_contents('swim');
$rows = explode("\n",$log);
foreach($rows as $y => $row){
    $columns = str_split($row);
    foreach($columns as $x => $column){
        switch($column){
        case 'x':
            $color =  imagecolorallocate($img,30,rand(0,100),rand(60,150));
            break;
        case 'o':
            $color = imagecolorallocate($img,0,rand(216,255),rand(150,255));
            break;
        default:
            $color = $black;
        }
        imagesetpixel($img,$x,$y,$color);
    }
}

$new_img = imagecreate($scale * $width,$scale * $height);
imagecopyresized($new_img,$img,0,0,0,0,$scale * $width,$scale * $height,$width,$height);

header('Content-Type: image/png');
imagepng($new_img);
imagedestroy($new_img);
imagedestroy($img);

The final image looks like this:

You can refresh this page to see that the image changes each time.

Posted in Computers, Something Interesting | Tagged , , , , | 1 Comment

Swimming to My Goal

A Belated New Year’s Resolution

It’s my goal to swim 100 miles this year. I’m not going to track weight loss, calories, hours of exercise or anything else — just the number of laps I have swum.

I was just going to try to swim a couple times a week, then I saw that the local YMCA has a “100 mile swim club”.  It’s not really a club (there’s no meetings) you just track your progress in a log under the life guard’s chair and when you reach 100 miles I think there’s a T-shirt or something.

So, I set this goal late, but I think it’s still early enough in the year to count as a New Year’s resolution, and early enough to still be feasible.

How Far is 100 Miles?

7200 lengths of the pool. 72 lengths of the pool per mile puts the pool at between 24 and 25 yards. That means I would have needed to swim about two miles per week had I started right at the beginning of the year.

The first time I went swimming for this goal (last week) I was only able to swim 8 laps before being all tired out. The last two days I have been able to do 24 laps each night. I need to get up to about 40 laps per night. I have put up a chart in the sidebar of this blog detailing my progress. Each black square is a quarter mile.

So, feel free to bug me about my progress or to come swim with me!

Posted in Something Interesting | Tagged | 2 Comments

Show the WP e-commerce shopping cart widget only on store pages

I’m using the WP E-Commerce plugin on a site I’m working on, and didn’t like the default cart options. The only options were to hide the cart when empty, or always display it.This might make sense on a primarily e-commerce site, but e-commerce is just a small sliver of the purpose of the site I am working on.

Instead, I wanted to display it a) Always when the user is on a store related page and b) on other pages only if the cart wasn’t empty.

This involved such a simple change I thought I would post it here for others to enjoy.

 

UPDATED to work with WP E-Commerce 3.8′s Shopping Cart Widget

The file to edit is now wp-content/plugins/wp-e-commerce/wpsc-widgets/shopping_cart_widget.php. You should now edit the widget() function at line 33.

Add this right at the top of the function:

if(preg_match('/^\/store\//',$_SERVER['REQUEST_URI']) === 0 && wpsc_cart_item_count() < 1){
      return;
 }

It looks like the hideonempty option is gone. The widget() function just does a bunch of echoes that print out the widget HTML/JavaScript. We now just do a quick check to see if we’re in the store, or if we have some items, and return immediately if we don’t.

In your file wp-content/plugins/wp-e-commerce/widgets/shopping_cart_widget.php on line 8 change

if(($options['hideonempty']== 1) && (wpsc_cart_item_count() < 1))

into

 if(preg_match('/^\/store\//',$_SERVER['REQUEST_URI']) === 0 && ($options['hideonempty']== 1) && (wpsc_cart_item_count() < 1))

Alternatively, if you only ever want to show it on store pages, change the line to

 if(preg_match('/^\/store\//',$_SERVER['REQUEST_URI']) === 0 || (($options['hideonempty']== 1) && (wpsc_cart_item_count() < 1)))

And that should do it!

PS. If you’re looking for a WordPress compatible cart, WP E-Commerce does, unfortunately, seem to be the easiest to use. I say unfortunately because it’s not as easy to set up as most WordPress plugins and the support is somewhat lacking. I have found it adequate but am interested in other, better, cart options if someone comes across one.

Posted in Computers | Tagged , , , , | 9 Comments