10

我想在 Wordpress 中使用 ImageMagick 对某个缩略图大小进行一些自定义处理,超出了正常的 Wordpress 功能,但我不太确定如何去做。

所以我添加了我的新缩略图大小:

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

然后这就是我不确定我应该在哪里挂钩的地方。在将缩略图的最终副本保存在 Wordpress 中之前,我想对其进行一些自定义处理。我想要的基本伪代码是:

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;
}

我可以处理 imagemagick 的东西,但我不确定如何/在哪里挂钩这个自定义缩略图处理。

任何建议将不胜感激!

4

3 回答 3

10

感谢@brasofilo 为我指明了正确的方向......

我摸索了一下,弄明白了这个。您可以连接到 wp_generate_attachment_metadata 并进行一些图像处理。

我尝试做的基本操作是将图像调整为特定大小(“品牌”缩略图),然后将该缩略图的画布扩展为具有白色背景的静态高度和宽度。

如果有人有类似的情况,我想我会粘贴一些代码。可以清理它以消除每种图像类型的填充。此代码的一个问题是,如果原始图像大小小于所需的缩略图大小,则不会生成(这是 WordPress 功能)。

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'] . '/' . $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;
}
于 2013-01-08T15:46:44.707 回答
8

在我的图书馆中,我有以下内容:

为生成的缩略图设置自定义名称

拇指名称和裁剪非常有趣的操作。检查此链接中的原始问答。

用途:

自动使用调整大小的图像而不是原件

用途:

  • wp_generate_attachment_metadata

如何自动为缩略图添加圆角?

用途:

如何要求上传的最小图像尺寸?

用途:

按年、月和日组织上传

用途:

于 2013-01-07T19:38:24.683 回答
3

我想建议更改,以使其与 Thumbnails Regenerate Plugin 一起使用

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

之所以如此,是因为 wp_upload_dir 返回当前年份月份的路径,但某些先前上传的图像可能具有不同的路径。

于 2014-11-02T13:33:11.467 回答