all repos — wp-resized2cache @ master

wp-resized2cache.php (view raw)

 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
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
<?php
/*
Plugin Name: wp-resized2cache
Plugin URI: https://github.com/petermolnar/wp-resized2cache
Description: Sharpen, enchance and move resized images to cache folder
Version: 0.5
Author: Peter Molnar <hello@petermolnar.eu>
Author URI: http://petermolnar.eu/
License: GPLv3
*/

/*  Copyright 2016 Peter Molnar ( hello@petermolnar.eu )

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License, version 3, as
	published by the Free Software Foundation.

	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

namespace WP_RESIZED2CACHE;

define ( 'WP_RESIZED2CACHE\CACHENAME', 'cache' );

define ( 'WP_RESIZED2CACHE\CACHE', \WP_CONTENT_DIR . DIRECTORY_SEPARATOR
	. CACHENAME . DIRECTORY_SEPARATOR );

\register_activation_hook( __FILE__ , 'WP_RESIZED2CACHE\plugin_activate' );

\add_action( 'init', 'WP_RESIZED2CACHE\init' );
\add_action( 'delete_attachment', 'WP_RESIZED2CACHE\delete_from_cache' );

function init () {
	if ( ! is_dir( CACHE ) ) {
		if ( ! mkdir( CACHE ) ) {
			debug('failed to create ' . CACHE, 4);
		}
	}

	// set higher jpg quality
	\add_filter( 'jpeg_quality', 'WP_RESIZED2CACHE\jpeg_quality' );
	\add_filter( 'wp_editor_set_quality', 'WP_RESIZED2CACHE\jpeg_quality' );

	// sharpen resized images on upload
	\add_filter( 'image_make_intermediate_size',
		'WP_RESIZED2CACHE\fix_location', 10 );

	add_filter( 'wp_get_attachment_image_src',
		'WP_RESIZED2CACHE\wp_get_attachment_image_src', 1, 4 );

}

/**
 * activate hook
 */
function plugin_activate() {
	if ( version_compare( phpversion(), 5.3, '<' ) ) {
		die( 'The minimum PHP version required for this plugin is 5.3' );
	}
}

function sizes() {
	return array (
		//90  => 's',
		360 => 'm',
		540 => 'n',
		720 => 'z',
		980 => 'c',
		1280 => 'b',
	);
}


/**
 * called on attachment deletion and takes care of removing the moved files
 *
 */
function delete_from_cache ( $aid = null ) {
	debug( "DELETE is called and aid is: {$aid}", 5 );
	if ($aid === null)
		return false;

	$attachment = \get_post( $aid );

	if ( is_post($attachment)) {
		$meta = \wp_get_attachment_metadata($aid);

		if ( isset( $meta['sizes'] ) && ! empty( $meta['sizes'] ) ) {
			foreach ( $meta['sizes'] as $size => $data ) {
				$file = CACHE . DIRECTORY_SEPARATOR . $data['file'];
				if ( isset( $data['file'] ) && is_file( $file ) ) {
					debug( " removing {$file}", 5 );
					unlink ( $file );
				}
			}
		}
	}

	return $aid;
}

/**
 * better jpgs
 */
function jpeg_quality () {
	$jpeg_quality = (int)92;
	return $jpeg_quality;
}


/**
 *
 */
function sharpen( $path ) {
	if ( ! class_exists( '\Imagick' ) )
		return;


	$dimensions = @getimagesize( $path );

	if ( ! isset( $dimensions[2] ) || IMAGETYPE_JPEG != $dimensions[2] )
		return;

	debug( "sharpening {$path}", 6 );
	try {
		$imagick = new \Imagick( $path );
		$imagick->unsharpMaskImage( 0, 0.5, 1, 0 );
		$imagick->setImageFormat( "jpg" );
		$imagick->setImageCompression( \Imagick::COMPRESSION_JPEG );
		$imagick->setImageCompressionQuality( jpeg_quality() );
		$imagick->setInterlaceScheme( \Imagick::INTERLACE_PLANE );

		// this is for watermarking
		$imagick = \apply_filters(
			"wp_resized2cache_imagick",
			$imagick,
			$path
		);

		$imagick->writeImage( $path );
		$imagick->destroy();
	}
	catch (Exception $e) {
		debug( 'something went wrong with imagemagick: ',  $e->getMessage(), 4 );
		return;
	}
}

/**
 *
 */
function link_largest_fallback ( $cached ) {

	$r = pathinfo( $cached );
	// trying to link the largest, resized image to the same name
	// as the original image is with; this is to make fallbacks easier
	$guess_original = preg_replace( '/^(.*)-[0-9]{2,4}x[0-9]{2,4}$/',
		'\\1', $r['filename'] ) . '.' . $r['extension'];

	$upload_dir = \wp_upload_dir();
	$linked = CACHE . DIRECTORY_SEPARATOR . $guess_original;
	$original = $upload_dir['basedir'] . DIRECTORY_SEPARATOR . $guess_original;

	// only continue if we could identify the original
	if ( ! file_exists( $original ) )
		return;

	// no link yet; create one
	if ( ! file_exists( $linked ) ) {
		symlink( $cached, $linked );
		debug ( "{$linked} linked to {$cached}", 6 );
		return;
	}

	// link exists, so compare the linked one's size with the current ones
	$linked_size = getimagesize( $linked );
	$cached_size = getimagesize( $cached );
	$compare = ( $cached_size[0] > $cached_size[1] ) ? 0 : 1;
	if ( $cached_size[ $compare ] > $linked_size[ $compare ] ) {
		unlink( $linked );
		symlink( $cached, $linked );
		debug ( "{$linked} linked to {$cached}, because it's larger", 6 );
	}

	return;
}

/**
 *
 */
function link_simple_name( $cached ) {

	$r = pathinfo( $cached );

		// try to find the relevant size
	$dimensions = @getimagesize( $cached );

	$match_by = ( $dimensions[0] > $dimensions[1] ) ? 0 : 1;

	$endings = sizes();

	//$endings = array_flip( $endings );

	if ( ! isset( $endings[ $dimensions[ $match_by ] ] ) )
		return;

	$simple = CACHE .
		preg_replace( '/^(.*)-[0-9]{2,4}x[0-9]{2,4}$/', '\\1', $r['filename'] )
		. '_' . $endings[ $dimensions[ $match_by ] ]
		. '.' . $r['extension'];

	if ( is_file( $simple ) )
		unlink( $simple );

	symlink( $cached, $simple );
	debug ( "{$cached} was linked to {$simple}" );

	return;
}

/**
 * adaptive sharpen images w imagemagick
 */
function fix_location( $resized ) {
	sharpen( $resized );

	$r = pathinfo( $resized );
	$cached = CACHE . $r['filename'] . '.' . $r['extension'];

	// move the file to cache
	if ( copy( $resized, $cached ) )
		unlink( $resized );

	link_largest_fallback( $cached );
	link_simple_name( $cached );


	return $cached;
}

/**
 *
 * debug messages; will only work if WP_DEBUG is on
 * or if the level is LOG_ERR, but that will kill the process
 *
 * @param string $message
 * @param int $level
 *
 * @output log to syslog | wp_die on high level
 * @return false on not taking action, true on log sent
 */
function debug( $message, $level = LOG_NOTICE ) {
	if ( empty( $message ) )
		return false;

	if ( @is_array( $message ) || @is_object ( $message ) )
		$message = json_encode($message);

	$levels = array (
		LOG_EMERG => 0, // system is unusable
		LOG_ALERT => 1, // Alert 	action must be taken immediately
		LOG_CRIT => 2, // Critical 	critical conditions
		LOG_ERR => 3, // Error 	error conditions
		LOG_WARNING => 4, // Warning 	warning conditions
		LOG_NOTICE => 5, // Notice 	normal but significant condition
		LOG_INFO => 6, // Informational 	informational messages
		LOG_DEBUG => 7, // Debug 	debug-level messages
	);

	// number for number based comparison
	// should work with the defines only, this is just a make-it-sure step
	$level_ = $levels [ $level ];

	// in case WordPress debug log has a minimum level
	if ( defined ( '\WP_DEBUG_LEVEL' ) ) {
		$wp_level = $levels [ \WP_DEBUG_LEVEL ];
		if ( $level_ > $wp_level ) {
			return false;
		}
	}

	// ERR, CRIT, ALERT and EMERG
	if ( 3 >= $level_ ) {
		\wp_die( '<h1>Error:</h1>' . '<p>' . $message . '</p>' );
		exit;
	}

	$trace = debug_backtrace();
	$caller = $trace[1];
	$parent = $caller['function'];

	if (isset($caller['class']))
		$parent = $caller['class'] . '::' . $parent;

	return error_log( "{$parent}: {$message}" );
}

/**
 * test if an object is actually a post
 */
function is_post ( &$post ) {
	if ( ! empty( $post ) &&
			 is_object( $post ) &&
			 isset( $post->ID ) &&
			 ! empty( $post->ID ) )
		return true;

	return false;
}

/**
 *
 */
function find_thid ( $resized ) {

	global $wpdb;
	$dbname = "{$wpdb->prefix}postmeta";
	$req = false;

	$q = $wpdb->prepare( "SELECT `post_id` FROM `{$dbname}` WHERE "
		."`meta_value` LIKE '%%%s%%' AND "
		."`meta_key` = '_wp_attachment_metadata' LIMIT 1",
	basename( $resized ) );

	try {
		$req = $wpdb->get_var( $q );
	}
	catch (Exception $e) {
		debug('Something went wrong: ' . $e->getMessage(), 4);
	}

	return $req;
}

/**
 *
 *  fix the wp content url/dir when trying to get an image src
 *
 */
function wp_get_attachment_image_src ( $image, $thid, $size, $icon ) {
	debug ( $image );
	$by = ( $image[1] > $image[2] ) ? $image[1] : $image[2];

	$endings = sizes();
	if ( isset( $endings[ $by ] ) ) {
		$simple = pathinfo( $image[0] );
		$simple = CACHE .
		preg_replace( '/^(.*)-[0-9]{2,4}x[0-9]{2,4}$/', '\\1', $simple['filename'] )
			. '_' . $endings[ $s ]
			. '.' . $simple['extension'];

		$image[0] = $simple;
	}
	else {
		$upload_dir = \wp_upload_dir();
		$cached = str_replace( trim( $upload_dir['baseurl'], '/' ),
			CACHENAME, $image[0] );

		if ( is_file( \WP_CONTENT_DIR . $cached ) )
			$image[0] = $cached;
	}

	return $image;
}