Skip to content

Instantly share code, notes, and snippets.

@soulreverie
Created February 1, 2019 19:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soulreverie/0f25741296173846361c69cfb57bd671 to your computer and use it in GitHub Desktop.
Save soulreverie/0f25741296173846361c69cfb57bd671 to your computer and use it in GitHub Desktop.
Regenerates Thumbnail PDFs to ensure that PDFs without a background are filled with white and not black.
add_filter( 'wp_generate_attachment_metadata', 'tdp_create_pdf_thumbnail', 10, 2 );
function tdp_create_pdf_thumbnail( $metadata, $attachment_id ) {
//Get the attachment/post object
$attachment_obj = get_post( $attachment_id );
//Check for mime type pdf
if( 'application/pdf' == get_post_mime_type( $attachment_obj ) ) {
//Get attachment URL http://yousite.org/wp-content/uploads/yourfile.pdf
$attachment_url = wp_get_attachment_url($attachment_id);
//Get attachment path /some/folder/on/server/wp-content/uploads/yourfile.pdf
$attachment_path = get_attached_file($attachment_id );
$attachment_filename = basename ( get_attached_file($attachment_id) );
//By adding [0] the first page gets selected, important because otherwise multi paged files wont't work
$pdf_source = $attachment_path.'[0]';
//Thumbnail format
$tn_format = 'jpg';
// Get All Wordpress Image Sizes
global $_wp_additional_image_sizes;
$media_sizes = get_intermediate_image_sizes();
// Setup to overwrite the Full Size thumbnail
array_push($media_sizes, "full");
// Loop through all Images Sizes
foreach ( $media_sizes as $_size ) {
if ( in_array( $_size, array('thumbnail', 'medium', 'medium_large', 'large') ) ) {
$width = get_option($_size . '_size_w');
$height = get_option($_size . '_size_h');
$crop = (bool) get_option( $_size . '_crop' );
} else if(in_array( $_size, array('full') )){
$width = 0;
$height = 1080;
$crop = 0;
} elseif ( isset( $_wp_additional_image_sizes[ $_size ] ) ) {
$width = $_wp_additional_image_sizes[ $_size ]['width'];
$height = $_wp_additional_image_sizes[ $_size ]['height'];
$crop = $_wp_additional_image_sizes[ $_size ]['crop'];
}
$img = new Imagick();
$img->setResolution(200,200);
$img->readImage($pdf_source);
$img->setImageBackgroundColor('white');
$img->setImageAlphaChannel(11); // Imagick::ALPHACHANNEL_REMOVE
$img = $img->mergeImageLayers(Imagick::LAYERMETHOD_FLATTEN);
if( $height != 0 && $width != 0){
$img->scaleImage(0,$height);
} else if( $height != 0 || $width == 0){
$img->scaleImage(0,$height);
} else if( $height == 0 || $width !== 0){
$img->scaleImage($width,0);
} else {
$img->scaleImage(1000,0);
}
$imgWidth = $img->getImageWidth();
$imgHeight = $img->getImageHeight();
$img->setImageFormat('jpg');
if($_size === 'full'){
// Thumbnail output as path + size + format
$thumb_out = substr($attachment_path, 0, -4).'-pdf.'.$tn_format;
$thumb_filename = substr($attachment_filename, 0, -4).'-pdf.'.$tn_format;
} else {
// Thumbnail output as path + size + format
$thumb_out = substr($attachment_path, 0, -4).'-pdf-'.$imgWidth.'x'.$imgHeight.'.'.$tn_format;
$thumb_filename = substr($attachment_filename, 0, -4).'-pdf-'.$imgWidth.'x'.$imgHeight.'.'.$tn_format;
}
// Write new image
$img->writeImages($thumb_out, false);
//Add thumbnail URL as metadata of pdf attachment
$metadata['sizes'][$_size]['file'] = $thumb_filename;
$metadata['sizes'][$_size]['width'] = $imgWidth;
$metadata['sizes'][$_size]['height'] = $imgHeight;
$metadata['sizes'][$_size]['mime-type'] = 'image/jpeg';
}
}
return $metadata;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment