Wie man sich in die WordPress-Thumbnail-Generierung einklinkt

Lesezeit: 5 Minuten

Ich möchte mit ImageMagick eine benutzerdefinierte Verarbeitung einer bestimmten Thumbnail-Größe in WordPress über die normale WordPress-Funktionalität hinaus durchführen und bin mir nicht ganz sicher, wie ich das machen soll.

Also füge ich meine neue Thumbnail-Größe hinzu:

add_image_size( 'new-thumb', 100, 100 );

Und dann bin ich mir nicht sicher, wo ich einhaken soll. Bevor die endgültige Kopie des Thumbnails in WordPress gespeichert wird, möchte ich eine benutzerdefinierte Verarbeitung daran vornehmen. Grundlegender Pseudo-Code für das, was ich will, ist:

The_hook_or_action_that_fires_when_a_thumbnail_is_saved() {

    if (<Thumbnail Being Generated> == 'new-thumb') {
      $thumb_file="the thumbnail image we are about to save";
      $thumbfile="do some imagemagic stuff here";
    }

    save_thumbnail;
}

Ich kann mit dem Imagemagick-Zeug umgehen, bin mir aber nicht sicher, wie / wo ich diese benutzerdefinierte Thumbnail-Verarbeitung einbinden soll.

Jeder Rat wäre sehr willkommen!

Danke @brasofilo, dass du mich in die richtige Richtung gewiesen hast…

Ich habe ein bisschen herumgestöbert und bin auf dieses hier gestoßen. Sie können sich in wp_generate_attachment_metadata einklinken und einige Bildmanipulationen vornehmen.

Die Grundlagen dessen, was ich zu tun versucht habe, sind die Größenänderung eines Bildes auf eine bestimmte Größe (“Marken”-Miniaturansicht) und die Erweiterung der Leinwand für diese Miniaturansicht auf eine statische Höhe und Breite mit einem weißen Hintergrund.

Falls jemand eine ähnliche Situation hat, dachte ich, ich würde etwas Code einfügen. Es könnte bereinigt werden, um die Vervollständigung für jeden Bildtyp zu entfernen. Das einzige Problem mit diesem Code ist, dass, wenn die ursprüngliche Bildgröße kleiner als die gewünschte Thumbnail-Größe ist, es nicht generiert wird (was die WordPress-Funktionalität ist).

add_image_size( 'brands', 200, 168 );

add_filter('wp_generate_attachment_metadata','replace_uploaded_image');
function replace_uploaded_image($image_data) {

    // if there is no brands image : return
    if ( !isset($image_data['sizes']['brands']) ) 
        return $image_data;

    //Set our desired static height / width (200px * 168px)
    $staticWidth  = 200;
    $staticHeight = 168;

    // paths to the uploaded image and the large image
    $upload_dir            = wp_upload_dir();
    $brands_image_location = $upload_dir['path'] . "https://stackoverflow.com/" . $image_data['sizes']['brands']['file'];

    // set our temp image file
    $brands_image_location_tmp = "$brands_image_location.tmp";

    // get the attributes of the source image
    list($imageWidth, $imageHeight, $imageType, $imageAttr) = getimagesize($brands_image_location);

    // there are different php functions depending on what type of image it is, so check the type
    switch($imageType) {
        //GIF
        case 1: 
            //Create a 200x168 white canvas
            $newimage=imagecreatetruecolor($staticWidth,$staticHeight);
            $white=imagecolorallocate($newimage, 255, 255, 255);
            imagefill($newimage,0,0,$white);

            //Calculate where the image should start so its centered
            if($imageWidth == $staticWidth)  { $x_pos = 0; } else { $x_pos = round( ($staticWidth - $imageWidth) / 2 ); }
            if($imageHeight == $staticHeight) { $y_pos = 0; } else { $y_pos = round( ($staticHeight - $imageHeight) / 2 ); }

            //Copy the source image to the new canvas
            $src = imagecreatefromgif($brands_image_location);
            imagecopy($newimage, $src, $x_pos, $y_pos, 0, 0, $imageWidth, $imageHeight);
            imagegif($newimage,$brands_image_location_tmp);

            // delete the uploaded image
            unlink($brands_image_location);

            // rename the temporary brands image
            rename($brands_image_location_tmp, $brands_image_location);

            // update image metadata and return them
            $image_data['sizes']['brands']['width'] = $staticWidth;
            $image_data['sizes']['brands']['height'] = $staticHeight;

            break;

        //JPG
        case 2:
            //Create a 200x168 white canvas
            $newimage=imagecreatetruecolor($staticWidth,$staticHeight);
            $white=imagecolorallocate($newimage, 255, 255, 255);
            imagefill($newimage,0,0,$white);

            //Calculate where the image should start so its centered
            if($imageWidth == $staticWidth)  { $x_pos = 0; } else { $x_pos = round( ($staticWidth - $imageWidth) / 2 ); }
            if($imageHeight == $staticHeight) { $y_pos = 0; } else { $y_pos = round( ($staticHeight - $imageHeight) / 2 ); }

            //Copy the source image to the new canvas
            $src = imagecreatefromjpeg($brands_image_location);
            imagecopy($newimage, $src, $x_pos, $y_pos, 0, 0, $imageWidth, $imageHeight);
            imagejpeg($newimage,$brands_image_location_tmp);

            // delete the uploaded image
            unlink($brands_image_location);

            // rename the temporary brands image
            rename($brands_image_location_tmp, $brands_image_location);

            // update image metadata and return them
            $image_data['sizes']['brands']['width'] = $staticWidth;
            $image_data['sizes']['brands']['height'] = $staticHeight;

            break;

        //PNG
        case 3:
            //Create a 200x168 white canvas
            $newimage=imagecreatetruecolor($staticWidth,$staticHeight);
            $white=imagecolorallocate($newimage, 255, 255, 255);
            imagefill($newimage,0,0,$white);

            //Calculate where the image should start so its centered
            if($imageWidth == $staticWidth)  { $x_pos = 0; } else { $x_pos = round( ($staticWidth - $imageWidth) / 2 ); }
            if($imageHeight == $staticHeight) { $y_pos = 0; } else { $y_pos = round( ($staticHeight - $imageHeight) / 2 ); }

            //Copy the source image to the new canvas
            $src = imagecreatefrompng($brands_image_location);
            imagecopy($newimage, $src, $x_pos, $y_pos, 0, 0, $imageWidth, $imageHeight);
            imagepng($newimage,$brands_image_location_tmp);

            // delete the uploaded image
            unlink($brands_image_location);

            // rename the temporary brands image
            rename($brands_image_location_tmp, $brands_image_location);

            // update image metadata and return them
            $image_data['sizes']['brands']['width'] = $staticWidth;
            $image_data['sizes']['brands']['height'] = $staticHeight;

            break;

    }

    return $image_data;
}

  • Brillant! Funktioniert noch. Es gibt jedoch eine wichtige Änderung, die in dieser Antwort von @Albin erwähnt wird: stackoverflow.com/a/26699842

    – Thomas Ebert

    12. Mai 2021 um 13:26 Uhr

Benutzeravatar von brasofilo
brasophilo

In meiner Bibliothek habe ich folgendes:

Legen Sie einen benutzerdefinierten Namen für generierte Miniaturansichten fest

Sehr interessante Manipulation von Daumennamen und Ernte. Überprüfen Sie die ursprünglichen Fragen und Antworten, die in diesem verlinkt sind.

Verwendet:

Vergrößerte Bilder automatisch anstelle von Originalen verwenden

Verwendet:

  • wp_generate_attachment_metadata

Wie füge ich Thumbnails automatisch abgerundete Ecken hinzu?

Verwendet:

Wie benötige ich eine Mindestgröße für das Hochladen von Bildern?

Verwendet:

Organisieren Sie Uploads nach Jahr, Monat und Tag

Verwendet:

Ich würde gerne eine Änderung vorschlagen, damit es mit dem Thumbnails Regenerate Plugin funktioniert

$upload_dir = wp_upload_dir();
$brands_image_location = $upload_dir['basedir']."https://stackoverflow.com/".dirname($image_data['file']) . "https://stackoverflow.com/" . $image_data['sizes']['brands']['file'];
$brands_image_location = $brands_image_location.'.tmp';

Das liegt daran, dass wp_upload_dir den Pfad für den aktuellen Jahr-Monat zurückgibt, aber einige zuvor hochgeladene Bilder einen anderen Pfad haben könnten.

  • Vielen Dank! Diese Antwort ist eher ein Kommentar zu einer Antwort von @ChuckMac, die hier zu finden ist stackoverflow.com/a/14218764

    – Thomas Ebert

    12. Mai 2021 um 13:25 Uhr


1394610cookie-checkWie man sich in die WordPress-Thumbnail-Generierung einklinkt

This website is using cookies to improve the user-friendliness. You agree by using the website further.

Privacy policy