PHP Thumbnail Image creation Script with Example

Here, we are using imagecopyresampled to resize the image. It will resize with smoothing of image in less CPU usage.
And it results better than imagecopyresized,

Following working example is to create thumbnail images with any desired size,

Thumb Image create declaration follows,

$old_path = './images/image_friends.jpg';
$new_path = './images/thumb_image_friends.jpg';
$new_width = '120';
$new_height = '80';
$squareSize = '';

$thumbnail_status = create_thumb($old_path, $new_path, $new_width, $new_height, $squareSize);

if ($thumbnail_status) {
    echo 'Thumbnail created:' . $new_path;
} else {
    echo 'Failed!';
}
Note:Set write permission for the directory images.

Thumb Image create definition follows,

function create_thumb($old_path, $new_path, $new_width, $new_height, $squareSize = '') {

    $source_image = FALSE;
    $src_h = 0;

    #It is to check the file type of the image, which is given below....
    $file_type = image_type($old_path);

    if (preg_match("/jpg|JPG|jpeg|JPEG/", $file_type)) {
        $source_image = imagecreatefromjpeg($old_path);
    } elseif (preg_match("/png|PNG/", $file_type)) {
        $source_image = imagecreatefrompng($old_path);
    } elseif (preg_match("/gif|GIF/", $file_type)) {
        $source_image = imagecreatefromgif($old_path);
    }

    if ($source_image == FALSE) {
        $source_image = imagecreatefromjpeg($old_path);
    }

    $orig_w = imageSX($source_image);
    $orig_h = imageSY($source_image);

    if ($orig_w < $new_width && $orig_h < $new_height) {
        $desired_width = $orig_w;
        $desired_height = $orig_h;
    } elseif ($orig_w < $orig_h) {
        #to avoid image stretch, here reduced height and height(y) starting position..
        $src_h = $orig_h / 7;
        $orig_h = $orig_h * .8;
        $desired_width = $new_width;
        $desired_height = $new_height;
    } else {
        $desired_width = $new_width;
        $desired_height = $new_height;
    }

    if ($squareSize != '') {
        $desired_width = $desired_height = $squareSize;
    }

    #create a new, virtual image....
    $virtual_image = imagecreatetruecolor($desired_width, $desired_height);

    #for PNG background white.....
    $kek = imagecolorallocate($virtual_image, 255, 255, 255);
    imagefill($virtual_image, 0, 0, $kek);

    if ($squareSize == '') {
        #copy source image at a resized size.....
        imagecopyresampled($virtual_image, $source_image, 0, 0, 0, $src_h, $desired_width, $desired_height, $orig_w, $orig_h);
    } else {
        $wm = $orig_w / $squareSize;
        $hm = $orig_h / $squareSize;
        $w_height = $squareSize / 2;

        if ($orig_w > $orig_h) {
            $adjusted_width = $orig_w / $hm;
            $half_width = $adjusted_width / 2;
            $int_width = $half_width - $w_height;
            imagecopyresampled($virtual_image, $source_image, -$int_width, 0, 0, 0, $adjusted_width, $squareSize, $orig_w, $orig_h);
        } elseif (($orig_w <= $orig_h)) {
            $adjusted_height = $orig_h / $wm;
            $half_height = $adjusted_height / 2;
            imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $squareSize, $adjusted_height, $orig_w, $orig_h);
        } else {
            imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $squareSize, $squareSize, $orig_w, $orig_h);
        }
    }
    if (@imagejpeg($virtual_image, $new_path, 90)) {
        imagedestroy($virtual_image);
        imagedestroy($source_image);
        return TRUE;
    } else {
        return FALSE;
    }
}

Image MIME type check  snippet follows,

function image_type($path) {
    if ($image_info = @getimagesize($path)) {
        $img_type = str_replace('image/', '', $image_info['mime']);
        return ($img_type == 'jpeg') ? 'jpg' : $img_type;
    }
}

PHP Files Images Posted on 2016-04-29 08:59:15