Files
pictshare/classes/image.php
2015-11-06 12:25:49 +01:00

100 lines
2.9 KiB
PHP
Executable File

<?php
class Image
{
function rotate(&$im,$direction)
{
switch($direction)
{
case 'upside': $angle = 180;break;
case 'left': $angle = 90;break;
case 'right': $angle = -90;break;
default: $angle = 0;break;
}
$im = imagerotate($im,$angle,0);
}
/**
* From: https://stackoverflow.com/questions/4590441/php-thumbnail-image-resizing-with-proportions
*/
function resize(&$img,$size)
{
if(!is_numeric($size))
$size = explode('x',$size);
if(is_array($size))
{
$maxwidth = $size[0];
$maxheight = $size[1];
}
else if($size)
{
$maxwidth = $size;
$maxheight = $size;
}
$width = imagesx($img);
$height = imagesy($img);
if(!ALLOW_BLOATING)
{
if($maxwidth>$width)$maxwidth = $width;
if($maxheight>$height)$maxheight = $height;
}
if ($height > $width)
{
$ratio = $maxheight / $height;
$newheight = $maxheight;
$newwidth = $width * $ratio;
}
else
{
$ratio = $maxwidth / $width;
$newwidth = $maxwidth;
$newheight = $height * $ratio;
}
$newimg = imagecreatetruecolor($newwidth,$newheight);
$palsize = ImageColorsTotal($img);
for ($i = 0; $i < $palsize; $i++)
{
$colors = ImageColorsForIndex($img, $i);
ImageColorAllocate($newimg, $colors['red'], $colors['green'], $colors['blue']);
}
imagefill($newimg, 0, 0, IMG_COLOR_TRANSPARENT);
imagesavealpha($newimg,true);
imagealphablending($newimg, true);
imagecopyresampled($newimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
$img = $newimg;
}
function filter(&$im,$vars)
{
foreach($vars as $var)
{
if(strpos($var,'_'))
{
$a = explode('_',$var);
$var = $a[0];
$val = $a[1];
}
switch($var)
{
case 'negative': imagefilter($im,IMG_FILTER_NEGATE); break;
case 'grayscale': imagefilter($im,IMG_FILTER_GRAYSCALE); break;
case 'brightness': imagefilter($im,IMG_FILTER_BRIGHTNESS,$val); break;
case 'edgedetect': imagefilter($im,IMG_FILTER_EDGEDETECT); break;
case 'smooth': imagefilter($im,IMG_FILTER_SMOOTH,$val); break;
case 'contrast': imagefilter($im,IMG_FILTER_CONTRAST,$val); break;
case 'pixelate': imagefilter($im,IMG_FILTER_PIXELATE,$val); break;
}
}
}
}