how to resize image in php

Today, We want to share with you how to resize image in php.In this post we will show you php multiple images resizes and save to folder, hear for resizes image in php without losing quality we will give you demo and example for implement.In this post, we will learn about File Upload โ€“ Auto Resize Compress-Reduce image using PHP with an example.

Resize image in PHP

With GD, for example, it’s as simple as…

Example 1:

function resize_image($file, $w, $h, $crop=FALSE) {
    list($width, $height) = getimagesize($file);
    $r = $width / $height;
    if ($crop) {
        if ($width > $height) {
            $width = ceil($width-($width*abs($r-$w/$h)));
        } else {
            $height = ceil($height-($height*abs($r-$w/$h)));
        }
        $settingofw = $w;
        $settingofh = $h;
    } else {
        if ($w/$h > $r) {
            $settingofw = $h*$r;
            $settingofh = $h;
        } else {
            $settingofh = $w/$r;
            $settingofw = $w;
        }
    }
    $src = imagecreatefromjpeg($file);
    $filedestination = imagecreatetruecolor($settingofw, $settingofh);
    imagecopyresampled($filedestination, $src, 0, 0, 0, 0, $settingofw, $settingofh, $width, $height);

    return $filedestination;
}

Free Live Chat for Any Issue

call this PHP function

$img = resize_image(โ€˜/path/to/some/image.jpgโ€™, 200, 200);

How to Resize JPEG Image in PHP ?

index.php

<?php 

 
$filename = 'pakainfo.jpg'; 


$width = 100; 
$height = 100; 


header('Content-Type: image/jpg'); 


list($width_orig, $height_orig) = getimagesize($filename); 

$organicpicrat = $width_orig/$height_orig; 

if ($width/$height > $organicpicrat) { 
	$width = $height*$organicpicrat; 
} else { 
	$height = $width/$organicpicrat; 
} 
 
$livepicture = imagecreatetruecolor($width, $height); 
$image = imagecreatefromjpeg($filename); 

imagecopyresampled($livepicture, $image, 0, 0, 0, 0, 
		$width, $height, $width_orig, $height_orig); 

imagejpeg($livepicture, null, 100); 

?>

I hope you get an idea about php images resizes scripts.
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.

Rate this post