Php gd library,resize image,image manipulation

Php gd library,resize image,image manipulation

A PHP image resizing script that will resize JPG and PNG images. Specify a width OR a height OR both. Images are resized proportionally if only a width or a height is specified. Great for thumbnails.
It works with both JPG and PNG images, and will resize proportionally to height OR width, and also resize to a specified height and width. So if you have an assortment of images that you all want to be 300 pixels wide, but have varying heights, this script handles it nicely (of course it works if you want to specify a height but not a width as well!).I’ve needed a PHP image resizing script on various projects, and I always seem to spend far too long looking for a simple one to use that just works. This happened one too many times, so I’ve written my own image resizer script.

imgresize

<?php
$Source_file = 'prem.jpg';
$Destination_file = 'resize/prem.jpg';
$width = 1800;
$height = 1200;
resizeMyImage($Source_file, $Destination_file, $width, $height);
/*****************thum image****/
$thumb = 'thumb/premthumb.jpg';
$thumwidth = 250;
$thumheight = 250;
resizeMyImage($Source_file, $thumb, $thumwidth, $thumheight);
/*************400*400 image*************/
$img400 = 'image400/prem400.jpg';
$width400 = 400;
$height400 = 400;
resizeMyImage($Source_file, $img400, $width400, $height400);

/**********************whater mark************************/
$stamp = imagecreatefrompng('fs-icon.png');///////////whater mark png image
$im = imagecreatefromjpeg($Destination_file);

// Set the margins for the stamp and get the height/width of the stamp image
$marge_right = 10;
$marge_bottom = 10;
$sx = imagesx($stamp);
$sy = imagesy($stamp);
 
$img = imagecopy($im, $stamp, imagesx($im) - $sx - $marge_right, imagesy($im) - $sy - $marge_bottom, 0, 0, imagesx($stamp), imagesy($stamp));
imagejpeg($im,$Destination_file);

imagedestroy($im);



function resizeMyImage($file, $destination, $w, $h) {
    //Get the original image dimensions + type
    list($source_width, $source_height, $source_type) = getimagesize($file);
 
    //Figure out if we need to create a new JPG, PNG or GIF
    $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION));
    if ($ext == "jpg" || $ext == "jpeg") {
        $source_gdim=imagecreatefromjpeg($file);
    } elseif ($ext == "png") {
        $source_gdim=imagecreatefrompng($file);
    } elseif ($ext == "gif") {
        $source_gdim=imagecreatefromgif($file);
    } else {
        //Invalid file type? Return.
        return;
    }
 
    //If a width is supplied, but height is false, then we need to resize by width instead of cropping
    if ($w && !$h) {
        $ratio = $w / $source_width;
        $temp_width = $w;
        $temp_height = $source_height * $ratio;
 
        $desired_gdim = imagecreatetruecolor($temp_width, $temp_height);
        imagecopyresampled(
            $desired_gdim,
            $source_gdim,
            0, 0,
            0, 0,
            $temp_width, $temp_height,
            $source_width, $source_height
        );
    } else {
        $source_aspect_ratio = $source_width / $source_height;
        $desired_aspect_ratio = $w / $h;
 
        if ($source_aspect_ratio > $desired_aspect_ratio) {
            /*
             * Triggered when source image is wider
             */
            $temp_height = $h;
            $temp_width = ( int ) ($h * $source_aspect_ratio);
        } else {
            /*
             * Triggered otherwise (i.e. source image is similar or taller)
             */
            $temp_width = $w;
            $temp_height = ( int ) ($w / $source_aspect_ratio);
        }
 
        /*
         * Resize the image into a temporary GD image
         */
 
        $temp_gdim = imagecreatetruecolor($temp_width, $temp_height);
        imagecopyresampled(
            $temp_gdim,
            $source_gdim,
            0, 0,
            0, 0,
            $temp_width, $temp_height,
            $source_width, $source_height
        );
 
        /*
         * Copy cropped region from temporary image into the desired GD image
         */
 
        $x0 = ($temp_width - $w) / 2;
        $y0 = ($temp_height - $h) / 2;
        $desired_gdim = imagecreatetruecolor($w, $h);
        imagecopy(
            $desired_gdim,
            $temp_gdim,
            0, 0,
            $x0, $y0,
            $w, $h
        );
    }
 
    /*
     * Render the image
     * Alternatively, you can save the image in file-system or database
     */
 
    if ($ext == "jpg" || $ext == "jpeg") {
        ImageJpeg($desired_gdim,$destination,100);
    } elseif ($ext == "png") {
        ImagePng($desired_gdim,$destination);
    } elseif ($ext == "gif") {
        ImageGif($desired_gdim,$destination);
    } else {
        return;
    }
 
    ImageDestroy ($desired_gdim);
}


Share Button

About admin

Leave a Reply

Your email address will not be published. Required fields are marked *

*

Author: pramod kumar kumawat Mob: 9269727062
Free WordPress Themes - Download High-quality Templates