Arpan Patel Cluster
Image resize in PHP :
In this example we are using 4 functions
1. imagecreatetruecolor
2. imagecreatefromjpeg
3. imagecopyresized
4. imagejpeg

1. imagecreatetruecolor :
Description : returns an image identifier representing a black image of the specified size.

2. imagecreatefromjpeg :
Description : returns an image identifier representing the image obtained from the given filename.

3. imagecopyresized :
Description : copies a rectangular portion of one image to another image.

4. imagejpeg :
Description : creates a JPEG file from the given image.

Example :

$filename = 'test.jpg';
$percent = 0.5;

// Content type - If you want to outout an image into browser.
header('Content-Type: image/jpeg';

// Size of image we want to resize
$newwidth = 200;//$width * $percent;
$newheight = 200;// $height * $percent;

// Get actual width and height of image
list($width, $height) = getimagesize($filename);

// Create a black image identifier for our desired resized image
$thumb = imagecreatetruecolor($newwidth, $newheight);

// Load actual image identifier
$source = imagecreatefromjpeg($filename);

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

// Output in browser
imagejpeg($thumb);

// To save image
imagejpeg($thumb,'test-thumb.jpg',100);

Resource : http://php.net/manual/en/function.imagecopyresized.php
Like · Comment ·
Jitu Vadher likes this
Download Android App