This repository was archived by the owner on Dec 10, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlugin.php
More file actions
94 lines (90 loc) · 3.16 KB
/
Plugin.php
File metadata and controls
94 lines (90 loc) · 3.16 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
<?php
namespace PHPoole;
use PHPoole\Utils;
/**
* PHPoole plugin Gallery
*/
Class Gallery extends Plugin
{
const RESIZE_W = 1024;
const RESIZE_H = 768;
public function postloopLoadPages($e)
{
$params = $e->getParams();
extract($params);
$gallery = array();
if (isset($pageInfo['gallery']) && !empty($pageInfo['gallery'])) {
$galleryIterator = new \FilesystemIterator($pageInfo['gallery']);
foreach ($galleryIterator as $galleryFile) {
if ($galleryFile->isFile()
&& (strtolower($galleryFile->getExtension()) == 'jpg'
|| strtolower($galleryFile->getExtension()) == 'png'))
{
$gallery[] = array(
'name' => Utils\slugify($galleryFile->getBasename($galleryFile->getExtension())) . '.' . $galleryFile->getExtension(),
'extension' => $galleryFile->getExtension(),
'filepath' => $galleryFile->getPathname(),
);
}
}
$pageData['gallery'] = $gallery;
}
return compact('pageInfo', 'pageIndex', 'pageData');
}
public function postloopGenerate($e)
{
$phpoole = $e->getTarget();
$params = $e->getParams();
extract($params);
if (isset($page['gallery']) && !empty($page['gallery'])) {
foreach ($page['gallery'] as $image) {
copy($image['filepath'], $phpoole->getWebsitePath() . '/' . $page['path'] . '/' . $image['name']);
$phpoole->addMessage(sprintf("Copy %s", $image['name']));
$this->imageResize($phpoole->getWebsitePath() . '/' . $page['path'] . '/' . $image['name'], self::RESIZE_W, self::RESIZE_H);
$phpoole->addMessage(sprintf("Resize %s", $image['name']));
}
}
}
private function imageResize($file, $w, $h, $crop=FALSE)
{
$fileInfo = new \SplFileInfo($file);
$fileExt = $fileInfo->getExtension();
list($width, $height) = getimagesize($file);
$r = $width / $height;
if ($crop) {
if ($width > $height) {
$width = ceil($width-($width*($r-$w/$h)));
}
else {
$height = ceil($height-($height*($r-$w/$h)));
}
$newwidth = $w;
$newheight = $h;
}
else {
if ($w/$h > $r) {
$newwidth = $h*$r;
$newheight = $h;
}
else {
$newheight = $w/$r;
$newwidth = $w;
}
}
if ($fileExt == 'jpg') {
$src = imagecreatefromjpeg($file);
}
elseif ($fileExt == 'png') {
$src = imagecreatefrompng($file);
}
$dst = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagedestroy($src);
if ($fileExt == 'jpg') {
imagejpeg($dst, $file, 100);
}
elseif ($fileExt == 'png') {
imagepng($dst, $file, 0);
}
}
}