Commit 3cd29237 by sn

mcImagick kann jetzt thumbnail

1 parent ec738e92
Showing with 81 additions and 0 deletions
...@@ -22,5 +22,85 @@ class mcImagick { ...@@ -22,5 +22,85 @@ class mcImagick {
} }
/**
* Calculate new image dimensions to new constraints
*
* @param Original X size in pixels
* @param Original Y size in pixels
* @return New X maximum size in pixels
* @return New Y maximum size in pixels
* http://php.net/manual/de/imagick.thumbnailimage.php
*/
private static function scaleImage($x,$y,$cx,$cy) {
//Set the default NEW values to be the old, in case it doesn't even need scaling
list($nx,$ny)=array($x,$y);
//If image is generally smaller, don't even bother
if ($x>=$cx || $y>=$cx) {
//Work out ratios
if ($x>0) $rx=$cx/$x;
if ($y>0) $ry=$cy/$y;
//Use the lowest ratio, to ensure we don't go over the wanted image size
if ($rx>$ry) {
$r=$ry;
} else {
$r=$rx;
}
//Calculate the new size based on the chosen ratio
$nx=intval($x*$r);
$ny=intval($y*$r);
}
//Return the results
return array($nx,$ny);
}
public static function makeThumbnail($param_original_filepath, $param_thumbnail_filepath, $param_maximumWidth=100, $param_maximumHeight=100) {
if(file_exists($param_original_filepath)) {
//Read original image and create Imagick object
try {
$thumb=new Imagick($param_original_filepath);
} catch (Exception $e) {
D::li('Exception abgefangen: '. $e->getMessage());
return false;
}
try {
//Work out new dimensions
list($newX,$newY)=mcImagick::scaleImage(
$thumb->getImageWidth(),
$thumb->getImageHeight(),
$param_maximumWidth,
$param_maximumHeight);
//Scale the image
$thumb->thumbnailImage($newX,$newY);
} catch (Exception $e) {
D::li('Exception abgefangen: '. $e->getMessage());
return false;
}
$create = mcDir::create(dirname($param_thumbnail_filepath));
//Write the new image to a file
try {
$thumb->writeImage($param_thumbnail_filepath);
} catch (Exception $e) {
D::li('Exception abgefangen: '. $e->getMessage());
return false;
}
if(file_exists($param_thumbnail_filepath)) {
return true;
} else {
return false;
}
} else {
return false;
}
}
} }
\ No newline at end of file
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!