Skip to content

Instantly share code, notes, and snippets.

@rhcarlosweb
Last active May 25, 2016 18:52
Show Gist options
  • Save rhcarlosweb/6bef38eb6f719cbd8059 to your computer and use it in GitHub Desktop.
Save rhcarlosweb/6bef38eb6f719cbd8059 to your computer and use it in GitHub Desktop.
WordPress Sharpen Image after Crop

Sharpen image after crop

This function sharpen images after WordPress crop, it's cool for images with small sizes width and height, make more visible and less blur, so more quality 😬, but test first, because on some type of images make a low quality image

Usage

Insert the function on your functions.php

<?php
/**
* Aplica sharpen nas imagens que serão recortadas
* @version 1.0
*/
function foxtemas_sharpen_image( $resized_file ) {
$image = imagecreatefromstring( file_get_contents( $resized_file ) );
$size = @getimagesize( $resized_file );
if ( !$size )
return new WP_Error('invalid_image', __('Could not read image size'), $file);
list($orig_w, $orig_h, $orig_type) = $size;
switch ( $orig_type ) {
case IMAGETYPE_JPEG:
$matrix = array(
array(apply_filters('sharpen_resized_corner',-1.2), apply_filters('sharpen_resized_side',-1), apply_filters('sharpen_resized_corner',-1.2)),
array(apply_filters('sharpen_resized_side',-1), apply_filters('sharpen_resized_center',20), apply_filters('sharpen_resized_side',-1)),
array(apply_filters('sharpen_resized_corner',-1.2), apply_filters('sharpen_resized_side',-1), apply_filters('sharpen_resized_corner',-1.2)),
);
$divisor = array_sum(array_map('array_sum', $matrix));
$offset = 0;
imageconvolution($image, $matrix, $divisor, $offset);
imagejpeg($image, $resized_file,apply_filters( 'jpeg_quality', 90, 'edit_image' ));
break;
case IMAGETYPE_PNG:
return $resized_file;
case IMAGETYPE_GIF:
return $resized_file;
}
// we don't need images in memory anymore
imagedestroy( $image );
return $resized_file;
}
add_filter('image_make_intermediate_size', 'foxtemas_sharpen_image',900);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment