-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageLibrary.inc
138 lines (95 loc) · 3.32 KB
/
ImageLibrary.inc
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
<?php
class ImageLibrary {
/* IMAGETYPE_* defined in http://www.php.net/manual/en/function.exif-imagetype.php */
private $IMAGE_TYPES = array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG);
public function __construct() {
}
private function createImageFromFile($filename, $fileType) {
switch($fileType) {
case IMAGETYPE_GIF:
return imagecreatefromgif($filename);
case IMAGETYPE_JPEG:
return imagecreatefromjpeg($filename);
case IMAGETYPE_PNG:
return imagecreatefrompng($filename);
default:
return false;
}
}
private function saveImageToFile($targetImage, $newFilename, $fileType) {
switch($fileType) {
case IMAGETYPE_GIF:
imagegif($targetImage, $newFilename);
break;
case IMAGETYPE_JPEG:
imagejpeg($targetImage, $newFilename);
break;
case IMAGETYPE_PNG:
imagepng($targetImage, $newFilename);
break;
default:
return false;
}
return true;
}
/**
* Scales the image to the new size given.
* Depending on the action given either resample or resize the image to the desired size.
*
* @param string $targetFile Where to place the new image.
* @param string $sourceFile What image to resize.
* @param integer $newWidth The new width of the image.
* @param integer $newHeight The new height of the image.
* @param string $action The action to take, either resample or resize.
*/
private function scaleImage($targetFile, $sourceFile, $newWidth, $newHeight, $action) {
$imagesize = getimagesize($sourceFile);
if(!in_array($imagesize[2], $this->IMAGE_TYPES)) {
return false;
}
$sourceImage = $this->createImageFromFile($sourceFile, $imagesize[2]);
if (!$sourceImage) {
return false;
}
$sourceWidth = imagesx($sourceImage);
$sourceHeight = imagesy($sourceImage);
$targetImage = imagecreatetruecolor($newWidth, $newHeight);
switch ($action) {
case "resample":
imagecopyresampled($targetImage, $sourceImage, 0, 0, 0, 0, $newWidth, $newHeight, $sourceWidth, $sourceHeight);
break;
case "resize":
imagecopyresized($targetImage, $sourceImage, 0, 0, 0, 0, $newWidth, $newHeight, $sourceWidth, $sourceHeight);
break;
default:
return false;
}
$this->saveImageToFile($targetImage, $targetFile, $imagesize[2]);
return true;
}
/**
* Resamples the given image ($sourceFile) image to a new size.
* The new image is saved in the given place ($targetFile)
*
* @param string $targetFile Where to place the new image.
* @param string $sourceFile What image to resize.
* @param integer $newWidth The new width of the image.
* @param integer $newHeight The new height of the image.
*/
public function resample($targetFile, $sourceFile, $newWidth, $newHeight) {
return $this->scaleImage($targetFile, $sourceFile, $newWidth, $newHeight, "resample");
}
/**
* Resizes the given image ($sourceFile) to a new size.
* The new image is saved in the given place ($targetFile)
*
* @param string $targetFile Where to place the new image.
* @param string $sourceFile What image to resize.
* @param integer $newWidth The new width of the image.
* @param integer $newHeight The new height of the image.
*/
public function resize($targetFile, $sourceFile, $newWidth, $newHeight) {
return $this->scaleImage($targetFile, $sourceFile, $newWidth, $newHeight, "resize");
}
}
?>