Skip to content

Commit eb1a932

Browse files
committed
Finish resize
0 parents  commit eb1a932

File tree

1 file changed

+110
-0
lines changed

1 file changed

+110
-0
lines changed

ImageResize.php

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
<?php
2+
class ImageResize
3+
{
4+
private $path;
5+
private $filename;
6+
private $extension;
7+
private $width;
8+
private $height;
9+
10+
private $rawResource;
11+
private $newResource;
12+
13+
public function __construct($path, $width = 170, $height = 128, $sourceFormat = null)
14+
{
15+
16+
// File and new size
17+
$this->path = $path;
18+
$this->width = $width;
19+
$this->height = $height;
20+
21+
if (is_null($sourceFormat)) {
22+
list($this->filename,$this->extension) = explode(".",$path);
23+
} else {
24+
$this->extension = $sourceFormat;
25+
}
26+
27+
if(!in_array($this->extension,['png', 'jpg', 'gif'])){
28+
trigger_error('no supported format.');
29+
}
30+
31+
$this->generate();
32+
}
33+
34+
private function generate(){
35+
// Get new sizes
36+
list($oldWidth, $oldHeight) = getimagesize($this->path);
37+
38+
$condition = $oldWidth / $oldHeight;
39+
$resizeWidthPercent = $this->width / $oldWidth;
40+
$resizeHeightPercent = $this->height / $oldHeight;
41+
42+
$offsetHeight = 0;
43+
$offsetWidth = 0;
44+
45+
if ($condition < ($this->width / $this->height)){
46+
$offsetHeight = ( $resizeWidthPercent * $oldHeight - $this->height ) * 0.5 / ($resizeWidthPercent);
47+
} else {
48+
$offsetWidth = ( $resizeHeightPercent * $oldWidth - $this->width ) * 0.5 / ($resizeHeightPercent);
49+
}
50+
51+
// Load
52+
$this->newResource = imagecreatetruecolor($this->width, $this->height);
53+
54+
55+
switch ($this->extension){
56+
case 'jpg' : $this->rawResource = imagecreatefromjpeg($this->path); break;
57+
case 'png' : $this->rawResource = imagecreatefrompng($this->path); break;
58+
case 'gif' : $this->rawResource = imagecreatefromgif($this->path); break;
59+
}
60+
61+
// Resize
62+
imagecopyresized($this->newResource, $this->rawResource, 0, 0, $offsetWidth, $offsetHeight, $this->width, $this->height, $oldWidth - 2 * $offsetWidth, $oldHeight - 2 * $offsetHeight);
63+
64+
}
65+
66+
public function save($path, $format = null){
67+
68+
if (is_null($format)) {
69+
list($filename,$extension) = explode(".",$path);
70+
} else {
71+
$extension = $format;
72+
}
73+
74+
if(!in_array($extension,['png', 'jpg', 'gif'])){
75+
trigger_error('no supported format.');
76+
}
77+
switch ($extension){
78+
case 'jpg' :
79+
imagejpeg($this->newResource, $path);
80+
break;
81+
case 'png' :
82+
imagepng($this->newResource, $path);
83+
break;
84+
case 'gif' :
85+
imagegif($this->newResource, $path);
86+
break;
87+
}
88+
89+
}
90+
91+
public function render(){
92+
switch ($this->extension){
93+
case 'jpg' :
94+
header('Content-Type: image/jpeg');
95+
imagejpeg($this->newResource);
96+
break;
97+
case 'png' :
98+
header('Content-Type: image/png');
99+
imagepng($this->newResource);
100+
break;
101+
case 'gif' :
102+
header('Content-Type: image/gif');
103+
imagegif($this->newResource);
104+
break;
105+
}
106+
}
107+
}
108+
109+
$a = new ImageResize('test.jpg',170,128);
110+
$a->save('a.png');

0 commit comments

Comments
 (0)