lorz’s blog

blog • programming • web design • tutorials • seo

Another version of Wordpress? No way!

I see that Wordpress is trying to annoy me with their version releases. I upgraded to the latest version 3 weeks ago and now… again? No way I’m gonna upgrade again.

Instead, I will create a custom blog script and I’ll import all posts from this blog to the other one. But this will take some time, so you shouldn’t expect any changes right away!

Why am I doing this? Because I’m tired of using somebody else’s script, especially when they require to upgrade to the latest version “because my site could get hacked and spammed and other bad bad things might happen”. I prefer to use my own script, to know how to modify it, how to use it and so on. And plus, it’s mine, so it’s the best!!

Some voices would say: “but man, wordpress has a lot of features and designs and plugins!”

True, but you don’t use all the plugins and themes available, right? You use just a few. Well, those “few” can be coded by myself. And so the theme, I prefer to be original. And to be honest, I hate the template which I currently have on this blog. :)

Automatically resize images in PHP - Script tutorial

Have you ever needed a simple script to automatically resize your uploaded images in PHP?

For example, suppose that you have a database containing some websites and that you want to store a thumbnail for every website. You take a screenshot of the website, but you have at a higher dimension. Suppose you want to resize it to 110×70px but your don’t want to manually resize it in your image editor. Well, let’s make a simple function in PHP so we can save our precious time.

Let’s start by first uploading and moving your image as it is:

move_uploaded_file ($_FILES['thumbnail']['tmp_name'], $_FILES['thumbnail']['name']);

You moved your file. Now we use a function like imagepng(), together with our custom resize_image() function. Remember that if your uploaded file is in PNG format, you must use imagepng() otherwise it won’t work. This applies for other extensions too (like GIF, JPG, etc.).

imagepng (resize_image ($_FILES['thumbnail']['name'], ‘png’, 110, 70), “/thumbnails/thumbnail-resized.png”);

If you do not plan to use the original, unresized image, you can delete it:

@unlink ($thumbnail['name']);

I placed the “@” symbol to suppress the errors (if any).

And now we create our resize_image() function. We have four arguments: the filename ($_FILES['thumbnail']['name']), the extension (png|gif|jpg), width and height:

function resize_image ($filename, $extension, $width, $height) {

Based on the file’s extension, we create our image:

if ($extension == “jpg” || $extension == “JPG”) {
$image = imagecreatefromjpeg ($filename);
} elseif ($extension == “png” || $extension == “PNG”) {
$image = imagecreatefrompng ($filename);
} elseif ($extension == “gif” || $extension == “GIF”) {
$image = imagecreatefromgif ($filename);
}

We find out the actual width and height of the image and we also put our new values in some variables:

$actual_width = imagesx ($image);
$actual_height = imagesy ($image);
$new_width = $width;
$new_height = $height;

Now we create our new resized image (a blank one) and after that we copy the old one, resizing it at the same time.

$new_image = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($new_image, $image, 0, 0, 0, 0, floor($new_width), floor($new_height), $actual_width, $actual_height);

We finally return the new image created and finalize the function.

return $new_image;
}

That’s it! Now you have a fully working image-resizing PHP script!