PHP – resizing an image

 

<?php
// File and new size
$filename = 'test.jpg';
$percent = 0.5;

// Content type
header('Content-Type: image/jpeg');

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Output
imagejpeg($thumb);
?>

Ex: resizing an image and change background

function resize_middle($size,$path_pic){

//header("Content-type: image/jpeg,image/gif,image/png");
// Get new sizes
list($width, $height) = getimagesize($path_pic);
$newwidth = $size;
$newheight = $size;

$dst_y=($size-$height)/2;
$dst_x=($size-$width)/2;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$background = imagecolorallocate( $thumb, 255, 255, 255 );
imagefill($thumb, 0, 0, $background);
//imagecolordeallocate($thumb,$background); // set_text_color
$source = imagecreatefromjpeg($path_pic);

// Resize
imagecopyresized($thumb, $source, $dst_x, $dst_y, 0, 0, $width, $height, $width, $height);

// Output
imagejpeg($thumb,$path_pic,100); //imagejpeg($thumb,null,100);
imagedestroy( $thumb );

}

 

reference: php.net