Speed up development with full-stack environments for every branch.

Learn More

How to resize an image using GD library [PHP]

7388 Runs 53086 Views 37768 Copies
Saved

Saved

gtsolutions 373

gtsolutions
published 4 years ago

<?php

/*
 * PHP GD
 * resize an image using GD library
 */

// File and new size
//the original image has 800x600
$filename = 'images/picture.jpg';
//the resize will be a percent of the original size
$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 and free memory
//the resized image will be 400x300
imagejpeg($thumb);
imagedestroy($thumb);
?>
Please login/signup to get access to the terminal.

Your session has timed out.

Dismiss (the page may not function properly).