Skip to content

Instantly share code, notes, and snippets.

@erikyo
Created May 1, 2020 00:37
Show Gist options
  • Save erikyo/428a8f4b04c057945116e70fd2891c0c to your computer and use it in GitHub Desktop.
Save erikyo/428a8f4b04c057945116e70fd2891c0c to your computer and use it in GitHub Desktop.
Force wordpress to generate a square intermediate image resize without hard crop (wp crops image, but i don't need this, I need some extra white space outside)
<?php
add_filter('wp_generate_attachment_metadata', 'custom_crop');
function custom_crop($metadata) {
$square_image_size = 320;
$uploads = wp_upload_dir();
$file = path_join( $uploads['basedir'], $metadata['file'] ); // original image file
list( $year, $month, $filename ) = explode( '/', $metadata['file'] );
list( $fname, $fext ) = explode( '.', $filename );
$crop_filename = "{$fname}-{$square_image_size}-square-crop.{$fext}";
$target = path_join( $uploads['basedir'], "{$year}/{$month}/{$crop_filename}" ); // intermediate size file
// find the long edge
$image_long_edge = $metadata['width'] >= $metadata['height'] ? $metadata['width'] : $metadata['height'];
$image_long_ratio = $image_long_edge / $square_image_size;
if ($fext === 'jpg' || $fext === 'jpeg') {
$image = imagecreatefromjpeg($file); // original image resource
} else if ($fext === 'png') {
$image = imagecreatefrompng($file); // original image resource
}
if ($image) {
// create a blank image to fill
$image_target = wp_imagecreatetruecolor( $square_image_size, $square_image_size );
if ($fext === 'png') {
// Fill the image with transparent color
imagesavealpha($image_target, true);
// set background to transparent
$background_color = imagecolorallocatealpha( $image_target, 0, 0, 0, 127 );
} else {
// set background to white
$background_color = imagecolorallocate($image_target, 255, 255, 255);
}
imagefill($image_target, 0, 0, $background_color);
if ($metadata['width'] >= $metadata['height']) {
imagecopyresampled($image_target, $image, 0, (($square_image_size - ($metadata['height'] / $image_long_ratio))/2), 0, 0, $square_image_size, ($metadata['height'] / $image_long_ratio), $metadata['width'], $metadata['height'] );
} else {
imagecopyresampled($image_target, $image, (($square_image_size - ($metadata['width'] / $image_long_ratio))/2), 0, 0, 0, ($metadata['width'] / $image_long_ratio), $square_image_size,$metadata['width'], $metadata['height'] );
}
}
// write the square crop image to disk
if ($fext === 'jpg' || $fext === 'jpeg') {
imagejpeg( $image_target, $target, apply_filters( 'jpeg_quality', 90, 'image_resize' ) );
} else if ($fext === 'png') {
imagepng( $image_target, $target );
}
// override photo generated meta
$metadata['sizes']['square-crop']['file'] = $crop_filename;
$metadata['sizes']['square-crop']['width'] = $square_image_size;
$metadata['sizes']['square-crop']['height'] = $square_image_size;
// Destroy image
imagedestroy($image_target);
return $metadata;
}
add_filter( 'image_size_names_choose', 'square_custom_size' );
function square_custom_size( $sizes ) {
return array_merge( $sizes, array(
'square-crop' => __( 'Square Crop' ),
) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment