Skip to content

Instantly share code, notes, and snippets.

@zippy1981
Created April 14, 2012 22:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zippy1981/2388225 to your computer and use it in GitHub Desktop.
Save zippy1981/2388225 to your computer and use it in GitHub Desktop.
Some example code for re-sizing images in PHP.
<?php
/**
* This file contains the class ExamplePicture.
* @package ExampleMiddleWare
**/
/**
* Include files
**/
require_once("bmp.php");
/**
* This class resizes images.
* onle one static function to set the users Avatar.
* It is likely to remain that way.
* @package ExampleMiddleWare
**/
class ExamplePicture {
/**
* Copies a bmp to a png, while scaling it.
* @param string $srcFile The name of the source file.
* @param string $destFile The name of the destination file
* @param int $width The width of the destination image.
* @param int $height The height of the destination image.
**/
public static function ScaleBmp2Png($srcFile, $destFile, $width, $height) {
$srcInfo = getimagesize($srcFile);
$im = imagecreatefrombmp($srcFile);
$im_dest = imagecreatetruecolor ($width, $height);
imagealphablending($im_dest, false);
imagecopyresampled
($im_dest, $im, 0, 0, 0, 0,
$width, $height, $srcInfo[0], $srcInfo[1]);
imagesavealpha($im_dest, false);
imagepng($im_dest, $destFile);
}
/**
* Copies a gif, while scaling it.
* @param string $srcFile The name of the source file.
* @param string $destFile The name of the destination file
* @param int $width The width of the destination image.
* @param int $height The height of the destination image.
**/
public static function ScaleGif($srcFile, $destFile, $width, $height) {
$srcInfo = getimagesize ($srcFile);
$im = imagecreatefromgif($srcFile);
$im_dest = imagecreatetruecolor ($width, $height);
imagealphablending($im_dest, false);
imagecopyresampled
($im_dest, $im, 0, 0, 0, 0,
$width, $height, $srcInfo[0], $srcInfo[1]);
imagesavealpha($im_dest, true);
imagegif($im_dest, $destFile);
}
/**
* Copies a jpeg, while scaling it.
* @param string $srcFile The name of the source file.
* @param string $destFile The name of the destination file
* @param int $width The width of the destination image.
* @param int $height The height of the destination image.
**/
public static function ScaleJpeg($srcFile, $destFile, $width, $height) {
$srcInfo = getimagesize ($srcFile);
$im = imagecreatefromjpeg($srcFile);
$im_dest = imagecreatetruecolor ($width, $height);
imagealphablending($im_dest, false);
imagecopyresampled
($im_dest, $im, 0, 0, 0, 0,
$width, $height, $srcInfo[0], $srcInfo[1]);
imagesavealpha($im_dest, true);
imagejpeg($im_dest, $destFile);
}
/**
* Copies a png, while scaling it.
* @param string $srcFile The name of the source file.
* @param string $destFile The name of the destination file
* @param int $width The width of the destination image.
* @param int $height The height of the destination image.
**/
public static function ScalePng($srcFile, $destFile, $width, $height) {
$srcInfo = getimagesize ($srcFile);
$im = imagecreatefrompng($srcFile);
$im_dest = imagecreatetruecolor ($width, $height);
imagealphablending($im_dest, false);
imagecopyresampled
($im_dest, $im, 0, 0, 0, 0,
$width, $height, $srcInfo[0], $srcInfo[1]);
imagesavealpha($im_dest, true);
imagepng($im_dest, $destFile);
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment