If you have a website where you let your users to upload images, you know that even the small images (around 100x100) can reach 50 - 100KB. This is way too much, when you could simply downsize the image to around 1 - 5KB, this is only the 1 - 5% of the size of the original image. Just imagine the benefits of downsizing 100.000 images... Bellow is an image before and after resizing:
Before 24.80KB
After 3.52KB
So let`s begin downsizing those images... The upload form upload-form.php:
enctype="multipart/form-data" - this line tells the server that you want to send a file MAX_FILE_SIZE - limits the maximum file size in bytes type="file" - this is the file selecting input
Checking the image First we need to check the file for 3 different problems: - it was submitted or not ? - it was uploaded ? - it is too big ? upload.php:
<?php if(isset($_FILES['theimage'])){ if($_FILES['theimage']['size'] < 1){ echo 'Upload error!'; } else if($_FILES['theimage']['size'] > 100000){ echo 'The image is too big!'; } } else { echo 'There is no image!'; } ?>
Check image type Now we will check the type of the file, because we will accept only .jpg .gif and .png. At this step we will use some GD, too, because we will put our image in a variable:
<?php if(isset($_FILES['theimage'])){ if($_FILES['theimage']['size'] < 1){ echo 'Upload error!'; } else if($_FILES['theimage']['size'] > 100000){ echo 'The image is too big!'; } else { switch($_FILES['theimage']['type']){ case 'image/gif': $image = imagecreatefromgif($_FILES['theimage']['tmp_name']); break; case 'image/jpeg': case 'image/pjpeg': $image = imagecreatefromjpeg($_FILES['theimage']['tmp_name']); break; case 'image/png': $image = imagecreatefrompng($_FILES['theimage']['tmp_name']); break; } if(!isset($image)){ echo 'Only .gif, .jpg or .png images are allowed!'; } } } else { echo 'There is no image!'; } ?>
Working with transparent images We will downsize the images by converting it to .jpg. The jpg images can not be transparent and when the user uploads a transparent image, the transparent part will be painted in black. To make the transparent part white we need to create a white image with the same size as our uploaded image and then copy the uploaded image over the white image:
<?php if(isset($_FILES['theimage'])){ if($_FILES['theimage']['size'] < 1){ echo 'Upload error!'; } else if($_FILES['theimage']['size'] > 100000){ echo 'The image is too big!'; } else { switch($_FILES['theimage']['type']){ case 'image/gif': $image = imagecreatefromgif($_FILES['theimage']['tmp_name']); break; case 'image/jpeg': case 'image/pjpeg': $image = imagecreatefromjpeg($_FILES['theimage']['tmp_name']); break; case 'image/png': $image = imagecreatefrompng($_FILES['theimage']['tmp_name']); break; } if(!isset($image)){ echo 'Only .gif, .jpg or .png images are allowed!'; } else { $size = getimagesize($_FILES['theimage']['tmp_name']); $background = imagecreatetruecolor($size[0],$size[1]); $white = imagecolorallocate($background,255,255,255); imagefill($background,0,0,$white); imagecopy($background,$image,0,0,0,0,$size[0],$size[1]); } } } else { echo 'There is no image!'; } ?>
Downsize the image This is the final step, when we save the image in .jpg format:
<?php if(isset($_FILES['theimage'])){ if($_FILES['theimage']['size'] < 1){ echo 'Upload error!'; } else if($_FILES['theimage']['size'] > 100000){ echo 'The image is too big!'; } else { switch($_FILES['theimage']['type']){ case 'image/gif': $image = imagecreatefromgif($_FILES['theimage']['tmp_name']); break; case 'image/jpeg': case 'image/pjpeg': $image = imagecreatefromjpeg($_FILES['theimage']['tmp_name']); break; case 'image/png': $image = imagecreatefrompng($_FILES['theimage']['tmp_name']); break; } if(!isset($image)){ echo 'Only .gif, .jpg or .png images are allowed!'; } else { $size = getimagesize($_FILES['theimage']['tmp_name']); $background = imagecreatetruecolor($size[0],$size[1]); $white = imagecolorallocate($background,255,255,255); imagefill($background,0,0,$white); imagecopy($background,$image,0,0,0,0,$size[0],$size[1]); $image = $background; $filename = explode(".",$_FILES['theimage']['name']); $filename = $filename[0]; imagejpeg($image,$_SERVER['DOCUMENT_ROOT'] . '/thumbnails/' . $filename . '.jpg',90); imagedestroy($image); echo 'Your image: <img src="/thumbnails/' . $filename . '.jpg" />'; } } } else { echo 'There is no image!'; } ?>
If you have any questions please don`t hesitate to ASK!
Finally my private chat room is ready! I`we used jQuery, PHP and 2 MySQL tables to write this script. This is the first version, this is some kind of beta version, because I hadn`t test it yet, because it needs at least 2 users to work... Here is a live demo, have fun with it and please tell me about the errors:
NOTES: Everything is refreshed each 15 seconds. An user is logged out after 10 minutes of inactivity. Each message is kept for 30 minutes. An users name is highlighted with orange if the user sent you a message. The selected username is highlighted with red.
As I said, we will need two mysql tables: one to hold the users (username, ip, last activity date) and one for the messages (from, to, message, date). Here is my code to create the tables: table1.php
<?php mysql_connect("host", "username", "password") or die(mysql_error()); mysql_select_db("database") or die(mysql_error()); mysql_query("CREATE TABLE chatusers( username VARCHAR(30), ip CHAR(15), lastactive INT UNSIGNED, PRIMARY KEY(username) )") or die(mysql_error()); echo "Table Created!"; ?>
table2.php
<?php mysql_connect("host", "username", "password") or die(mysql_error()); mysql_select_db("database") or die(mysql_error()); mysql_query("CREATE TABLE messages( messagefrom VARCHAR(30), sendto VARCHAR(30), message VARCHAR(255), date INT UNSIGNED )") or die(mysql_error()); echo "Table Created!"; ?>
It`s hard to explain how it works... but I will try to. At the start if your ip is not in the database you will be asked for an username and you will be added to the database and a list with the online users is generated by the onlineusers.php file. When you click on somebody the messages sent by that person to you and the messages sent by you to that person are shown by the show-messages.php file. When you write a message and press the send button then your message will be sent to the selected user by saving it on the messages table.
Why to rewrite your URL`s ? The main reason is because static URL`s (the second URL is static) are indexed more faster than dynamic URL`s (like the first one), because from a static URL is easier to understand what the page is about both for search engines and your visitors. When using dynamic URL`s there is an another problem: search engines are reading only the beginning of the long URL`s, so if your addresses are different only at the end, than search engines will see the same URL for all of your pages and from this reason none will be indexed.
How this works ? We will use Apache mod_rewrite. The mod_rewrite transforms back the second URL into the first URL, so the server will access the "ugly" address, but the visitors and the search engines will see the "clear" one. mod_rewrite simply rewrites the URL`s what meets specific conditions into addresses what are understood by the server.
Let`s rewrite it! The code what rewrites the website address needs to be placed in your .htaccess file (if you don`t have it in your root folder, create it), the code is very short:
RewriteEngine on RewriteRule ^tutorials/(.*)/(.*)-([0-9]+)/?$ /tutorials.php?id=$3&title=$2&category=$1
Take it apart! The first line: RewriteEngine on simply does what it says, it starts the rewrite engine. Now let`s take apart the second line:
RewriteRule - this tells the server about the rule to follow when rewriting the URL ^ - this exponential sign means the start of the URL: http://www.mywebsite.com/ tutorials - this word is simply added to the website adress / - this character separates the directories (.*) - this tells the server that here will be placed some data (any kind of characters) (e.g.: php) - - this character will be simply added to the website adress ([0-9]) - this tells the server that here will be placed a digit (e.g: 1) + - the plus sign tells the server that here can be more digits (e.g.: 123) ? - this sign tell the server that the character in it`s front it`s not required (so the URL will work with and without the last "/") $ - the dollar sign tells the server that here is the end of the rule
The last part of the line is the old address, the value of each variable (id, title and category) are changed to $3, $2 and $1. mod_rewrite takes these variables and places them in their new position in the new URL. In the first place for data (the first (.*)) it places the $1`s value (the category value), in the second place the value of $2 and so on. If you want to change the order just change the $1, $2 and $3 order, but be careful to set the right data type in the new URL.
I know that it`s not easy, if you have any question please don`t hesitate, ASK ME! And I will try to answer as fast as I can. You can for example write me in a comment your URL and how you want to transform it and I will try to make it ;)
I`m very happy, today my first AdSense payment has arrived. When I`we opened this website I wasn`t even sure if AdSense will send me the money if I reach the threshold. In the first 1 - 2 months it seems that I will never reach the 70€, but since that time my incomings are slowly increasing. I`we worked hard for this money but it worth it! I`we made an image about the check, bellow the check is my adsense pin:
I know that 70€ is a small amount but I`m sure that I will get more checks like this or even with higher amount of money.
Most of my traffic comes from tutorial sites and only a small percentage from google so I want to search engine optimize this website, this would bring more visitors, more visitors means more money and a larger community!
Oh and Thanks for all of my visitors, you all have contributed to this!
This is my first 2D game. It`s written in C++ using DirectX9 (Direct3D and DirectInput). This is a very simple Snake game. At the beginning the snake is only 1 square long and when it "eats" the target it`s length is encreased by 1 square. The snake can have a maximum of 20 squares long (each square is 20px), after it reaches this maximum length the snake is moving faster and faster. If the head of the snake hit`s it`s tail the game is over: