FREE PHP Scripts and Snippets

Ready to use Free PHP scripts and snippets to use in your projects.
All Free Scripts & Snippets Complete PHP Scripts

PHP Create thumbnail of an image

>> Share on Facebook & Impress your friends <<

PHP Script to create thumbnail of any image (.jpg, .png and .gif). This create thumbnail function is ready to use. Just provide desired parameters and it will do the magic. It uses GD library for thumbnail generation.

<?php

/*
Title: Create thumbnail of an image in PHP

PHP SCRIPT to create thumbnail of any image. This works for .jpg, .gif and .png files as well. 

$source  parameter expects path & file name of original image
$thumbnail_path  expects the path & file name where thumbnail is to be stored
$thumbnail_width is the width of the resulting thumbnail
$thumbnail_height is the height of the thumbnail

*/
function createImageThumbnail($source, $thumbnail_path, $thumbnail_width, $thumbnail_height, $background=false) {
	
	// Get dimensions of original image
    list($image_width, $imagel_height, $original_type) = getimagesize($source);
	
	// Set dimensions of the thumbnail
    if ($image_width > $imagel_height) {
        $new_width = $thumbnail_width;
        $new_height = intval($imagel_height * $new_width / $image_width);
    } else {
        $new_height = $thumbnail_height;
        $new_width = intval($image_width * $new_height / $imagel_height);
    }
	
	
    $dest_x = intval(($thumbnail_width - $new_width) / 2);
    $dest_y = intval(($thumbnail_height - $new_height) / 2);


// Depending on the fact if original image is GIF, JPEG or PNG, we use respective GD functions
    if ($original_type === 1) {
        $imgt = "ImageGIF";
        $imgcreatefrom = "ImageCreateFromGIF";
    } else if ($original_type === 2) {
        $imgt = "ImageJPEG";
        $imgcreatefrom = "ImageCreateFromJPEG";
    } else if ($original_type === 3) {
        $imgt = "ImagePNG";
        $imgcreatefrom = "ImageCreateFromPNG";
    } else {
        return false;
    }


// Load the original image
    $old_image = $imgcreatefrom($source);
    $new_image = imagecreatetruecolor($thumbnail_width, $thumbnail_height); // creates new image, but with a black background

    // If background color parameter is provided, then use it
    if(is_array($background) && count($background) === 3) {
      list($red, $green, $blue) = $background;
      $color = imagecolorallocate($new_image, $red, $green, $blue);
      imagefill($new_image, 0, 0, $color);
	  
    // For PNG files use, we make their backgroung transparent
    } else if($background === 'transparent' && $original_type === 3) {
      imagesavealpha($new_image, TRUE);
      $color = imagecolorallocatealpha($new_image, 0, 0, 0, 127);
      imagefill($new_image, 0, 0, $color);
    }

// Create final thumbnail image.
    imagecopyresampled($new_image, $old_image, $dest_x, $dest_y, 0, 0, $new_width, $new_height, $image_width, $imagel_height);
    $imgt($new_image, $thumbnail_path);
    return file_exists($thumbnail_path);
}  // End of Create thumbnail of an image function 


// This is path of the image file for which thumbnail is to be generated
// Make sure image one.jpg exists in folder image. 
$source="image/one.jpg";
// Folder named thumb should exist. Because thumbnail image will be stored here
$destination="thumb/one.jpg";

// Set height and width of the thumbnail
$thumbnail_width=200;
$thumbnail_height=200;

// Call the function with the above paraters. It will just create an thumbnail and on success return and echo 1.
echo createImageThumbnail($source, $destination, $thumbnail_width, $thumbnail_height);

?>


Request Custom Snippet

Just $5 Onwards

 

Previously Ordered Snippets

PHP Script that can take folder name as parameter and generate thumbnails of all images in folder into another folder. Original images should remain as it is.
Price $10 Order Now

I want PHP function to generate thumbnail of an image on the fly and server it to the web page to save bandwidth.
Price $5 Order Now

I want PHP snippet to upload an image to server and then create its thumbnail.
Price $5 Order Now

I want all images in a folder to be watermarked with another image in bottom right corner. No thumbnail creation is needed.
Price $5 Order Now

I want a script to find the dominant color in an image.
Price $5 Order Now

I want PHP function that returns the size of any jpeg, png or gif image.
Price $5 Order Now

PHP script to create a pallet style image using the colors provided in #ffffff format.
Price $5 Order Now

I want php image rotate script which saves the image with transparent background.
Price $5 Order Now

I want php script to crop an image to cut 5 pixel from all 4 sides and then save the resulting images overwriting the original image.
Price $5 Order Now

I want PHP Script to create blur version of an image with same size and format.
Price $5 Order Now

Random PHP Scripts and Snippets

Create URL Slug from text string [PHP]

PHP Script to create URL from string. It retails only URL Safe characters in the strings. So, now easily create...

PHP Code to list all files in a folder

This PHP Code snippet lists all files in a folder and hyperlinks them accordingly. Very clean, easy to use function....

[PHP] Time ago calculation function

It is a very nice PHP time ago function. You gave pass any date to it and it will tell...

Extracting all numbers from a String [PHP]

This PHP function extracts all numbers from a String and prints them as a comma separated list of numbers. Ready...

Detect Valid Email address with PHP

A very simple php snippet to check if an email id is valid or not. If the email address is...

[PHP] generate keywords from text function

This is a simple function written in PHP to generate keywords from any text. Just pass any free text to...

PHP - Extract all links from a web page

PHP Script to extract all links from a web page. This PHP Snippet reads the contents of a web page...

Make URLs in plain text clickable [PHP]

This is a simple PHP function to parse text provided and automatically hyperlink the links in the text.