/**
* crop an image while preserving aspect ratio
*
* @param int $nw the new width of the image
* @param int $nh the new height of the image
* @param string $source the path and file of the image to be cropped
* @param string $dest the path and file of the cropped image
* @return boolean true on success
*
*/
function crop_image($nw, $nh, $source, $dest, $sharpen = true)
{
    if (!is_file($source) || !is_readable($source)) {
        trigger_error('cannot resize image; no file exists at the given path ' . '(' . var_export($source, true) . ')', WARNING);
        return false;
    }
    $perms = substr(sprintf('%o', fileperms($source)), -4);
    if (imagemagick_available()) {
        $result = _imagemagick_crop_image($nw, $nh, $source, $dest, $sharpen);
    } else {
        if (function_exists('imagecreatetruecolor')) {
            $result = _gd_crop_image($nw, $nh, $source, $dest, $sharpen);
        } else {
            trigger_error('neither ImageMagick nor GD are available; cannot ' . 'crop image', WARNING);
            return false;
        }
    }
    // Prevent the transformation from changing the file permissions.
    clearstatcache();
    $newperms = substr(sprintf('%o', fileperms($dest)), -4);
    if ($perms != $newperms) {
        @chmod($dest, octdec($perms));
    }
    return $result;
}
/**
* A test function for testing _imagemagick_crop_image.  It allows
* you to test several cases quickly.
*/
function im_resize_images()
{
    global $d;
    $img_ids = explode(",", $d['image_id']);
    $width = (int) $d['w'];
    $height = (int) $d['h'];
    $crop_style = $d['crop_style'];
    $target = array();
    $source = array();
    $url = array();
    foreach ($img_ids as $id) {
        $rsi = new reasonSizedImage();
        $rsi->set_id($id);
        $rsi->set_width($width);
        $rsi->set_height($height);
        $rsi->set_crop_style($crop_style);
        $target[] = $rsi->_get_path();
        $entity = new entity($id);
        $source[] = reason_get_image_path($entity, 'standard');
        $url[] = $rsi->_get_url();
    }
    //	print_r($target);
    //	pray($source);
    //	pray($url);
    for ($i = 0; $i < count($source); $i++) {
        _imagemagick_crop_image($width, $height, $source[$i], $target[$i], false);
    }
    $str = "";
    for ($i = 0; $i < count($url); $i++) {
        $str .= "" . $url[$i] . ",";
    }
    $str = trim($str, ",");
    echo $str;
    //		_imagemagick_crop_image($nw,$nh,$source,$target,false);
    //	resize_images();
}