class_mcImagick.inc.php
2.62 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php
/**
* @copyright (c) 2014, netz.coop eG
*/
class mcImagick {
private static $files = array();
public static function getData($param_file_path) {
if(file_exists($param_file_path)) {
$ncimage_Imagick = new Imagick($param_file_path);
$ncimage_data = array(
'height' => $ncimage_Imagick->getImageHeight(),
'width' => $ncimage_Imagick->getImageWidth(),
'properties' => $ncimage_Imagick->getImageProperties(),
'size' => $ncimage_Imagick->getImageSize(),
'type' => $ncimage_Imagick->getImageType(),
);
return $ncimage_data;
}
}
/**
* 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;
}
}
}