-
Notifications
You must be signed in to change notification settings - Fork 2
/
picturebuilder.php
105 lines (89 loc) · 3.84 KB
/
picturebuilder.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
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
/**
* picture-builder plugin
* @author web-tiki
* @copyright Copyright (C) 2018 web-tiki
* @license Apache License Version 2.0, January 2004 http://www.apache.org/licenses/
* Website https://web-tiki.com
*/
// no direct access
defined( '_JEXEC' ) or die( 'Restricted access' );
class PlgContentPictureBuilder extends JPlugin {
public static function picturebuilder($imageUrl, $imageAlt, $thumbParams) {
// Check if original image exists
if (!file_exists($imageUrl)) {
echo "<script>console.log( 'Cannot find : " . htmlspecialchars($imageUrl) . "' );</script>";
return false;
}
// Get the params form the plugin backend
$plugin = JPluginHelper::getPlugin('content', 'picturebuilder');
$pluginParams = new JRegistry($plugin->params);
// if the parameters aren't set in the event call, use the parameter helper to generate them from the plugin (backend)
if(!is_array($thumbParams)) {
$IDerror = true;
include 'helpers/params.php';
// stop here and echo an error in the console if the called ID doesn't match any ID set in the backend
if ($IDerror) {
echo "<script>console.log( 'Error. Called ID doesnt match any backend parameter ID: " . $thumbParams . " for image " . htmlspecialchars($imageUrl) . " ' );</script>";
return false;
}
}
// Get the original image info and define ratio for HD images
// TODO maybe better to use the imagick library to get the original image info + size
$imageInfo = pathinfo($imageUrl);
$imageName = $imageInfo['filename'];
$imageExtension = '.' . $imageInfo['extension'];
$thumbDir = $pluginParams['thumbFolder'] . $imageInfo['dirname'];
$upscaleHDthumbs = 1.5;
list($imageWidth, $imageHeight) = getimagesize($imageUrl);
$thumbQuality = $thumbParams['quality'];
$breakPoints = $thumbParams['breakPoints'];
// Build the Thumb width array
$paramWidths = $thumbParams['thumbWidths'];
$thumbWidths = array();
foreach ($paramWidths as $paramWidth) {
array_push($thumbWidths, $paramWidth);
array_push($thumbWidths, floor($paramWidth * $upscaleHDthumbs));
}
// Build the thumb height array
$paramHeights = $thumbParams['thumbHeights'];
$thumbHeights = array();
if ($paramHeights[0]) {
$crop = true;
// If thumb heights are specified get the width and height of image from params
foreach ($paramHeights as $paramHeight) {
array_push($thumbHeights, $paramHeight);
array_push($thumbHeights, floor($paramHeight * $upscaleHDthumbs));
}
} else {
$crop = false;
// If no heights are specified get the width and height of thumbnails from original image aspect ratio
$imageAspectRatio = $imageHeight/$imageWidth;
foreach ($thumbWidths as $thumbWidth) {
$thumbHeight = floor($thumbWidth*$imageAspectRatio);
array_push($thumbHeights, $thumbHeight);
}
}
// Create the thumb paths array
$thumbPaths = array();
foreach ($thumbWidths as $i => $thumbWidth) {
$thumbPath = $thumbDir . '/' . $imageName . '-' . $i . '-' . $thumbWidth . 'x' . $thumbHeights[$i] . 'q' . $thumbQuality . $imageExtension;
array_push($thumbPaths,$thumbPath);
}
// Generate thumbnails if the number 8 doesn't exist
if(!file_exists($thumbPaths[8])){
// Create folders if they don't exixt
if (!file_exists($thumbDir)) { mkdir($thumbDir, 0777, true); }
// Check if imagick is installed
if (extension_loaded('imagick') && $pluginParams['forceGD'] == 0) {
include 'helpers/imagick_thumbnail_generator.php';
} else {
include 'helpers/gd_thumbnail_generator.php';
}
}
// Call the plugin layout to return the picture element
$path = JPluginHelper::getLayoutPath('content', 'picturebuilder');
include $path;
}
}
?>