2D Imaging / Animation
Adobe Photoshop Adobe Flash 3D Imaging / Animation
Autodesk Maya Cinema 4D Game Development Valve Hammer (Source) Web Adobe Dreamweaver AJAX HTML / CSS PHP Programming C++ Java Python
September 06, 2010, 12:20 AM
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Feel free to help us make a community!
 
   Home   Help / Rules Search Login Register  
Pages: [1]
 

An OOP Image Module in PHP


Replies: 0 Views: 441
Posted: July 31, 2009, 03:03 AM
Okay, so, I've been experimenting with my own project 'phpDraw' which is the fundamental to most of my other projects inolving PHP imaging. But naturally, I got sick of using those long lettered functions which would cause more errors possible project after project, looking back at the PHP manual for reference. I invested a lot of time learning about Object Oriented Programming in PHP, and frankly, it saved me a lot of time and I'm glad I learned it.

In short, I got sick of PHP's long functions and made my own module to shorten time.

For those who don't know, Object Oriented Programming is a famous method for handling data in programming, reducing coding time, and handles data much faster than any method today. PHP is another language that modernizes that language amongst the forums today (C#, VB, etc.)

There's a few requirements here.

1. You have a web server space
2. That server space lets you run PHP scripts
3. It's running on a recent PHP version (4.3 or higher)
4. Has compiled with the latest GD package (2.0 or higher)

Depending on which version of PHP you have and which GD package you are able to run, you will get different results and some things may not work, but from this tutorial, you'll get the idea as to how to run a class which uses PHP image functions.

So, first off, let's show you a few lines of basic PHP image code. This will be an example of WHY it is so stressful after a lot of PHP image projects.

Code:
<?php
$img
=imagecreate(300,300);
$black=imagecolorallocate(0,0,0);
imagefill($img,0,0,$black);
header("Content-type: image/png");
imagepng($img);
?>


That's a few lines of PHP text, and all you get is just a 300x300 black box of death.

Let me show you how I create MY black box of death using MY module.
Code:
<?php
require('phpDraw.php');
$img=new phpDraw(300,300);
$img->fill("black",0,0);
$img->output();
?>


If you had my module, that's how you'd create a black box of death.

But wait, you don't...?

Well, that's the point of this tutorial! I'd rather not keep my private secret module all to myself. I'd rather share the pleasure I got out of making this bad boy module!

How do we use Object-Oriented Programming? Well it's the idea that we create a blueprint for a type of data, and then based off that blueprint, we can create as many objects of that blueprint as we want. Given that we have a blueprint for a shoebox, we can create as many shoeboxes as we need thanks to the blueprint. The same applies for OOP - we create something called a 'class', and based off that class, we can create as many objects as we need thanks to the class. We'll be designing my module as a class. Each object that uses the class will be considered an image in PNG format.

For any experienced Photoshop user, I'm sure you're familiar with creating a new image and selecting it's width and height dimensions. That's the kind of thing I wanted to implement in my module when creating a new image. You manually select the dimensions yourself. Of course, I also wanted to implement basic colors already prepackaged when the class is stored anew. Much like playing in Microsoft Paint when the premade colors are at the bottom. All this must be done in the CONSTRUCT of the class.

The construct of the class is a function in which when an object is created, everything and anything inside the construct is done first. Kind of like when you install a program to your computer, it move and copy files first, then let you use the program.

But before beginning the class, variables have to be set before programming. You can call these variables global throughout the entire class because they work entirely in the class itself. And they're also not called like any other variable, they have to be called through a pseudo variable, $this. $this stands for the object itself, when it is created.

Let's create our variables. We need to store the width and height (let's call them x and y), and an array to store our multitudes of colors. Of course we also need to store our image somewhere, so we'll save that as a 'image'.

I'll also have to go over something called 'visibility modifiers' at the end of this code, so just go along for the most part and don't question where it says 'public', 'private' or 'protected'.

Make this a local PHP file somewhere on your server.

Code:
<?php
class phpDraw{
protected 
$x;
protected 
$y;
private 
$image;
protected 
$colors;

function 
__construct($width,$height){
$this->$width;
$this->$height;
$this->image imagecreate($this->x,$this->y);
$this->colors["black"] = imagecolorallocate(0,0,0);
$this->colors["white"] = imagecolorallocate(255,255,255);
}

function 
fill($color_name$x$y){
imagefill($this->image,$x,$y,$this->colors[$color_name]);
}

function 
output(){
header("Content-type: image/png");
imagepng($this->image);
}
?>



That's the first part of our module! Testing it now like I did in the previous code will now reveal a - black box of death. Congratulations, you created your first black hole.

Okay, so I admit, this isn't easy to take in. When I started learning OOP, it was not the most fun thing in the world. Now, I'm actually dedicated to OOP. But that's me.

Let's go over some subjects that need explainin'.

$this as I mentioned is a pseudo variable for the entire object itself. Declaring $this means you're referencing to the object itself, not any specific data. However, after using $this and combining ->, we can point to specific variables and functions. Like I did with in the constructor and $this->x = $width, we're using $this to access the object, and then ->x to point at the variable we created at the top of the class, $x. $this->x points to the $x variable we created. We cannot access that variable by normally calling $x, instead we use $this because the $x is built into the object.

If you remember the first code I displayed, imagecolorallocate is a function to, well, allocate colors to a variable. I used a single variable, $colors in the object, as an array to store multiple imagecolorallocates depending on their name. This is my method for using the objects. That way, when the identified array is called back, imagecolorallocate will pop up. Such as black was called in the previous codes.

Looking back at the fill() function we created, we needed a $color_name. At this point we can only use "black" or "white" because that's all we created. Using anything else will just give us an error because we didn't properly declare other colors.

--
That should do it just about for now. Obviously this needs major editing since this is from August 2008.
« Last Edit: August 01, 2009, 03:56 PM by Steve »
Pages: [1]
 
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.10 | SMF © 2006-2008, Simple Machines LLC Valid XHTML 1.0! Valid CSS!