wp_crop_image

The timeline below displays how wordpress function wp_crop_image has changed across different WordPress versions. If a version is not listed, refer to the next available version below.

WordPress Version: 6.3

/**
 * File contains all the administration image manipulation functions.
 *
 * @package WordPress
 * @subpackage Administration
 */
/**
 * Crops an image to a given size.
 *
 * @since 2.1.0
 *
 * @param string|int   $src      The source file or Attachment ID.
 * @param int          $src_x    The start x position to crop from.
 * @param int          $src_y    The start y position to crop from.
 * @param int          $src_w    The width to crop.
 * @param int          $src_h    The height to crop.
 * @param int          $dst_w    The destination width.
 * @param int          $dst_h    The destination height.
 * @param bool|false   $src_abs  Optional. If the source crop points are absolute.
 * @param string|false $dst_file Optional. The destination file to write to.
 * @return string|WP_Error New filepath on success, WP_Error on failure.
 */
function wp_crop_image($src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false)
{
    $src_file = $src;
    if (is_numeric($src)) {
        // Handle int as attachment ID.
        $src_file = get_attached_file($src);
        if (!file_exists($src_file)) {
            /*
             * If the file doesn't exist, attempt a URL fopen on the src link.
             * This can occur with certain file replication plugins.
             */
            $src = _load_image_to_edit_path($src, 'full');
        } else {
            $src = $src_file;
        }
    }
    $editor = wp_get_image_editor($src);
    if (is_wp_error($editor)) {
        return $editor;
    }
    $src = $editor->crop($src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs);
    if (is_wp_error($src)) {
        return $src;
    }
    if (!$dst_file) {
        $dst_file = str_replace(wp_basename($src_file), 'cropped-' . wp_basename($src_file), $src_file);
    }
    /*
     * The directory containing the original file may no longer exist when
     * using a replication plugin.
     */
    wp_mkdir_p(dirname($dst_file));
    $dst_file = dirname($dst_file) . '/' . wp_unique_filename(dirname($dst_file), wp_basename($dst_file));
    $result = $editor->save($dst_file);
    if (is_wp_error($result)) {
        return $result;
    }
    if (!empty($result['path'])) {
        return $result['path'];
    }
    return $dst_file;
}

WordPress Version: 6.1

/**
 * File contains all the administration image manipulation functions.
 *
 * @package WordPress
 * @subpackage Administration
 */
/**
 * Crops an image to a given size.
 *
 * @since 2.1.0
 *
 * @param string|int   $src      The source file or Attachment ID.
 * @param int          $src_x    The start x position to crop from.
 * @param int          $src_y    The start y position to crop from.
 * @param int          $src_w    The width to crop.
 * @param int          $src_h    The height to crop.
 * @param int          $dst_w    The destination width.
 * @param int          $dst_h    The destination height.
 * @param bool|false   $src_abs  Optional. If the source crop points are absolute.
 * @param string|false $dst_file Optional. The destination file to write to.
 * @return string|WP_Error New filepath on success, WP_Error on failure.
 */
function wp_crop_image($src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false)
{
    $src_file = $src;
    if (is_numeric($src)) {
        // Handle int as attachment ID.
        $src_file = get_attached_file($src);
        if (!file_exists($src_file)) {
            // If the file doesn't exist, attempt a URL fopen on the src link.
            // This can occur with certain file replication plugins.
            $src = _load_image_to_edit_path($src, 'full');
        } else {
            $src = $src_file;
        }
    }
    $editor = wp_get_image_editor($src);
    if (is_wp_error($editor)) {
        return $editor;
    }
    $src = $editor->crop($src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs);
    if (is_wp_error($src)) {
        return $src;
    }
    if (!$dst_file) {
        $dst_file = str_replace(wp_basename($src_file), 'cropped-' . wp_basename($src_file), $src_file);
    }
    /*
     * The directory containing the original file may no longer exist when
     * using a replication plugin.
     */
    wp_mkdir_p(dirname($dst_file));
    $dst_file = dirname($dst_file) . '/' . wp_unique_filename(dirname($dst_file), wp_basename($dst_file));
    $result = $editor->save($dst_file);
    if (is_wp_error($result)) {
        return $result;
    }
    if (!empty($result['path'])) {
        return $result['path'];
    }
    return $dst_file;
}

WordPress Version: 5.7

/**
 * File contains all the administration image manipulation functions.
 *
 * @package WordPress
 * @subpackage Administration
 */
/**
 * Crops an image to a given size.
 *
 * @since 2.1.0
 *
 * @param string|int   $src      The source file or Attachment ID.
 * @param int          $src_x    The start x position to crop from.
 * @param int          $src_y    The start y position to crop from.
 * @param int          $src_w    The width to crop.
 * @param int          $src_h    The height to crop.
 * @param int          $dst_w    The destination width.
 * @param int          $dst_h    The destination height.
 * @param bool|false   $src_abs  Optional. If the source crop points are absolute.
 * @param string|false $dst_file Optional. The destination file to write to.
 * @return string|WP_Error New filepath on success, WP_Error on failure.
 */
function wp_crop_image($src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false)
{
    $src_file = $src;
    if (is_numeric($src)) {
        // Handle int as attachment ID.
        $src_file = get_attached_file($src);
        if (!file_exists($src_file)) {
            // If the file doesn't exist, attempt a URL fopen on the src link.
            // This can occur with certain file replication plugins.
            $src = _load_image_to_edit_path($src, 'full');
        } else {
            $src = $src_file;
        }
    }
    $editor = wp_get_image_editor($src);
    if (is_wp_error($editor)) {
        return $editor;
    }
    $src = $editor->crop($src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs);
    if (is_wp_error($src)) {
        return $src;
    }
    if (!$dst_file) {
        $dst_file = str_replace(wp_basename($src_file), 'cropped-' . wp_basename($src_file), $src_file);
    }
    /*
     * The directory containing the original file may no longer exist when
     * using a replication plugin.
     */
    wp_mkdir_p(dirname($dst_file));
    $dst_file = dirname($dst_file) . '/' . wp_unique_filename(dirname($dst_file), wp_basename($dst_file));
    $result = $editor->save($dst_file);
    if (is_wp_error($result)) {
        return $result;
    }
    return $dst_file;
}

WordPress Version: 5.5

/**
 * File contains all the administration image manipulation functions.
 *
 * @package WordPress
 * @subpackage Administration
 */
/**
 * Crops an image to a given size.
 *
 * @since 2.1.0
 *
 * @param string|int $src      The source file or Attachment ID.
 * @param int        $src_x    The start x position to crop from.
 * @param int        $src_y    The start y position to crop from.
 * @param int        $src_w    The width to crop.
 * @param int        $src_h    The height to crop.
 * @param int        $dst_w    The destination width.
 * @param int        $dst_h    The destination height.
 * @param bool       $src_abs  Optional. If the source crop points are absolute.
 * @param string     $dst_file Optional. The destination file to write to.
 * @return string|WP_Error New filepath on success, WP_Error on failure.
 */
function wp_crop_image($src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false)
{
    $src_file = $src;
    if (is_numeric($src)) {
        // Handle int as attachment ID.
        $src_file = get_attached_file($src);
        if (!file_exists($src_file)) {
            // If the file doesn't exist, attempt a URL fopen on the src link.
            // This can occur with certain file replication plugins.
            $src = _load_image_to_edit_path($src, 'full');
        } else {
            $src = $src_file;
        }
    }
    $editor = wp_get_image_editor($src);
    if (is_wp_error($editor)) {
        return $editor;
    }
    $src = $editor->crop($src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs);
    if (is_wp_error($src)) {
        return $src;
    }
    if (!$dst_file) {
        $dst_file = str_replace(wp_basename($src_file), 'cropped-' . wp_basename($src_file), $src_file);
    }
    /*
     * The directory containing the original file may no longer exist when
     * using a replication plugin.
     */
    wp_mkdir_p(dirname($dst_file));
    $dst_file = dirname($dst_file) . '/' . wp_unique_filename(dirname($dst_file), wp_basename($dst_file));
    $result = $editor->save($dst_file);
    if (is_wp_error($result)) {
        return $result;
    }
    return $dst_file;
}

WordPress Version: 5.4

/**
 * File contains all the administration image manipulation functions.
 *
 * @package WordPress
 * @subpackage Administration
 */
/**
 * Crop an Image to a given size.
 *
 * @since 2.1.0
 *
 * @param string|int $src The source file or Attachment ID.
 * @param int $src_x The start x position to crop from.
 * @param int $src_y The start y position to crop from.
 * @param int $src_w The width to crop.
 * @param int $src_h The height to crop.
 * @param int $dst_w The destination width.
 * @param int $dst_h The destination height.
 * @param int $src_abs Optional. If the source crop points are absolute.
 * @param string $dst_file Optional. The destination file to write to.
 * @return string|WP_Error New filepath on success, WP_Error on failure.
 */
function wp_crop_image($src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false)
{
    $src_file = $src;
    if (is_numeric($src)) {
        // Handle int as attachment ID.
        $src_file = get_attached_file($src);
        if (!file_exists($src_file)) {
            // If the file doesn't exist, attempt a URL fopen on the src link.
            // This can occur with certain file replication plugins.
            $src = _load_image_to_edit_path($src, 'full');
        } else {
            $src = $src_file;
        }
    }
    $editor = wp_get_image_editor($src);
    if (is_wp_error($editor)) {
        return $editor;
    }
    $src = $editor->crop($src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs);
    if (is_wp_error($src)) {
        return $src;
    }
    if (!$dst_file) {
        $dst_file = str_replace(wp_basename($src_file), 'cropped-' . wp_basename($src_file), $src_file);
    }
    /*
     * The directory containing the original file may no longer exist when
     * using a replication plugin.
     */
    wp_mkdir_p(dirname($dst_file));
    $dst_file = dirname($dst_file) . '/' . wp_unique_filename(dirname($dst_file), wp_basename($dst_file));
    $result = $editor->save($dst_file);
    if (is_wp_error($result)) {
        return $result;
    }
    return $dst_file;
}

WordPress Version: 5.2

/**
 * File contains all the administration image manipulation functions.
 *
 * @package WordPress
 * @subpackage Administration
 */
/**
 * Crop an Image to a given size.
 *
 * @since 2.1.0
 *
 * @param string|int $src The source file or Attachment ID.
 * @param int $src_x The start x position to crop from.
 * @param int $src_y The start y position to crop from.
 * @param int $src_w The width to crop.
 * @param int $src_h The height to crop.
 * @param int $dst_w The destination width.
 * @param int $dst_h The destination height.
 * @param int $src_abs Optional. If the source crop points are absolute.
 * @param string $dst_file Optional. The destination file to write to.
 * @return string|WP_Error New filepath on success, WP_Error on failure.
 */
function wp_crop_image($src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false)
{
    $src_file = $src;
    if (is_numeric($src)) {
        // Handle int as attachment ID
        $src_file = get_attached_file($src);
        if (!file_exists($src_file)) {
            // If the file doesn't exist, attempt a URL fopen on the src link.
            // This can occur with certain file replication plugins.
            $src = _load_image_to_edit_path($src, 'full');
        } else {
            $src = $src_file;
        }
    }
    $editor = wp_get_image_editor($src);
    if (is_wp_error($editor)) {
        return $editor;
    }
    $src = $editor->crop($src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs);
    if (is_wp_error($src)) {
        return $src;
    }
    if (!$dst_file) {
        $dst_file = str_replace(wp_basename($src_file), 'cropped-' . wp_basename($src_file), $src_file);
    }
    /*
     * The directory containing the original file may no longer exist when
     * using a replication plugin.
     */
    wp_mkdir_p(dirname($dst_file));
    $dst_file = dirname($dst_file) . '/' . wp_unique_filename(dirname($dst_file), wp_basename($dst_file));
    $result = $editor->save($dst_file);
    if (is_wp_error($result)) {
        return $result;
    }
    return $dst_file;
}

WordPress Version: 4.5

/**
 * File contains all the administration image manipulation functions.
 *
 * @package WordPress
 * @subpackage Administration
 */
/**
 * Crop an Image to a given size.
 *
 * @since 2.1.0
 *
 * @param string|int $src The source file or Attachment ID.
 * @param int $src_x The start x position to crop from.
 * @param int $src_y The start y position to crop from.
 * @param int $src_w The width to crop.
 * @param int $src_h The height to crop.
 * @param int $dst_w The destination width.
 * @param int $dst_h The destination height.
 * @param int $src_abs Optional. If the source crop points are absolute.
 * @param string $dst_file Optional. The destination file to write to.
 * @return string|WP_Error New filepath on success, WP_Error on failure.
 */
function wp_crop_image($src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false)
{
    $src_file = $src;
    if (is_numeric($src)) {
        // Handle int as attachment ID
        $src_file = get_attached_file($src);
        if (!file_exists($src_file)) {
            // If the file doesn't exist, attempt a URL fopen on the src link.
            // This can occur with certain file replication plugins.
            $src = _load_image_to_edit_path($src, 'full');
        } else {
            $src = $src_file;
        }
    }
    $editor = wp_get_image_editor($src);
    if (is_wp_error($editor)) {
        return $editor;
    }
    $src = $editor->crop($src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs);
    if (is_wp_error($src)) {
        return $src;
    }
    if (!$dst_file) {
        $dst_file = str_replace(basename($src_file), 'cropped-' . basename($src_file), $src_file);
    }
    /*
     * The directory containing the original file may no longer exist when
     * using a replication plugin.
     */
    wp_mkdir_p(dirname($dst_file));
    $dst_file = dirname($dst_file) . '/' . wp_unique_filename(dirname($dst_file), basename($dst_file));
    $result = $editor->save($dst_file);
    if (is_wp_error($result)) {
        return $result;
    }
    return $dst_file;
}

WordPress Version: 4.0

/**
 * File contains all the administration image manipulation functions.
 *
 * @package WordPress
 * @subpackage Administration
 */
/**
 * Crop an Image to a given size.
 *
 * @since 2.1.0
 *
 * @param string|int $src The source file or Attachment ID.
 * @param int $src_x The start x position to crop from.
 * @param int $src_y The start y position to crop from.
 * @param int $src_w The width to crop.
 * @param int $src_h The height to crop.
 * @param int $dst_w The destination width.
 * @param int $dst_h The destination height.
 * @param int $src_abs Optional. If the source crop points are absolute.
 * @param string $dst_file Optional. The destination file to write to.
 * @return string|WP_Error New filepath on success, WP_Error on failure.
 */
function wp_crop_image($src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false)
{
    $src_file = $src;
    if (is_numeric($src)) {
        // Handle int as attachment ID
        $src_file = get_attached_file($src);
        if (!file_exists($src_file)) {
            // If the file doesn't exist, attempt a url fopen on the src link.
            // This can occur with certain file replication plugins.
            $src = _load_image_to_edit_path($src, 'full');
        } else {
            $src = $src_file;
        }
    }
    $editor = wp_get_image_editor($src);
    if (is_wp_error($editor)) {
        return $editor;
    }
    $src = $editor->crop($src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs);
    if (is_wp_error($src)) {
        return $src;
    }
    if (!$dst_file) {
        $dst_file = str_replace(basename($src_file), 'cropped-' . basename($src_file), $src_file);
    }
    /*
     * The directory containing the original file may no longer exist when
     * using a replication plugin.
     */
    wp_mkdir_p(dirname($dst_file));
    $dst_file = dirname($dst_file) . '/' . wp_unique_filename(dirname($dst_file), basename($dst_file));
    $result = $editor->save($dst_file);
    if (is_wp_error($result)) {
        return $result;
    }
    return $dst_file;
}

WordPress Version: 3.7

/**
 * File contains all the administration image manipulation functions.
 *
 * @package WordPress
 * @subpackage Administration
 */
/**
 * Crop an Image to a given size.
 *
 * @since 2.1.0
 *
 * @param string|int $src The source file or Attachment ID.
 * @param int $src_x The start x position to crop from.
 * @param int $src_y The start y position to crop from.
 * @param int $src_w The width to crop.
 * @param int $src_h The height to crop.
 * @param int $dst_w The destination width.
 * @param int $dst_h The destination height.
 * @param int $src_abs Optional. If the source crop points are absolute.
 * @param string $dst_file Optional. The destination file to write to.
 * @return string|WP_Error New filepath on success, WP_Error on failure.
 */
function wp_crop_image($src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false)
{
    $src_file = $src;
    if (is_numeric($src)) {
        // Handle int as attachment ID
        $src_file = get_attached_file($src);
        if (!file_exists($src_file)) {
            // If the file doesn't exist, attempt a url fopen on the src link.
            // This can occur with certain file replication plugins.
            $src = _load_image_to_edit_path($src, 'full');
        } else {
            $src = $src_file;
        }
    }
    $editor = wp_get_image_editor($src);
    if (is_wp_error($editor)) {
        return $editor;
    }
    $src = $editor->crop($src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs);
    if (is_wp_error($src)) {
        return $src;
    }
    if (!$dst_file) {
        $dst_file = str_replace(basename($src_file), 'cropped-' . basename($src_file), $src_file);
    }
    // The directory containing the original file may no longer exist when
    // using a replication plugin.
    wp_mkdir_p(dirname($dst_file));
    $dst_file = dirname($dst_file) . '/' . wp_unique_filename(dirname($dst_file), basename($dst_file));
    $result = $editor->save($dst_file);
    if (is_wp_error($result)) {
        return $result;
    }
    return $dst_file;
}