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.

Read Also:  PHP Ajax Image Upload Resize Crop using jQuery

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;

call this PHP function

$img = resize_image(‘/path/to/some/image.jpg’, 200, 200);

How to Resize JPEG Image in 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 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.

Read Also:  PHP Automatic Flexify image resizing

Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  SQL left outer join Examples
  2. Read Also:  PHP Variable Scope Global Static & Local
  3. Read Also:  PHP Resize Image Upload Example
  4. Read Also:  laravel where date between multiple columns
  5. Read Also:  Simple Jquery ajax request example with demo
  6. Read Also:  PHP Auto image resizing Script
  7. Read Also:  PHP Image Resize Script Example
  8. Read Also:  Ajax Shopping Cart PHP MYSQLi Tutorial With Example
  9. Read Also:  laravel excel import Export to Database Example
  10. Read Also:  how to call javascript function in html?