-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathImage.php
More file actions
102 lines (92 loc) · 2.54 KB
/
Image.php
File metadata and controls
102 lines (92 loc) · 2.54 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
<?php
/**
* Image
* Class for working with images
*
* @author igor <onachenko@gmail.com>
* @example <br/>
* <pre>
* $image = Image::open('test.jpg');
* $image->resizeToWidth(100);
* $image->save("test_width.jpg");
* </pre>
*/
abstract class Image {
protected $_fileName;
protected $_width;
protected $_height;
protected $_imgData;
function __construct($dest)
{
if (!file_exists($dest)) {
throw new Exception("Failed to open file '{$dest}'");
}
$this->_fileName = $dest;
$this->_imgData = getimagesize($this->_fileName);
$this->_width = $this->_imgData[0];
$this->_height = $this->_imgData[1];
}
function getWidth()
{
return $this->_width;
}
function getHeight()
{
return $this->_height;
}
/**
* resize with width and height
* @param int $width
* @param int $height
*/
function resize($width, $height)
{
$this->_width = $width;
$this->_height = $height;
}
/**
* resize width saving ratio of image
* @param int $width
*/
function resizeToWidth($width)
{
$ratio = $width / $this->_width;
$this->_width = $this->_width * $ratio;
$this->_height = $this->_height * $ratio;
}
/**
* resize height saving ratio of image
* @param int $height
*/
function resizeToHeight($height)
{
$ratio = $height / $this->_height;
$this->_height = $this->_height * $ratio;
$this->_width = $this->_width * $ratio;
}
/**
* saves file to the server
* if parametr is null method saves image with filename giving in constructor <br/>
* if parametr is not null method saves image with new name
* @param string $filename
*/
abstract public function save($filename=null);
/**
* Opens file and choose adapter according to the image type
* @param string $dest
* @return Image
*/
static public function open($dest)
{
$imageData = getimagesize($dest);
switch($imageData[2]) {
case IMAGETYPE_JPEG : $image = new JpegImage($dest);
break;
case IMAGETYPE_GIF : $image = new GifImage($dest);
break;
case IMAGETYPE_PNG : $image = new PngImage($dest);
break;
}
return $image;
}
}