--- media.php 2008-11-26 18:51:01.000000000 -0500 +++ media.php.new 2008-11-26 18:54:42.000000000 -0500 @@ -194,66 +194,52 @@ } // Scale down an image to fit a particular size and save a new copy of the image -function image_resize( $file, $max_w, $max_h, $crop=false, $suffix=null, $dest_path=null, $jpeg_quality=90) { +function image_resize( $file, $max_w, $max_h, $crop=false, $suffix=null, $dest_path=null, $jpeg_quality=90) +{ + $is = getimagesize($file); + $dims = image_resize_dimensions($is[0],$is[1],$max_w,$max_h,$crop); + if(!$dims) + return $dims; + list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims; + + // $suffix will be appended to the destination filename, just before the extension + if ( !$suffix ) + $suffix = "{$dst_w}x{$dst_h}"; + + $info = pathinfo($file); + $dir = $info['dirname']; + $ext = $info['extension']; + $name = basename($file, ".{$ext}"); + if (!is_null($dest_path) and $_dest_path = realpath($dest_path)) + $dir = $_dest_path; + $destfilename = "{$dir}/{$name}-{$suffix}.{$ext}"; + + switch($is[2]) + { + case IMAGETYPE_PNG: + $quality = "-quality 100"; + break; + case IMAGETYPE_JPEG: + $quality = "-quality {$jpeg_quality}"; + break; + } + + $depth = (!empty($is['bits']) ? "-depth {$is['bits']}" : null); + $cmd = + "convert ". + "-size {$src_w}x{$src_h} ". + "{$file} ". + ($crop ? "-crop {$src_w}x{$src_h}+{$src_x}+{$src_y} " : null). + "-resize {$dst_w}x{$dst_h} ". + "{$quality} {$depth} {$destfilename}"; + system($cmd); + + // Set correct file permissions + $stat = stat(dirname($destfilename)); + $perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits + @ chmod($destfilename, $perms); - $image = wp_load_image( $file ); - if ( !is_resource( $image ) ) - return new WP_Error('error_loading_image', $image); - - list($orig_w, $orig_h, $orig_type) = getimagesize( $file ); - $dims = image_resize_dimensions($orig_w, $orig_h, $max_w, $max_h, $crop); - if (!$dims) - return $dims; - list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims; - - $newimage = imagecreatetruecolor( $dst_w, $dst_h); - - // preserve PNG transparency - if ( IMAGETYPE_PNG == $orig_type && function_exists( 'imagealphablending' ) && function_exists( 'imagesavealpha' ) ) { - imagealphablending( $newimage, false); - imagesavealpha( $newimage, true); - } - - imagecopyresampled( $newimage, $image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); - - // we don't need the original in memory anymore - imagedestroy( $image ); - - // $suffix will be appended to the destination filename, just before the extension - if ( !$suffix ) - $suffix = "{$dst_w}x{$dst_h}"; - - $info = pathinfo($file); - $dir = $info['dirname']; - $ext = $info['extension']; - $name = basename($file, ".{$ext}"); - if ( !is_null($dest_path) and $_dest_path = realpath($dest_path) ) - $dir = $_dest_path; - $destfilename = "{$dir}/{$name}-{$suffix}.{$ext}"; - - if ( $orig_type == IMAGETYPE_GIF ) { - if (!imagegif( $newimage, $destfilename ) ) - return new WP_Error('resize_path_invalid', __( 'Resize path invalid' )); - } - elseif ( $orig_type == IMAGETYPE_PNG ) { - if (!imagepng( $newimage, $destfilename ) ) - return new WP_Error('resize_path_invalid', __( 'Resize path invalid' )); - } - else { - // all other formats are converted to jpg - $destfilename = "{$dir}/{$name}-{$suffix}.jpg"; - if (!imagejpeg( $newimage, $destfilename, apply_filters( 'jpeg_quality', $jpeg_quality ) ) ) - return new WP_Error('resize_path_invalid', __( 'Resize path invalid' )); - } - - imagedestroy( $newimage ); - - // Set correct file permissions - $stat = stat( dirname( $destfilename )); - $perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits - @ chmod( $destfilename, $perms ); - - return $destfilename; + return $destfilename; } // resize an image to make a thumbnail or intermediate size, and return metadata describing the new copy