I currently live in Switzerland, but I've spent most of my life in Turkey. I have graduated from political science and international relations from Bogazici University in Istanbul, and next semester I'm restarting an informatics bachelor in Switzerland. I have a strong passion for programming and lately I started to like taking photos and sharing them. I was born in 89. If you want to learn more about me, or what I do, or where I am, just continue to check my website!

Version History

  • v.1.2 Updated the layout.
  • v.1.1 Added disqus, tags, search engine and version box.
  • v.1.0 Uploaded the website.

PHP Image Resizing Using GD
10 May

I was struggling with a PHP module that I've created which basically lets the user manage images. They can upload, remove, select and list all images in a specific folder. To be honest, I was doing this for my mom :) I am scripting a website for her where she can sell her paintings. The problem is that I don't want her to resize manually the images before uploading since it is an overwork for her. So I've researched and found this beautiful script which automatically resizes images to the given size. I used it along with the file upload function I've written, so it became pretty handy. Anyways, here is the code that resizes the images.

Make sure to have read the file upload function and make the relevant checks before uploading files (like checking the allowed types, the size etc etc...)

// Check if 
// - tmp_name is not empty (since if uploaded file is larger than php directives then it is empty)
// - gd package is loaded (on some platforms gd is not loaded so check it before running the code)
if (!empty($_FILES['file']['tmp_name']) && extension_loaded('gd') && extension_loaded('gd2'))
    // Since the following code is error-prone make sure to use try-catch block.
        $extension = strtolower(end(explode('.', $_FILES['file']['name'])));
        if ($extension == "jpg" || $extension=="jpeg")
            $uploadedfile = $_FILES['file']['tmp_name'];
            $src = imagecreatefromjpeg($uploadedfile);
        else if ($extension == "png")
            $uploadedfile = $_FILES['file']['tmp_name'];
            $src = imagecreatefrompng($uploadedfile);
            $src = imagecreatefromgif($uploadedfile);

        list($width, $height) = getimagesize($uploadedfile);
        $newwidth = 800; // Change this value to whatever you want
        $newheight = ($height / $width) * $newwidth;

        $tmp = imagecreatetruecolor($newwidth, $newheight);

        $filename = '/path/to/images/' . $_FILES['file']['name'];

        imagecopyresampled($tmp, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
        imagejpeg($tmp, $filename, 100);

    catch (Exception $e)
        echo $e->getMessage();

If for whatever reason the gd library is not enabled and if you are using ubuntu the following code will enable it:

sudo apt-get install php5-gd

Then make sure to change the php.ini so that you allow large files to be uploaded. If you leave small values then most probably the tmp_name will result empty since php will not upload the file to server and the function fill fail. Again if you're using ubuntu to locate your php.ini:

sudo find / -name php.ini

Then most probably you will see that it is located under /etc/php5/apache2:

sudo vim /etc/php5/apache2/php.ini

And change the following values:


Finally restart apache2 and you're done:

sudo service apache2 restart

As always, I am ready to answer your questions! Have fun and happy coding.

Drop me an Email!