Skip to content
This repository was archived by the owner on Jul 3, 2024. It is now read-only.

Commit 5e940aa

Browse files
Progress
1 parent 2553634 commit 5e940aa

File tree

1 file changed

+195
-0
lines changed

1 file changed

+195
-0
lines changed
Lines changed: 195 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,195 @@
1+
<?php
2+
/*
3+
=====================================================
4+
Easy Cloudinary - by Aaron Gustafson
5+
-----------------------------------------------------
6+
https://www.aaron-gustafson.com/
7+
=====================================================
8+
This extension was created by Aaron Gustafson
9+
(aaron@easy-designs.net)
10+
This work is licensed under the MIT License.
11+
=====================================================
12+
File: pi.easy_cloudinary.php
13+
-----------------------------------------------------
14+
Purpose: Automates swapping of Cloudinary’s image
15+
paths for local ones.
16+
=====================================================
17+
*/
18+
19+
$plugin_info = array(
20+
'pi_name' => 'Easy Cloudinary',
21+
'pi_version' => '1.0',
22+
'pi_author' => 'Aaron Gustafson',
23+
'pi_author_url' => 'https://www.aaron-gustafson.com/',
24+
'pi_description' => 'Automates swapping of Cloudinary’s image paths for local (server-based) ones.',
25+
'pi_usage' => Easy_cloudinary::usage()
26+
);
27+
28+
class Easy_cloudinary {
29+
30+
var $return_data;
31+
var $site_domain;
32+
var $cloudinary_config = '';
33+
34+
/**
35+
* Easy_cloudinary constructor
36+
* stores the config & triggers the processing
37+
*/
38+
function __construct()
39+
{
40+
if ( isset( ee()->config['easy_cloudinary'] ) )
41+
{
42+
# grab the config
43+
$this->cloudinary_config = ee()->config['easy_cloudinary'];
44+
45+
# get the host name
46+
$http_host = strToLower( ee()->input->server('HTTP_HOST') );
47+
$ssl = isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on';
48+
$protocol = $ssl ? 'https://' : 'http://';
49+
$this->site_domain = "{$protocol}{$http_host}";
50+
$this->return_data = $this->convert( ee()->TMPL->tagdata );
51+
}
52+
else
53+
{
54+
$this->return_data = ee()->TMPL->tagdata;
55+
}
56+
} # end Easy_cloudinary constructor
57+
58+
/**
59+
* Easy_cloudinary::convert()
60+
* processes the supplied content based on the configuration
61+
*
62+
* @param str $str - the content to be parsed
63+
*/
64+
function convert( $str='' )
65+
{
66+
$account = $this->cloudinary_config['account'];
67+
$template = $this->cloudinary_config['template'];
68+
69+
# in case we have relative image paths
70+
$current_path = ee()->uri->uri_string();
71+
if ( substr( $current_path, -1, 1 ) != '/' )
72+
{
73+
$current_path = explode( '/', $current_path );
74+
$last = count( $current_path ) - 1;
75+
# if the path is a file…
76+
if ( strpos( $current_path[$last], '.' ) !== FALSE )
77+
{
78+
# remove the file name
79+
$current_path[$last] = '';
80+
}
81+
# otherwise it’s probably a folder
82+
else
83+
{
84+
$current_path[] = '';
85+
}
86+
$current_path = implode( '/', $current_path );
87+
}
88+
89+
# trim
90+
$str = trim( $str );
91+
92+
$img_lookup = '/(<img([^>]*)\/?>)/';
93+
if ( preg_match_all( $img_lookup, $str, $found, PREG_SET_ORDER ) )
94+
{
95+
# loop the matches
96+
foreach ( $found as $instance )
97+
{
98+
$original_img = $instance[1];
99+
$src = '';
100+
$srcset = [];
101+
$other_attributes = [];
102+
103+
# remove the /
104+
if ( substr( $instance[2], -1, 1 ) == '/' )
105+
{
106+
$instance[2] = substr( $instance[2], 0, -1 );
107+
}
108+
109+
# Get all attributes
110+
# Reference: http://stackoverflow.com/questions/138313/how-to-extract-img-src-title-and-alt-from-html-using-php#answer-2937682
111+
$doc = new DOMDocument();
112+
@$doc->loadHTML( $original_img );
113+
$tags = $doc->getElementsByTagName('img');
114+
115+
foreach ( $tags as $tag )
116+
{
117+
foreach ( $tag->attributes as $attribute )
118+
{
119+
$name = $attribute->name;
120+
$value = $attribute->value;
121+
122+
if ( $name == 'src' )
123+
{
124+
$src = $value;
125+
}
126+
if ( $name == 'srcset' )
127+
{
128+
// do nothing (Cloudinary dynamically handles or the template can)
129+
}
130+
else
131+
{
132+
$attributes[$name] = "{$name}=\"{$value}\"";
133+
}
134+
}
135+
}
136+
137+
# enforce an alt attribute
138+
if ( ! isset( $attributes['alt'] ) )
139+
{
140+
$attributes['alt'] = '';
141+
}
142+
143+
# Format the src to include this domain is we don’t have it already
144+
if ( strpos( $src, $this->site_domain ) === FALSE )
145+
{
146+
if ( substr ( $src, 0, 1 ) == '/' )
147+
{
148+
$src = "{$this->site_domain}{$src}";
149+
}
150+
else
151+
{
152+
$src = "{$this->site_domain}{$current_path}{$src}"
153+
}
154+
}
155+
156+
# build the new image
157+
$swap = array(
158+
'account' => $this->account,
159+
'attributes' => implode( ' ', $attributes ),
160+
'image_url' => $src
161+
);
162+
$new_img = ee()->functions->var_swap( $this->template, $swap );
163+
164+
$str = str_replace( $original_img, $new_img, $str );
165+
166+
} # end foreach instance
167+
168+
} # end if match
169+
170+
$this->return_data = $str;
171+
172+
return $this->return_data;
173+
174+
} # end Easy_cloudinary::convert()
175+
176+
/**
177+
* Easy_cloudinary::usage()
178+
* Describes how the plugin is used
179+
*/
180+
function usage()
181+
{
182+
ob_start(); ?>
183+
184+
COMING SOON
185+
186+
<?php
187+
$buffer = ob_get_contents();
188+
ob_end_clean();
189+
return $buffer;
190+
} # end Easy_cloudinary::usage()
191+
192+
} # end Easy_cloudinary
193+
194+
/* End of file pi.easy_cloudinary.php */
195+
/* Location: ./system/expressionengine/third_party/easy_cloudinary/pi.easy_cloudinary.php */

0 commit comments

Comments
 (0)