我正在尝试拍摄现有图像,并通过将照片调整到较小的分辨率来缩小文件大小。到目前为止,我已经尝试了几种方法:
只需在photoshop中编辑照片并重新加载,即可替换现有(或更大)文件Imsanity 插件并将选项设置为小于1024x1024,然后对图像进行大容量调整,但没有失败(但没有成功)Regenerate Thumbnails 插件,并成功运行重建(但没有调整照片大小)
我正在运行多站点安装,WP FTP未启用,无法启用以解决此问题。
我正在尝试拍摄现有图像,并通过将照片调整到较小的分辨率来缩小文件大小。到目前为止,我已经尝试了几种方法:
只需在photoshop中编辑照片并重新加载,即可替换现有(或更大)文件Imsanity 插件并将选项设置为小于1024x1024,然后对图像进行大容量调整,但没有失败(但没有成功)Regenerate Thumbnails 插件,并成功运行重建(但没有调整照片大小)
我正在运行多站点安装,WP FTP未启用,无法启用以解决此问题。
您可以执行多种操作来实现较小的文件大小。让我来引导你通过它。
首先,你可以add custom image sizes 并将尺寸调整到最小。将以下内容放入函数文件中:
function my_insert_custom_image_sizes( $sizes ) {
// get the custom image sizes
global $_wp_additional_image_sizes;
// if there are none, just return the built-in sizes
if ( empty( $_wp_additional_image_sizes ) )
return $sizes;
// add all the custom sizes to the built-in sizes
foreach ( $_wp_additional_image_sizes as $id => $data ) {
// take the size ID (e.g., \'my-name\'), replace hyphens with spaces,
// and capitalise the first letter of each word
if ( !isset($sizes[$id]) )
$sizes[$id] = ucfirst( str_replace( \'-\', \' \', $id ) );
}
return $sizes;
}
function custom_image_setup () {
add_theme_support( \'post-thumbnails\' );
add_image_size( \'small\', 160, 9999 ); // small
add_image_size( \'medium\', 300, 9999 ); // medium
add_image_size( \'large-cropped\', 578, 200, true ); // cropped
add_filter( \'image_size_names_choose\', \'my_insert_custom_image_sizes\' );
}
add_action( \'after_setup_theme\', \'custom_image_setup\' );
所以,my_insert_custom_image_sizes
将自定义图像添加到Media uploader窗口,我在custom_image_setup
.接下来,我们需要为每个尺寸设置JPEG压缩值。遵守以下规定:
add_filter(\'jpeg_quality\', create_function(\'$quality\', \'return 100;\'));
add_action(\'added_post_meta\', \'ad_update_jpeg_quality\', 10, 4);
function ad_update_jpeg_quality($meta_id, $attach_id, $meta_key, $attach_meta) {
if ($meta_key == \'_wp_attachment_metadata\') {
$post = get_post($attach_id);
if ($post->post_mime_type == \'image/jpeg\' && is_array($attach_meta[\'sizes\'])) {
$pathinfo = pathinfo($attach_meta[\'file\']);
$uploads = wp_upload_dir();
$dir = $uploads[\'basedir\'] . \'/\' . $pathinfo[\'dirname\'];
foreach ($attach_meta[\'sizes\'] as $size => $value) {
$image = $dir . \'/\' . $value[\'file\'];
$resource = imagecreatefromjpeg($image);
if ($size == \'large\') {
// set the jpeg quality for \'large\' size
imagejpeg($resource, $image, 35);
} elseif ($size == \'medium\') {
// set the jpeg quality for the \'medium\' size
imagejpeg($resource, $image, 40);
} elseif ($size == \'small\') {
// set the jpeg quality for the \'small\' size
imagejpeg($resource, $image, 40);
} elseif ($size == \'large-cropped\') {
// set the jpeg quality for the \'large-cropped\' size
imagejpeg($resource, $image, 45);
} else {
// set the jpeg quality for the rest of sizes
imagejpeg($resource, $image, 60);
}
// or you can skip a particular image size
// and set the quality for the rest:
// if ($size == \'splash\') continue;
imagedestroy($resource);
}
}
}
}
如注释所示,本部分设置压缩值:imagejpeg($resource, $image, VALUE)
. 您将需要仔细研究一下,找出哪个值最适合哪个图像大小。我为其余的图像设置了60%的压缩。上述代码仅影响新更新的图像。如果您希望运行一个脚本来更改现有图像,请在插件文件夹中添加一个新文件夹并创建一个新的php文件,其中包含以下代码:
<?php
/*
Plugin Name: Compression JPEG Quality
Plugin URI: http://domain.com
Description: This plugin will change the jpeg image quality according to its size.
Author: Christine
Version: 1.0
Author URI: http://domain.com
*/
register_activation_hook(__FILE__, \'ad_modify_jpeg_quality\');
function ad_modify_jpeg_quality() {
$attachments = get_posts(array(
\'numberposts\' => -1,
\'post_type\' => \'attachment\',
\'post_mime_type\' => \'image/jpeg\'
));
if (empty($attachments)) return;
$uploads = wp_upload_dir();
foreach ($attachments as $attachment) {
$attach_meta = wp_get_attachment_metadata($attachment->ID);
if (!is_array($attach_meta[\'sizes\'])) break;
$pathinfo = pathinfo($attach_meta[\'file\']);
$dir = $uploads[\'basedir\'] . \'/\' . $pathinfo[\'dirname\'];
foreach ($attach_meta[\'sizes\'] as $size => $value) {
$image = $dir . \'/\' . $value[\'file\'];
$resource = imagecreatefromjpeg($image);
if ($size == \'large\') {
// set the jpeg quality for \'large\' size
imagejpeg($resource, $image, 35);
} elseif ($size == \'medium\') {
// set the jpeg quality for the \'medium\' size
imagejpeg($resource, $image, 40);
} elseif ($size == \'small\') {
// set the jpeg quality for the \'small\' size
imagejpeg($resource, $image, 40);
} elseif ($size == \'large-cropped\') {
// set the jpeg quality for the \'large-cropped\' size
imagejpeg($resource, $image, 45);
} else {
// set the jpeg quality for the rest of sizes
imagejpeg($resource, $image, 60);
}
imagedestroy($resource);
}
}
}
?>
最后,添加WP Smush.it 插件将进一步降低文件大小。需要考虑的几个注意事项:
所有这些都不会影响PNG文件的上载。使用此文件格式的唯一选项是添加一个插件,该插件将PNG转换为JPEG,但这意味着透明度将无法工作。另一个绝望的解决方案是将所有PNG文件转换为PNG 8位文件格式,该格式限制为256色。但是,这将使您的上载回到90年代。压缩不会影响Full size
大小选项。这是原始文件上载。如果您运行的是多作者网站,并且希望避免作者添加此文件大小,则可以添加筛选器以从下拉列表中删除此文件大小
您可以使用wp smush it插件http://wordpress.org/extend/plugins/wp-smushit/
它缩小了图像的大小
有没有办法在帖子标题中插入图片?它还需要在<title> 标签、永久链接等。我之所以需要这样做,是因为我有一个客户希望在页面标题中使用他们的徽标,而不是纯文本。因此,图像可能位于标题中的任何位置,因此我不能只是附加/前置图像。我能想到的唯一解决方案是使用javascript进行查找和替换,但这似乎不是一个很好的解决方案。。。