false, wenn aus Dateiinhalt keine gueltige PHP-Bildresource erstellt werden konnte (z.b. bei BMP-Datei)
* @throws InvalidArgumentException Wenn Datei kein gueltiges Bild ist, oder nicht gelesen werden kann
*
*/
function createImageFromFile($filename, $use_include_path = false, $context = null, &$info = null) {
// try to detect image informations -> info is false if image was not readable or is no php supported image format (a check for "is_readable" or fileextension is no longer needed)
$info = array("image"=>getimagesize($filename));
$info["image"] = getimagesize($filename);
if($info["image"] === false) throw new InvalidArgumentException("\"".$filename."\" is not readable or no php supported format");
else {
// fetches fileconten from url and creates an image ressource by string data
// if file is not readable or not supportet by imagecreate FALSE will be returnes as $imageRes
$imageRes = imagecreatefromstring(file_get_contents($filename, $use_include_path, $context));
// export $http_response_header to have this info outside of this function
if(isset($http_response_header)) $info["http"] = $http_response_header;
return $imageRes;
}
}
function cropImageToFit($src, $width, $height) {
/*
* Crop-to-fit PHP-GD
* http://salman-w.blogspot.com/2009/04/crop-to-fit-image-using-aspphp.html
*
* Resize and center crop an arbitrary size image to fixed width and height
* e.g. convert a large portrait/landscape image to a small square thumbnail
*/
define('DESIRED_IMAGE_WIDTH', $width);
define('DESIRED_IMAGE_HEIGHT', $height);
$source_path = $src;
/*
* Add file validation code here
*/
$source_gdim = createImageFromFile("http://www.newyorker.com/wp-content/uploads/2014/10/Davidson-Malala-Yousafzai-728-375.jpg");
list($source_width, $source_height, $source_type) = getimagesize($source_gdim);
// switch ($source_type) {
// case IMAGETYPE_GIF:
// $source_gdim = imagecreatefromgif($source_path);
// break;
// case IMAGETYPE_JPEG:
// $source_gdim = imagecreatefromjpeg($source_path);
// break;
// case IMAGETYPE_PNG:
// $source_gdim = imagecreatefrompng($source_path);
// break;
// }
$source_aspect_ratio = $source_width / $source_height;
$desired_aspect_ratio = DESIRED_IMAGE_WIDTH / DESIRED_IMAGE_HEIGHT;
if ($source_aspect_ratio > $desired_aspect_ratio) {
/*
* Triggered when source image is wider
*/
$temp_height = DESIRED_IMAGE_HEIGHT;
$temp_width = ( int ) (DESIRED_IMAGE_HEIGHT * $source_aspect_ratio);
} else {
/*
* Triggered otherwise (i.e. source image is similar or taller)
*/
$temp_width = DESIRED_IMAGE_WIDTH;
$temp_height = ( int ) (DESIRED_IMAGE_WIDTH / $source_aspect_ratio);
}
/*
* Resize the image into a temporary GD image
*/
$temp_gdim = imagecreatetruecolor($temp_width, $temp_height);
imagecopyresampled(
$temp_gdim,
$source_gdim,
0, 0,
0, 0,
$temp_width, $temp_height,
$source_width, $source_height
);
/*
* Copy cropped region from temporary image into the desired GD image
*/
$x0 = ($temp_width - DESIRED_IMAGE_WIDTH) / 2;
$y0 = ($temp_height - DESIRED_IMAGE_HEIGHT) / 2;
$desired_gdim = imagecreatetruecolor(DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT);
imagecopy(
$desired_gdim,
$temp_gdim,
0, 0,
$x0, $y0,
DESIRED_IMAGE_WIDTH, DESIRED_IMAGE_HEIGHT
);
/*
* Render the image
* Alternatively, you can save the image in file-system or database
*/
//header('Content-type: image/jpeg');
return $desired_gdim;
//return imagejpeg($desired_gdim);
}
$src = "images/ballerina.jpg";
$src = "images/mouse.jpg";
// if (tiltshift($src, '0.5', '0.5', '0.25', '0.25')) {
// echo " ";
// }
if (gd_filter_pixelate($src, 25)) {
echo " ";
}
?>