Skip to content

Commit

Permalink
v3.3.2
Browse files Browse the repository at this point in the history
- fixed: image display when direct URL method is used and thumbnail already exists in the cache folder
- improved PHP cache on direct-url thumbs creation
  • Loading branch information
LCweb-ita committed Feb 1, 2025
1 parent f080323 commit d6954b5
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 44 deletions.
25 changes: 16 additions & 9 deletions easy_wp_thumbs.php
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
<?php
/**
* Easy WP thumbs v3.3.0
* Easy WP thumbs v3.3.2
* NOTE: Designed for use with PHP version 5.2 and up. Requires at least WP 3.5
*
* @author Luca Montanari (LCweb)
* @copyright 2023 Luca Montanari - https://lcweb.it
* @copyright 2012-2025 Luca Montanari - https://lcweb.it
*
* Licensed under the MIT license
*/
Expand All @@ -13,7 +13,7 @@

// be sure ewpt has not been initialized yet
if(!defined('EWPT_VER')) {
define('EWPT_VER', '3.2.1');
define('EWPT_VER', '3.3.2');
define('EWPT_ERROR_PREFIX', 'Easy WP Thumbs v'.EWPT_VER.' - ');


Expand Down Expand Up @@ -130,6 +130,9 @@
* @return (string) thumbnail URL or error message
*/
function easy_wp_thumb($img_src, $w_jolly = false, $h = false, $quality = 80, $align = 'c', $resize = 1, $canvas_col = 'FFFFFF', $fx = array(), $get_url_if_not_cached = false) {
if(strpos($img_src, '%2F') !== false) {
$img_src = urldecode($img_src);
}

// old retrocompatibility definition way
if(!is_array($w_jolly)) {
Expand All @@ -153,7 +156,15 @@ function easy_wp_thumb($img_src, $w_jolly = false, $h = false, $quality = 80, $a
$ewpt = new easy_wp_thumbs;
$thumb = $ewpt->get_thumb($img_src, $params, $get_url_if_not_cached);

return (!$thumb) ? __('thumb creation failed', 'ewpt_ml') : $thumb;
if(!$thumb) {
if(isset($_GET['ewpt_debug'])) {
foreach($ewpt->errors as $error) {
trigger_error('Easy WP Thumbs v'. EWPT_VER .' - '. $img_src .' - '. $error);
}
}
}

return (!$thumb) ? 'thumb-creation-failed' : $thumb;
}


Expand All @@ -166,17 +177,13 @@ function easy_wp_thumb($img_src, $w_jolly = false, $h = false, $quality = 80, $a
ob_end_clean();
}

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

// check for external leechers
ewpt_helpers::block_external_leechers();

// browser cache based on URL
ewpt_helpers::manage_browser_cache($_SERVER['REQUEST_URI']);


// clean url and get args
$url_arr = explode('?', $_SERVER['REQUEST_URI']);
parse_str($url_arr[1], $params);
Expand Down
36 changes: 18 additions & 18 deletions include/engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public function get_method($path = false) {
}

if(!file_exists($path)) {
$this->errors[] = 'get_method - '. __("path does not exist", 'ewpt_ml');
$this->errors[] = 'get_method - '. esc_html__("path does not exist", 'ewpt_ml');
return false;
}

Expand All @@ -104,7 +104,7 @@ public function is_ready($recursing = false) {
return $this->is_ready(true);
}

$this->errors[] = __("Cache folder doesn't exist", 'ewpt_ml');
$this->errors[] = esc_html__("Cache folder doesn't exist", 'ewpt_ml');
return false;
}

Expand All @@ -122,7 +122,7 @@ public function is_ready($recursing = false) {
// check saved credentials against WP_filesys
else {
if(!$this->creds || !WP_Filesystem($this->creds, $this->cache_dir)) {
$this->errors[] = '01 - WP_filesystem - '. __("connection failed", 'ewpt_ml');
$this->errors[] = '01 - WP_filesystem - '. esc_html__("connection failed", 'ewpt_ml');
return false;
}

Expand All @@ -141,7 +141,7 @@ public function is_ready($recursing = false) {
*/
public function create_file($filename, $contents) {
if(empty($filename) || empty($contents)) {
$this->errors[] = __('Filename or contents are missing', 'ewpt_ml');
$this->errors[] = esc_html__('Filename or contents are missing', 'ewpt_ml');
return false;
}

Expand All @@ -153,12 +153,12 @@ public function create_file($filename, $contents) {
define('FS_METHOD', 'direct');

if(!$this->is_ready()) {
$this->errors[] = '02 - WP_filesystem - '. __('connection failed', 'ewpt_ml');
$this->errors[] = '02 - WP_filesystem - '. esc_html__('connection failed', 'ewpt_ml');
return false;
}
}
else {
$this->errors[] = '02 - WP_filesystem - '. __('connection failed', 'ewpt_ml');
$this->errors[] = '02 - WP_filesystem - '. esc_html__('connection failed', 'ewpt_ml');
return false;
}
}
Expand All @@ -175,7 +175,7 @@ public function create_file($filename, $contents) {
}

if(!$wp_filesystem->put_contents($fullpath, $contents, EWPT_CHMOD_FILE)) {
$this->errors[] = __('Error creating the file', 'ewpt_ml') .' '. $filename;
$this->errors[] = esc_html__('Error creating the file', 'ewpt_ml') .' '. $filename;
return false;
}

Expand All @@ -188,9 +188,9 @@ public function create_file($filename, $contents) {
* Returns the errors
*/
public function get_errors() {
if(count($this->errors) > 0){
if(count($this->errors)){
$html = '
<h2>Easy WP Thumbs - '. __('errors occurred', 'ewpt_ml') .'</h2>
<h2>Easy WP Thumbs - '. esc_html__('errors occurred', 'ewpt_ml') .'</h2>
<ul>';

foreach($this->errors as $error) {
Expand Down Expand Up @@ -252,7 +252,7 @@ class easy_wp_thumbs extends ewpt_connect {
*/
public function get_thumb($img_src, $params = false, $get_url_if_not_cached = false, $stream = false) {
@ini_set('memory_limit','768M');

// connect to WP filesystem
if(!$this->is_ready()) {
return false;
Expand All @@ -272,7 +272,7 @@ public function get_thumb($img_src, $params = false, $get_url_if_not_cached = fa

if(!filter_var($img_src, FILTER_VALIDATE_URL)) {
if(!$img_src || !file_exists($img_src)) {
$this->errors[] = __('WP image not found or invalid image path', 'ewpt_ml');
$this->errors[] = esc_html__('WP image not found or invalid image path', 'ewpt_ml');
return false;
}
}
Expand All @@ -288,7 +288,7 @@ public function get_thumb($img_src, $params = false, $get_url_if_not_cached = fa
}

if(in_array($this->mime, $supported_mimes) === false) {
$this->errors[] = __('File extension not supported', 'ewpt_ml');
$this->errors[] = esc_html__('File extension not supported', 'ewpt_ml');
return false;
}

Expand All @@ -314,7 +314,7 @@ public function get_thumb($img_src, $params = false, $get_url_if_not_cached = fa

//// use the wp image editor
if(!$this->load_image($img_src)) {
$this->errors[] = __('Error loading image', 'ewpt_ml');
$this->errors[] = esc_html__('Error loading image', 'ewpt_ml');
return false;
}

Expand All @@ -328,7 +328,7 @@ public function get_thumb($img_src, $params = false, $get_url_if_not_cached = fa
$img_contents = $this->image_contents();

if( !$this->create_file($this->cache_img_name, $img_contents) ) {
$this->errors[] = __('Error creating thumbnail file', 'ewpt_ml');
$this->errors[] = esc_html__('Error creating thumbnail file', 'ewpt_ml');
return false;
}

Expand Down Expand Up @@ -392,7 +392,7 @@ private function load_image($img_src) {
return true;
}
else {
$this->errors[] = 'WP image editor - '. __('Invalid image data', 'ewpt_ml');
$this->errors[] = 'WP image editor - '. esc_html__('Invalid image data', 'ewpt_ml');
return false;
}
}
Expand Down Expand Up @@ -649,7 +649,7 @@ private function check_source($img_src) {
}

if(!$allowed){
$this->errors[] = __('Image source is not among allowed websites', 'ewpt_ml');
$this->errors[] = esc_html__('Image source is not among allowed websites', 'ewpt_ml');
return false;
}

Expand Down Expand Up @@ -745,10 +745,10 @@ private function return_image($filename, $stream = false, $img_contents = false)
}

// set filename to avoid WP editor issues
$this->editor->ewpt_setup_filename( $this->cache_img_name );
$this->editor->ewpt_setup_filename($cache_fullpath);

$this->stream_img();
die();
exit;
}
}
}
22 changes: 5 additions & 17 deletions include/helpers.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,11 @@ public static function block_external_leechers() {
* @param (string) $img_path
*/
public static function manage_browser_cache($img_path) {
$etag_file = md5($img_path);
$etag_header = (isset($_SERVER['HTTP_IF_NONE_MATCH'])) ? trim($_SERVER['HTTP_IF_NONE_MATCH']) : false;
$cache_duration = 15 * 24 * 60 * 60; // 15 days
$hash = md5($img_path);
$expires = gmdate('U') + $cache_duration;

header("Etag: ". $etag_file);

$seconds_to_cache = 3600 * 24 * 15; // 15 days
$ts = gmdate("D, d M Y H:i:s", time() + $seconds_to_cache) . " GMT";
header("Expires: ". $ts);
header('Cache-Control: must-revalidate');
header("Cache-Control: max-age=3600");
header("Cache-Control: public");
header('Pragma: public');

// check if contents changed. If not, send 304 and exit
if($etag_header == $etag_file) {
header("HTTP/1.1 304 Not Modified");
die();
}
header("Cache-Control: public, max-age=$cache_duration, immutable");
header("Expires: " . gmdate("D, d M Y H:i:s", $expires) . " GMT");
}
}

0 comments on commit d6954b5

Please sign in to comment.