Code: Select all
if ( !extension_loaded('gd') )
{
die("PHP GD extension not loaded on server!<br />Please contact the system adminsitrator!");
}
// what type of image is this?
$imageBaseData = getimagesize($source);
switch ( $imageBaseData[2] )
{
// ---------------------------
// GIF
// ---------------------------
case 1:
// create a new image with the new width and height
$newImage = imagecreatetruecolor($width, $height);
$newImageBackground = imagecolorallocate($newImage, 0, 0, 0);
ImageColorTransparent($newImage, $newImageBackground); // make the new image completely transparent
imagealphablending($newImage, false); // turn off the alphs blending to keep the alpha channel
imageSaveAlpha($newImage, true); // make sure alpha saving is on
// resize the file
$imgResource = imagecreatefromgif($source);
imagecopyresampled($newImage, $imgResource, 0, 0, 0, 0, $width, $height, imagesx($imgResource), imagesy($imgResource));
// write the new image & destroy the temp
imagegif($newImage, $destination);
imagedestroy($newImage);
break;
// ---------------------------
// JPEG
// ---------------------------
case 2:
// create a new image with the new width and height
$newImage = imagecreatetruecolor($width, $height);
$newImageBackground = imagecolorallocate($newImage, 0, 0, 0);
imagealphablending($newImage, true);
// resize the file
$imgResource = imagecreatefromjpeg($source);
imagecopyresampled($newImage, $imgResource, 0, 0, 0, 0, $width, $height, imagesx($imgResource), imagesy($imgResource));
// write the new image & destroy the temp
imagejpeg($newImage, $destination);
imagedestroy($newImage);
break;
// ---------------------------
// PNG
// ---------------------------
case 3:
// create a new image with the new width and height
$newImage = imagecreatetruecolor($width, $height);
$newImageBackground = imagecolorallocate($newImage, 0, 0, 0);
ImageColorTransparent($newImage, $newImageBackground); // make the new image completely transparent
imagealphablending($newImage, false); // turn off the alphs blending to keep the alpha channel
imageSaveAlpha($newImage, true); // make sure alpha saving is on
// resize the file
$imgResource = imagecreatefrompng($source);
imagecopyresampled($newImage, $imgResource, 0, 0, 0, 0, $width, $height, imagesx($imgResource), imagesy($imgResource));
// write the new image & destroy the temp
imagepng($newImage, $destination);
imagedestroy($newImage);
break;
// ---------------------------
// err
// ---------------------------
default:
die ( sprintf("Unhandled image type (%d)!<br />Please only upload JPEG, GIF or PNG images.", $imageBaseData[2]) );
break;
}