Resize images on upload in Wordpress

Tags: ,

If you need to resize all images on upload in Wordpress to a set size, as a prevention to large images breaking the design, use this hack. I’ve collated and adapted some code, from an original post on the Wordpress Mu forums, to work on Wordpress 2.2.


Open file: /wp-admin/admin-functions.php:

Find function: (near line 984)
function has_meta($postid) ....
Add the following code right BEFORE the above function:
function wp_create_smallerimage($file, $max_side, $effect = '') {
if (file_exists($file)) {
$type = getimagesize($file);
if ((!function_exists('imagegif') && $type[2] == 1) || (!function_exists(’imagejpeg’) && $type[2] == 2) || (!function_exists(’imagepng’) && $type[2] == 3)) {
$error = __(’Filetype not supported. Thumbnail not created.’);
} else {
if ($type[2] == 1) {
$image = imagecreatefromgif($file);
} elseif ($type[2] == 2) {
$image = imagecreatefromjpeg($file);
} elseif ($type[2] == 3) {
$image = imagecreatefrompng($file);
}
if (function_exists(’imageantialias’))
imageantialias($image, TRUE);
$image_attr = getimagesize($file);
if ($image_attr[0] > $image_attr[1]) {
$image_width = $image_attr[0];
$image_height = $image_attr[1];
$image_new_width = $max_side;
$image_ratio = $image_width / $image_new_width;
$image_new_height = $image_height / $image_ratio;
} else {
$image_width = $image_attr[0];
$image_height = $image_attr[1];
$image_new_height = $max_side;
$image_ratio = $image_height / $image_new_height;
$image_new_width = $image_width / $image_ratio;
$thumbnail = imagecreatetruecolor($image_new_width, $image_new_height);
@ imagecopyresampled($thumbnail, $image, 0, 0, 0, 0, $image_new_width, $image_new_height, $image_attr[0], $image_attr[1]);
if ( basename($file) == $thumb = apply_filters(’thumbnail_filename’, basename($file)) )
$thumb = preg_replace(’!(\.[^.]+)?$!’, __(”).’$1′, basename($file), 1);
$thumbpath = str_replace(basename($file), $thumb, $file);
if ($type[2] == 1) {
if (!imagegif($thumbnail, $thumbpath)) {
$error = __(”Thumbnail path invalid”);
}} elseif ($type[2] == 2) {
if (!imagejpeg($thumbnail, $thumbpath, 80)) {
$error = __(”Thumbnail path invalid”);
}} elseif ($type[2] == 3) {
if (!imagepng($thumbnail, $thumbpath)) {
$error = __(”Thumbnail path invalid”);
}}}} else {
$error = __(’File not found’);
}
if (!empty ($error)) {
return $error;
} else {
return $thumbpath;
}}

To change the quality of jpg, change the numeric value in this line (from the above code) 100 = highest, 0 = lowest:
if (!imagejpeg($thumbnail, $thumbpath, 80))

Then open this file: /wp-admin/upload-functions.php

Find function: wp_upload_tab_upload_action function

Find this line: wp_update_attachment_metadata( $id, wp_generate_attachment_metadata( $id, $file ) );

After this line add the following code:
(change $max to your desired maximum width / height)
$max = 485;
$imagesize = getimagesize($file);
$imagedata = array();
$imagedata['width'] = $imagesize['0'];
$imagedata['height'] = $imagesize['1'];
if ( $imagedata['width'] > $max ) {
if ($imagedata['height'] > $imagedata['width']) {
$aspect = $imagedata['width']/$imagedata['height'];
$max = $max / $aspect;
}
$thumb = wp_create_smallerimage($file,$max);
if ( @file_exists($thumb) ) {
$newdata = $imagedata;
$newdata['thumb'] = basename($thumb);
update_post_meta($id, ‘_wp_attachment_metadata’, $newdata, $imagedata);
} else {
$error = $thumb;
}}

3 comments for this post

  1. thijs Says:

    Hi, great! thanks for sharing.
    I has some problem with the part where you set the $thumb variable. The two underscores and empty strihg in __(”) might be the cause, it don;t know. Asolution I found is

    replace
    $thumb = preg_replace(’!(\.[^.]+)?$!’, __(”).’$1′, basename($file), 1);

    with
    $thumb = basename( $file );

  2. oneaustin Says:

    Have you tried this on 2.6 yet?

  3. Sean Johnson Says:

    I haven’t tried it since 2.5 when they introduced a better image upload - there’s not much need for it now.

Leave a comment