-
Notifications
You must be signed in to change notification settings - Fork 0
/
image.php
80 lines (69 loc) · 1.59 KB
/
image.php
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
<?php
/**
* Image
*
* @author Rob Keplin
* @link http://www.robkeplin.com
**/
class Image
{
public $errors;
private $_image;
public function __construct()
{
$this->errors = array();
$this->_image = NULL;
}
public function getImage($url)
{
$ext = strrchr($url, '.');
$ext = strtolower($ext);
switch($ext) {
case '.jpeg':
case '.jpg':
$this->_image = imagecreatefromjpeg($url);
break;
case '.gif':
$this->_image = imagecreatefromgif($url);
break;
case '.png':
$this->_image = imagecreatefrompng($url);
default:
$this->errors[] = "The linked file must be a .jpeg, .jpg, .gif, or .png";
break;
}
}
public function resizeImage($new_width, $new_height)
{
if($this->_image)
{
//Resize the image
$old_width = imagesx($this->_image);
$old_height = imagesy($this->_image);
//Keep the resolution of the image the same
if($old_width > $old_height)
{
$ratio = $old_height / $new_height;
}
else
{
$ratio = $old_width / $new_width;
}
$new_width = ceil($old_width / $ratio);
$new_height = ceil($old_height / $ratio);
$new_image = imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($new_image, $this->_image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);
$this->_image = $new_image;
}
}
public function displayImage()
{
if($this->_image)
{
header('Content-type: image/jpeg');
imagejpeg($this->_image, NULL, 100);
imagedestroy($this->_image);
}
}
}
?>