Thursday, October 30th, 2008
Today, I will walk you through the process of using the GD Library. This library is installed on most PHP servers, and works really well when you want to allow someone to upload an image of any size, and then resize it into a proper thumbnail.
First of lets create a simple form that allows us to upload our image file in a file called image.php
<h1>Upload Image</h1>
<p>
Below will be a preview of our image once uploaded:
</p>
<img src="images/thumb.jpg" alt="Preview Will Be Shown Here" />
<form action="image.php" enctype="multipart/form-data" method="post">
<label for="image">Image</label>
<input id="image" name="image" type="file" />
<input name="submit" type="submit" value="Upload!" />
</form>Here we have setup that thumb.jpg will be shown. Of course we have not yet uploaded an image so nothing will show up here yet. We have set the action to be the same file, so it will just keep posting back the information to itself. Another important attribute of the form that we need to assign is the enctype. If this is not here, it will not send our image across properly.
We now need to do some fancy PHP when the above form is submitted. Add this above the form:
< ?php
if( isset($_POST['submit']) )
{
// gets the variable needed from our type=file field with a name of image
$image = $_FILES['image']['tmp_name'];
// makes sure we have browsed to an image and not left it blank
if( !empt y($image) )
{
// put the contents of the image into a php variable
$src = imagecreatefromjpeg($image);
// get the original images width and height
list($width, $height) = getimagesize($image);
// calculate the new width and height that we want our image to be
// height is calculated to keep the same ratio with our new width
$newwidth = 100;
$newheight = ($height/$width)*$newwidth;
// create a holder for our new image, and copy the original with new dimensions
$tmp = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
// lastly output the new image to our directory structure
imagejpeg($tmp,'images/thumb.jpg',100);
}
}
?>
One last thing to do is make sure your “images” directory is writable. Now open up your image.php file and try it out. It should now allow you to resize any image into a great looking thumbnail!
Tags: image manipulation, PHP
Posted in Web Programming | 2 Comments »
Wednesday, October 29th, 2008
This is a question that seems really odd to me, and I’m sure it does to you as well. I have actually found out that it does have some affect on Photoshop for whatever reason.
First let me brief you on the setup I had that brought up this issue. Earlier this year I bought myself a laptop (Dell XPS M1530) and connected it to my wireless network. I then setup my desktop computer to share its printers with the surrounding devices. It all worked fine even thought I noticed it was a bit slow when printing to the network printer.
I know that network printing is slower than local printing, so I didn’t think too much about it. I then had another problem that I thought was a separate issue from the slow printing. My Photoshop (CS3) was loading fast, but as soon as I opened an image it would freeze for a minute or two. I knew that the problem was not in my computers hardware as it is loaded with 4GB memory.
This is when I started hunting for why my Photoshop was slow when opening an image. I found out that it was an issue with my default printer being on a network. I put Adobe PDF as my default printer, and now Photoshop loaded images quick and without freezing.
I later found out that all I had to do to speed up my network printing was configure it differently. In essence, I tricked my computer into thinking it is local, but really it is on the network. Below are the steps I took:
I hope this article helps some of you out there with the same problem. Probably isn’t an overly common problem, but drop me a line if this works for you!
Tags: network, printing
Posted in Software | 3 Comments »
Monday, October 27th, 2008
Can you remember the last time you were hacked? Was it a SQL Injection, a Denial Of Service (DoS), or even allowed complete control of your web server? All three of these attacks are well known and can be prevented using the appropriate measures. First of let’s start by describing a little bit about each one.
SQL injection is when you create a SQL statement that does not escape the data that it puts into the query. For example, if you use this as your code:
SELECT username, password
FROM users
WHERE user_id = {$_GET['user_id']}
This seems like a safe query right? Wrong, what if I would enter the following this… 5 OR 1
SELECT username, password FROM users WHERE user_id = 5 OR 1
We now might get a listing of all the users in are table as the OR 1 always matches.
Now let’s talk about a Denial Of Service Attack. These are attacks that really don’t allow the hacker to do anything exciting. All they do is knock your web server for extended periods of time. Some ways of accomplishing this is sending large data to the server, flooding the server with packets, etc…
What makes these attacks so dangerous is that a hacker may have thousands or more computers under control that can make these attacks simultaneously. You may ask how they get access to this many computers? Well they use machines like yours and mine… by infecting them with trojans, viruses, and worms. They can instruct these machines to all hit a web server and thus cause it to go down.
Lastly we talk about the most technical of them all. Getting down to bugs in the web server software, or even the web server operating system. We all know operating systems have bugs, and some of them can cause great problems. There is a framework out there that has a whole list of bugs with particular software, and operating systems. All you really need to do is select the machine you want to target with the vulnerability and click “Attack!” These attacks can completely compromise a system by sending back a remote shell (cmd in windows) that allows a hacker to do whatever they want. Delete files, install viruses, and track banking information are a few things they could do.
Now, I don’t tell you all this to scare you away from creating web apps, as there are ways to prevent the above from happening.
Please let me know any stories you have regarding the above attacks, and how you handled the situation. We all need to become more aware of the dangers that lurk on the World Wide Web.
Posted in Web Programming | 1 Comment »
Monday, October 27th, 2008
If you are a relatively new web developer classes and objects are probably one of those topics you have heard about, but don’t really have a grasp on. I know when I went to university very little time was spent on object orientation, which I really wished would have been covered. The majority, if not all programming languages support objects. It is a fundamental concept underlying the language. You most likely use objects without even knowing it!.
First of all let’s ask why should we use objects? Well the biggest reason is that it creates reusable code. If you create a ShoppingCart class once, you can reuse it in many of your other projects. How many times have you had to do a shopping cart? The logic from project to project is exactly the same, so you could reuse the class you have already created to speed up your turnaround time. Below I have shown how to create objects in a variety of languages:
PHP – $mybook= new Book();
Ruby – mybook= Book.new
Python – mybook= Book()
Java – Book mybook= new Book();
You can see that the above statements all instantiate an object. This means you use a class (blueprint) for which the basis of your object will be formed. Objects will have properties, and methods associated with them. For example let’s use an object of a book. A book has attributes such as number of pages, author, and category. These could be accessed in PHP using the following:
$mybook->numPages $mybook->author
Methods (functions) on the other hand are actions taken. So for example a book could close. In PHP this is represented by:
$mybook->close();
Now these concepts are fine, but in order for the above to work we need to create a blueprint for the Book class. Below is an example that maps the above logic:
class Book
{
var numPages;
var author;
var closed;
function __construct($author)
{
$this->closed = 0;
$this->author = $author;
}
function close()
{
$this->closed = 1;
}
}
We have introduced a few new things with the above code. As you can see we define object properties by coding:
var propertyName;
We then create methods (functions) inside our object. The __construct() function may look a bit strange. It is a predefined function that PHP automatically loads when new Book() is called. In this function we assign our properties values. The $this variable is visible anywhere inside our class. We tell our object to assign whatever author is passed into our function to our internal property. Below is how we would give our book an author and how we could print it to the screen:
$mybook = new Book('CSS: The Missing Manual');
echo $mybook->author;
We have covered some of the basics of PHP objects. The following are topics you can further research if you are interested!
Posted in Web Programming | 4 Comments »
Thursday, October 23rd, 2008
I was prompted to write about this because of a great article written by a fellow Internet Specialist, Steve Gomori. He took the same program that I did at the University of Winnipeg – Distance of Continuing Education. The post deals with the Online Databases, Perl, and PHP Course.
He mentions that the “smaller”, “trivial”, or “baby steps” of the script are brought to attention and tested on a regular basis. This I think is a critical phase that many educational programs and programmers leave out. Planning and debugging your scripts are crucial aspects of any web application. As one professor of mine has said, “There are many code monkeys out there for companies to hire, but what separates you from a high school programmer is your ability to plan, debug, and communicate your ideas effectively.”
Can any of you agree with the above statement? Debugging, and testing every single line of code may seem tedious at first, but in the end will actually save you time. Imagine writing 100 lines of code, just to find out that all you get is an error message. Now you have to retrace your steps through a lot of code, instead of just last couple lines of code.
Below I have listed several helpful debugging methods/tools:
If you have your own debugging techniques and/or tools for programming, share them in the comments below!
Tags: debugging
Posted in Web Programming | 2 Comments »
Twitter
Follow me on Twitter to keep up to date!
RSS Feed
Keep up with all of our updates by subscribing to our RSS feed!
FaceBook
Join our group on Facebook and become a fan of us!