Wednesday, 10 February 2016

How to Create Thumbnail Images of uploaded image using PHP

How to Create Thumbnail Images of uploaded image using PHP

This tutorial will describe how to create thumbnail images on the fly using PHP while uploading a image. With uploaded of single image you can create different thumbnail with different size of same Image. Here you will Learn how to process a images and create their thumbnails.

System Requirements:
  1. Work for PHP only.
  2. GD library must be installed which you can check with phpinfo() with version of >=GD 2.0.1
  3. Uploading image size should be less than 2MB but if you want to increase then increase the value of "upload_max_filesize", "post_max_size" from php.ini

Code Snippets
   function resize($width, $height, $imageName,$extension) {
        if(empty($width) || empty($height) || empty($imageName) || empty($extension)){
            die('Required parameter is missing');
        }
        $docRoot = getenv("DOCUMENT_ROOT");
        
        /* Get original image x y */
        $tmpNM = $_FILES['files']['tmp_name'];
        
        list($w, $h) = getimagesize($_FILES['files']['tmp_name']);
        /* calculate new image size with ratio */
        $ratio = max($width / $w, $height / $h);
        $h = ceil($height / $ratio);
        $x = ($w - $width / $ratio) / 2;
        $w = ceil($width / $ratio);
        /* new file name */
        $path = $docRoot . '/images/thumb/' . $imageName;

        /* read binary data from image file */
        $imgString = file_get_contents($_FILES['files']['tmp_name']);
        /* create image from string */
        $image = imagecreatefromstring($imgString);
        $tmp = imagecreatetruecolor($width, $height);
        imagecopyresampled($tmp, $image, 0, 0, $x, 0, $width, $height, $w, $h);
        $fileTypes = array('jpg', 'jpeg', 'jpe', 'png', 'bmp', 'gif'); // File extensions
        /* Save image */
        switch ($extension) {
            case 'jpg':
            case 'jpeg':
            case 'jpe':
                imagejpeg($tmp, $path, 100);
                break;
            case 'png':
                imagepng($tmp, $path,0);
                break;
            case 'gif':
                imagegif($tmp, $path, 100);
                break;
            case 'bmp':
                imagewbmp($tmp, $path);
                break;
            default:
                exit;
                break;
        }
        return $path;
        /* cleanup memory */
        imagedestroy($image);
        imagedestroy($tmp);
    }
This function is able to create multiple thumbnail with different sizes of uploaded images.

How to  create thumbnail of 50pxX50px image?
    
    $w='50'; //New image width
    $h='50'; //New image height
    $imageName=$w.'_'.$h.'_'.$_FILES['files']['name']; //Uploaded file Name
    $imageExtensionArray = explode(".", $_FILES['files']['name']);
    $extension=$imageExtensionArray[1];
    $this->resize($w, $h,  "{$imageName}",$extension);

How to create thumbnail of 100pxX100px image?
$w='100'; //New image width
$h='100'; //New image height
$imageName=$w.'_'.$h.'_'.$_FILES['files']['name']; //Uploaded file Name
$imageExtensionArray = explode(".", $_FILES['files']['name']);
$extension=$imageExtensionArray[1];
$this->resize($w, $h,  "{$imageName}",$extension);

Saturday, 30 January 2016

What is Zend Engine in PHP

Zend Engine is used internally by PHP as a complier and runtime engine. PHP Scripts are loaded into memory and compiled into Zend opcodes. These opcodes are executed and the HTML generated is sent to the client.

What is Zend Engine



The Zend Engine is an open source scripting engine, commonly known for the important role it plays in the web automation language PHP. It was originally developed by Andi Gutmans and Zeev Suraski while they were students at the Technion - Israel Institute of Technology.