php resizing image – How to Resize JPEG Image in PHP ?

php resizing image To resize an image according to one dimension (keeping aspect ratio): Get image resource id for the source image and Get resource id for target image layer, Resizing and reassembling and Save Resized Image into Target Location.

php resizing image | php resize img | php image resize function

We will be henceforth using PHP to decrease the image dimension and render. First pass in the image we want to resize in the class constructor, then define the width and height with the scale option of exact.

Loading the Image Before Resizing

//php resizing image
function init_profile_img($dpflnamenew, $type) {
    if( $type == IMAGETYPE_JPEG ) {
        $image = imagecreatefromjpeg($dpflnamenew);
    }
    elseif( $type == IMAGETYPE_PNG ) {
        $image = imagecreatefrompng($dpflnamenew);
    }
    elseif( $type == IMAGETYPE_GIF ) {
        $image = imagecreatefromgif($dpflnamenew);
    }
    return $image;
}

$dpflnamenew = "media/pakainfo.jpg";
list($width, $height, $type) = getimagesize($dpflnamenew);
$kim_img_nm = init_profile_img($dpflnamenew, $type);

Resize Image to Fixed Width and Height

//php resizing images
bool imagecopyresampled ( resource $dst_image , resource $src_image , int $dst_x , int $dst_y , int $src_x , int $src_y , int $dst_w , int $dst_h , int $src_w , int $src_h )

//php resize img
function resize_image($fresh_quick_w, $new_height, $image, $width, $height) {
    $organic_img_file = imagecreatetruecolor($fresh_quick_w, $new_height);
    imagecopyresampled($organic_img_file, $image, 0, 0, 0, 0, $fresh_quick_w, $new_height, $width, $height);
    return $organic_img_file;
}

$dpflnamenew = "media/pakainfo.jpg";
list($width, $height, $type) = getimagesize($dpflnamenew);
$kim_img_nm = init_profile_img($dpflnamenew, $type);
$organic_img_file = resize_image(280, 180, $kim_img_nm, $width, $height);

Resize Image to Fixed Width While Maintaining the Aspect Ratio

//php resize image
function resize_image_to_width($fresh_quick_w, $image, $width, $height) {
    $resize_ratio = $fresh_quick_w / $width;
    $new_height = $height * $resize_ratio;
    return resize_image($fresh_quick_w, $new_height, $image, $width, $height);
}

$dpflnamenew = "media/pakainfo.jpg";
list($width, $height, $type) = getimagesize($dpflnamenew);
$kim_img_nm = init_profile_img($dpflnamenew, $type);
$image_width_fixed = resize_image_to_width(560, $kim_img_nm, $width, $height);

Resize Image to Fixed Height While Maintaining the Aspect Ratio

//resizing images php
function resize_image_to_height($new_height, $image, $width, $height) {
    $ratio = $new_height / $height;
    $fresh_quick_w = $width * $ratio;
    return resize_image($fresh_quick_w, $new_height, $image, $width, $height);
}

$dpflnamenew = "media/pakainfo.jpg";
list($width, $height, $type) = getimagesize($dpflnamenew);
$kim_img_nm = init_profile_img($dpflnamenew, $type);
$image_height_fixed = resize_image_to_height(900, $kim_img_nm, $width, $height);

Scale Image By a Given Factor

//php images resize
function scale_image($scale, $image, $width, $height) {
    $fresh_quick_w = $width * $scale;
    $new_height = $height * $scale;
    return resize_image($fresh_quick_w, $new_height, $image, $width, $height);
}

$dpflnamenew = "media/pakainfo.jpg";
list($width, $height, $type) = getimagesize($dpflnamenew);
$kim_img_nm = init_profile_img($dpflnamenew, $type);
$image_scaled = scale_image(0.8, $kim_img_nm, $width, $height);

Save the Resized Image

function convert_store_img($organic_img_file, $new_dpflnamenew, $new_type='jpeg', $quality=80) {
    if( $new_type == 'jpeg' ) {
        imagejpeg($organic_img_file, $new_dpflnamenew, $quality);
     }
     elseif( $new_type == 'png' ) {
        imagepng($organic_img_file, $new_dpflnamenew);
     }
     elseif( $new_type == 'gif' ) {
        imagegif($organic_img_file, $new_dpflnamenew);
     }
}

$dpflnamenew = "media/pakainfo.jpg";
list($width, $height, $type) = getimagesize($dpflnamenew);
$kim_img_nm = init_profile_img($dpflnamenew, $type);
$organic_img_file = resize_image(280, 180, $kim_img_nm, $width, $height);
convert_store_img($organic_img_file, 'media/resized-'.basename($dpflnamenew), 'jpeg', 75);

The Complete PHP Code for Loading, Resizing and Saving Images

//php imageresize
function init_profile_img($dpflnamenew, $type) {
    if( $type == IMAGETYPE_JPEG ) {
        $image = imagecreatefromjpeg($dpflnamenew);
    }
    elseif( $type == IMAGETYPE_PNG ) {
        $image = imagecreatefrompng($dpflnamenew);
    }
    elseif( $type == IMAGETYPE_GIF ) {
        $image = imagecreatefromgif($dpflnamenew);
    }
    return $image;
}

function resize_image($fresh_quick_w, $new_height, $image, $width, $height) {
    $organic_img_file = imagecreatetruecolor($fresh_quick_w, $new_height);
    imagecopyresampled($organic_img_file, $image, 0, 0, 0, 0, $fresh_quick_w, $new_height, $width, $height);
    return $organic_img_file;
}

function resize_image_to_width($fresh_quick_w, $image, $width, $height) {
    $ratio = $fresh_quick_w / $width;
    $new_height = $height * $ratio;
    return resize_image($fresh_quick_w, $new_height, $image, $width, $height);
}

function resize_image_to_height($new_height, $image, $width, $height) {
    $ratio = $new_height / $height;
    $fresh_quick_w = $width * $ratio;
    return resize_image($fresh_quick_w, $new_height, $image, $width, $height);
}

function scale_image($scale, $image, $width, $height) {
    $fresh_quick_w = $width * $scale;
    $new_height = $height * $scale;
    return resize_image($fresh_quick_w, $new_height, $image, $width, $height);
}


function convert_store_img($organic_img_file, $new_dpflnamenew, $new_type='jpeg', $quality=80) {
    if( $new_type == 'jpeg' ) {
        imagejpeg($organic_img_file, $new_dpflnamenew, $quality);
     }
     elseif( $new_type == 'png' ) {
        imagepng($organic_img_file, $new_dpflnamenew);
     }
     elseif( $new_type == 'gif' ) {
        imagegif($organic_img_file, $new_dpflnamenew);
     }
}

//php image resize function
/* Testing the above PHP source code */

$dpflnamenew = "media/pakainfo.jpg";
list($width, $height, $type) = getimagesize($dpflnamenew);
$kim_img_nm = init_profile_img($dpflnamenew, $type);

$organic_img_file = resize_image(280, 180, $kim_img_nm, $width, $height);
$image_width_fixed = resize_image_to_width(560, $kim_img_nm, $width, $height);
$image_height_fixed = resize_image_to_height(900, $kim_img_nm, $width, $height);
$image_scaled = scale_image(0.8, $kim_img_nm, $width, $height);

convert_store_img($organic_img_file, 'media/resized-'.basename($dpflnamenew), 'jpeg', 75);
convert_store_img($image_width_fixed, 'media/fixed-width-'.basename($dpflnamenew), 'jpeg', 75);
convert_store_img($image_height_fixed, 'media/fixed-height-'.basename($dpflnamenew), 'jpeg', 75);
convert_store_img($image_scaled, 'media/scaled-'.basename($dpflnamenew), 'jpeg', 75);

Also Read : PHP Image Resize Script Example

Read Also:  PHP Laravel Sessions Example Tutorial

Get Resource Id for Target Image Layer

$target_width =400;
$target_height =400;
$target_layer=imagecreatetruecolor($target_width,$target_height);

Resizing and Reassembling

imagecopyresampled($target_layer,$image_resource_id,0,0,0,0,$target_width,$target_height, $source_properties[0],$source_properties[1]);

I hope you get an idea about php resizing image.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.