<?php
function resize($src, $dest, $width = NULL, $height = NULL, $stamp = false, $quality = 100, $deg = 0) {	
	$dest_ext = strtolower(pathinfo($dest, PATHINFO_EXTENSION));

	if (strtolower(pathinfo($src, PATHINFO_EXTENSION)) === 'svg') {
    if (!is_file($src)) return false;
    return copy($src, $dest);
	}
	
	if (!$src || !is_file($src)) return false;
	$image_size = @getimagesize($src);
	if (!$image_size) return false;
	
  $image_width = $image_size[0];
  $image_height = $image_size[1];
  
	if ($quality < 10) $quality = 10;
	if ($quality > 100) $quality = 100;
	
	if (!$width && !$height){
		$width = $image_width;
		$height = $image_height;
	}
	
	if (!$src OR (!$width && !$height)) return false;
	
	if (!$width) $width = $image_width;
	if (!$height) $height = $image_height;
	
	$img_ratio = $image_width/$image_height;
	
	if (($image_width > $width) || ($image_height > $height)) {
		if ($height * $img_ratio > $width) $height = $width / $img_ratio;
		else $width = $height * $img_ratio;
		if ($width / $img_ratio < $height) $height = $width / $img_ratio;
		else $width = $height * $img_ratio;
	} else {
		$width = $image_width;
		$height = $image_height;
	}
	
	if (extension_loaded('gd')) {
		$info = @getimagesize($src);
    if (!$info || !isset($info['mime'])) return false;
    
    $image = false;
    
		switch ($info['mime']) {
			case 'image/png': $image = @imagecreatefrompng($src); break;
			case 'image/bmp':	$image = @imagecreatefrombmp($src); break;
			case 'image/webp': $image = @imagecreatefromwebp($src); break;
			case 'image/jpeg': $image = @imagecreatefromjpeg($src); break;
			case 'image/gif':	$image = @imagecreatefromgif($src); break;
			default: return false;
		}
		
		if (!$image) return false;
	  
	  if ($stamp == true) img_add_watermark($image);
	  
	  $width = (int) $width;
	  $height = (int) $height;
	  $image_width = (int) $image_width;
	  $image_height = (int) $image_height;
	  
	  $thumb = imagecreatetruecolor($width, $height);
	  if ($dest_ext == 'png' || $dest_ext == 'webp'){
	  	imagealphablending($thumb, false);
			imagesavealpha($thumb, true);
		} else {
			$white = imagecolorallocate($thumb, 255, 255, 255);
			imagefill($thumb, 0, 0, $white);
		}
		if ($width == $image_width && $height == $image_height) {
			imagecopy($thumb,	$image,	0, 0,	0, 0,	$image_width,	$image_height);
		} else {
	  	imagecopyresampled($thumb, $image, 0, 0, 0, 0, $width, $height, $image_width, $image_height);
	  }
	  
		switch ($dest_ext){
			case 'png':
				$quality = ($quality - 100) / 11.111111;
				$quality = round(abs($quality));
				imagepng($thumb, $dest, $quality);
				break;
			case 'bmp': imagebmp($thumb, $dest, true); break;
			case 'webp': 
				if (defined('IMG_WEBP_LOSSLESS')) {
					imagewebp($thumb, $dest, IMG_WEBP_LOSSLESS);
				} else {
					imagewebp($thumb, $dest, $quality);
				}
				break;
			case 'jpg': imagejpeg($thumb, $dest, $quality); break;
			case 'jpeg': imagejpeg($thumb, $dest, $quality); break;
			case 'gif': imagegif($thumb, $dest); break;
		}
	  imagedestroy($image);
	  imagedestroy($thumb);
	  return true;
	} elseif (extension_loaded('imagick')){
		$imagick = new Imagick();
		$imagick->readImage($src);
		if ($width != $image_width || $height != $image_height) {
			$imagick->resizeImage($width, $height, Imagick::FILTER_LANCZOS, 1, 1);
		}
		$imagick_format = $dest_ext == 'jpg' ? 'jpeg' : $dest_ext;
		$imagick->setImageFormat($imagick_format);
		
		if ($dest_ext == 'webp') {
			$imagick->setOption('webp:lossless', 'true');
		} else {
			$imagick->setImageCompressionQuality($quality);
		}
		
	  $imagick->writeImage($dest);
	  $imagick->clear();
	  $imagick->destroy();
	  return true;
	} else {
		$fehler = LABEL_ERROR_IMAGE_LIB;
	  return false;
	}
}


function img_add_watermark($thumb) {
	global $watermark, $watermark_position, $watermark_top, $watermark_right, $watermark_bottom, $watermark_left;
	
	$width = imagesx($thumb);
	$height = imagesy($thumb);
	
	if (!$watermark_top) $watermark_top = 0;
	if (!$watermark_right) $watermark_right = 0;
	if (!$watermark_bottom) $watermark_bottom = 0;
	if (!$watermark_left) $watermark_left = 0;
	
	if (extension_loaded('gd') && is_file("images/".$watermark)) {
		$watermark_file = imagecreatefrompng("images/".$watermark);
		
  	$image_width = imagesx($watermark_file);
  	$image_height = imagesy($watermark_file);
  	
  	switch ($watermark_position){
  		case 'RT':
  			$watermark_x = $width - $image_width - $watermark_right;
  			$watermark_y = 0 + $watermark_top;
  			break;
  		case 'RB':
  			$watermark_x = $width - $image_width - $watermark_right;
  			$watermark_y = $height - $image_height - $watermark_bottom;
  			break;
  		case 'LB':
  			$watermark_x = 0 + $watermark_left;
  			$watermark_y = $height - $image_height - $watermark_bottom;
  			break;
  		case 'LT':
  		default:
  			$watermark_x = 0 + $watermark_left;
  			$watermark_y = 0 + $watermark_top;
  			break;
  	}  	
  	
	  $watermark_x = (int) $watermark_x;
	  $watermark_y = (int) $watermark_y;
	  $image_width = (int) $image_width;
	  $image_height = (int) $image_height;
		
		imagecopyresampled ($thumb, $watermark_file, $watermark_x, $watermark_y, 0, 0, $image_width, $image_height, $image_width, $image_height);
	}
}