image_resize [ WordPress Function ]
| Parameters: |
|
| Returns: |
|
| Defined at: |
|
Scale down an image to fit a particular size and save a new copy of the image.
The PNG transparency will be preserved using the function, as well as the image type. If the file going in is PNG, then the resized image is going to be PNG. The only supported image types are PNG, GIF, and JPEG.
Some functionality requires API to exist, so some PHP version may lose out support. This is not the fault of WordPress (where functionality is downgraded, not actual defects), but of your PHP version.
Source
<?php
function image_resize( $file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 90 ) {
$image = wp_load_image( $file );
if ( !is_resource( $image ) )
return new WP_Error( 'error_loading_image', $image, $file );
$size = @getimagesize( $file );
if ( !$size )
return new WP_Error('invalid_image', __('Could not read image size'), $file);
list($orig_w, $orig_h, $orig_type) = $size;
$dims = image_resize_dimensions($orig_w, $orig_h, $max_w, $max_h, $crop);
if ( !$dims )
return new WP_Error( 'error_getting_dimensions', __('Could not calculate resized image dimensions') );
list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims;
$newimage = wp_imagecreatetruecolor( $dst_w, $dst_h );
imagecopyresampled( $newimage, $image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
// convert from full colors to index colors, like original PNG.
if ( IMAGETYPE_PNG == $orig_type && function_exists('imageistruecolor') && !imageistruecolor( $image ) )
imagetruecolortopalette( $newimage, false, imagecolorstotal( $image ) );
// 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 = wp_basename($file, ".$ext");
if ( !is_null($dest_path) and $_dest_path = realpath($dest_path) )
$dir = $_dest_path;
$destfilename = "{$dir}/{$name}-{$suffix}.{$ext}";
if ( IMAGETYPE_GIF == $orig_type ) {
if ( !imagegif( $newimage, $destfilename ) )
return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
} elseif ( IMAGETYPE_PNG == $orig_type ) {
if ( !imagepng( $newimage, $destfilename ) )
return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
} else {
// all other formats are converted to jpg
if ( 'jpg' != $ext && 'jpeg' != $ext )
$destfilename = "{$dir}/{$name}-{$suffix}.jpg";
if ( !imagejpeg( $newimage, $destfilename, apply_filters( 'jpeg_quality', $jpeg_quality, 'image_resize' ) ) )
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;
}
?>
Examples [ wp-snippets.com ]
Google Arama Sonuçlarý
- image_resize | image.inc | Drupal 6 | Drupal API
5 image.inc · image_resize($source, $destination, $width, $height). 6 image.inc, image_resize( $source , $destination , $width, $height). 7 image.inc ...
api.drupal.org - image_resize - WordPress Codex
Description. Scale down an image to fit a particular size and save a new copy of the image. The PNG transparency will be preserved using the function, as well ...
codex.wordpress.org - WordPress › Support » Image_resize and add_filter
I'm trying to change in functions.php how to work image_resize function. I found this post (http://jstnryan.com/custom-image-suffixes-in-wordpress/) where the ...
wordpress.org - image_resize (WordPress Function) - WPSeek.com
WordPress lookup for image_resize, a WordPress Function. wpseek.com is a WordPress-centric search tool for developers and theme authors.
wpseek.com