Resize an Image on the Fly with PHP

Followings Are Different Function To resize Image.

Examples

1)  Crop Image

resize_image(‘crop’,’test.jpg’,’test_crop.jpg’,100,100);

2) Resize To Max

resize_image(‘max’,‘test.jpg’,’test_crop.jpg’,100,100);

3) Force Resize

resize_image(‘force’,‘test.jpg’,’test_crop.jpg’,100,100);

resize_image_crop($image,$width,$height)

This function generates a new image in the dimensions you specify, maintaining the aspect ratio by cropping when necessary.


resize_image_max($image,$max_width,$max_height)

This function checks to see if your image exceeds either the max width or the max height specified and, if so, resizes the image while maintaining the aspect ratio.


resize_image_force($image,$width,$height)

This function generates a new image in the dimensions you specify without cropping, so the aspect ratio may be altered.


resize_image($method,$image_loc,$new_loc,$width,$height)

Those functions all start with an image that has already been opened by the GD Image functions. This next function goes farther and opens up an image file, resizes it according to the method you choose, and then saves it for you. It also displays any errors that are generated in an easy-to-understand way.

<?php

function resize_image($method,$image_loc,$new_loc,$width,$height) {
    if (!is_array(@$GLOBALS['errors'])) { $GLOBALS['errors'] = array(); }
    
    if (!in_array($method,array('force','max','crop'))) { $GLOBALS['errors'][] = 'Invalid method selected.'; }
    
    if (!$image_loc) { $GLOBALS['errors'][] = 'No source image location specified.'; }
    else {
        if ((substr(strtolower($image_loc),0,7) == 'http://') || (substr(strtolower($image_loc),0,7) == 'https://')) { /*don't check to see if file exists since it's not local*/ }
        elseif (!file_exists($image_loc)) { $GLOBALS['errors'][] = 'Image source file does not exist.'; }
        $extension = strtolower(substr($image_loc,strrpos($image_loc,'.')));
        if (!in_array($extension,array('.jpg','.jpeg','.png','.gif','.bmp'))) { $GLOBALS['errors'][] = 'Invalid source file extension!'; }
    }
    
    if (!$new_loc) { $GLOBALS['errors'][] = 'No destination image location specified.'; }
    else {
        $new_extension = strtolower(substr($new_loc,strrpos($new_loc,'.')));
        if (!in_array($new_extension,array('.jpg','.jpeg','.png','.gif','.bmp'))) { $GLOBALS['errors'][] = 'Invalid destination file extension!'; }
    }

    $width = abs(intval($width));
    if (!$width) { $GLOBALS['errors'][] = 'No width specified!'; }
    
    $height = abs(intval($height));
    if (!$height) { $GLOBALS['errors'][] = 'No height specified!'; }
    
    if (count($GLOBALS['errors']) > 0) { echo_errors(); return false; }
    
    if (in_array($extension,array('.jpg','.jpeg'))) { $image = @imagecreatefromjpeg($image_loc); }
    elseif ($extension == '.png') { $image = @imagecreatefrompng($image_loc); }
    elseif ($extension == '.gif') { $image = @imagecreatefromgif($image_loc); }
    elseif ($extension == '.bmp') { $image = @imagecreatefromwbmp($image_loc); }
    
    if (!$image) { $GLOBALS['errors'][] = 'Image could not be generated!'; }
    else {
        $current_width = imagesx($image);
        $current_height = imagesy($image);
        if ((!$current_width) || (!$current_height)) { $GLOBALS['errors'][] = 'Generated image has invalid dimensions!'; }
    }
    if (count($GLOBALS['errors']) > 0) { @imagedestroy($image); echo_errors(); return false; }

    if ($method == 'force') { $new_image = resize_image_force($image,$width,$height); }
    elseif ($method == 'max') { $new_image = resize_image_max($image,$width,$height); }
    elseif ($method == 'crop') { $new_image = resize_image_crop($image,$width,$height); }
    
    if ((!$new_image) && (count($GLOBALS['errors'] == 0))) { $GLOBALS['errors'][] = 'New image could not be generated!'; }
    if (count($GLOBALS['errors']) > 0) { @imagedestroy($image); echo_errors(); return false; }
    
    $save_error = false;
    if (in_array($extension,array('.jpg','.jpeg'))) { imagejpeg($new_image,$new_loc) or ($save_error = true); }
    elseif ($extension == '.png') { imagepng($new_image,$new_loc) or ($save_error = true); }
    elseif ($extension == '.gif') { imagegif($new_image,$new_loc) or ($save_error = true); }
    elseif ($extension == '.bmp') { imagewbmp($new_image,$new_loc) or ($save_error = true); }
    if ($save_error) { $GLOBALS['errors'][] = 'New image could not be saved!'; }
    if (count($GLOBALS['errors']) > 0) { @imagedestroy($image); @imagedestroy($new_image); echo_errors(); return false; }

    imagedestroy($image);
    imagedestroy($new_image);
    
    return true;
}

function echo_errors() {
    if (!is_array(@$GLOBALS['errors'])) { $GLOBALS['errors'] = array('Unknown error!'); }
    foreach ($GLOBALS['errors'] as $error) { echo '<p style="color:red;font-weight:bold;">Error: '.$error.'</p>'; }
}



?>

 

How to Customized User Setting acording to user role in wordpress

/*remove unwanted menu for user Start*/

add_action( ‘admin_init’, ‘my_remove_menu_pages’);

function my_remove_menu_pages() {

global $current_user;

if ($current_user->roles[0] == ‘contributor’) {

$tourpackage = get_user_meta($current_user->ID,’tourpackage’,true);
$carhire = get_user_meta($current_user->ID,’carhires’,true);

remove_menu_page(‘tools.php’);
remove_menu_page(‘edit-comments.php’);
remove_menu_page(‘edit.php’);

if($tourpackage!=’tour’){

remove_menu_page(‘edit.php?post_type=tourpackage’);

}

if($carhire!=’car’){

remove_menu_page(‘edit.php?post_type=car-hire’);

}

remove_meta_box(‘dashboard_right_now’, ‘dashboard’, ‘normal’); //Removes the ‘At a Glance’ widget
remove_meta_box(‘dashboard_activity’, ‘dashboard’, ‘normal’); //Removes the ‘Activity’ widget (since 3.8)

}

}

/*remove unwanted menu item from admin bar for user Start*/

function remove_admin_bar_links() {
global $wp_admin_bar, $current_user;

if ($current_user->roles[0] == ‘contributor’) {

$wp_admin_bar->remove_menu(‘updates’);          // Remove the updates link
$wp_admin_bar->remove_menu(‘comments’);         // Remove the comments link
$wp_admin_bar->remove_menu(‘new-content’);      // Remove the content link
$wp_admin_bar->remove_menu(‘w3tc’);             // If you use w3 total cache remove the performance link

}
}
add_action( ‘wp_before_admin_bar_render’, ‘remove_admin_bar_links’ );

/*remove unwanted menu item from admin bar for user End*/

/*Add Extra fieled for create and edit user and save to database Start*/

add_action( ‘edit_user_profile’, ‘my_show_extra_profile_fields’ );
add_action( ‘user_new_form’, ‘my_show_extra_profile_fields’ );

function my_show_extra_profile_fields( $user ) { ?>

<h3>Allow Acess For Post</h3>

<table class=”form-table”>

<tr>
<th><label for=”tourpackage”>Tour Packages</label></th>

<td>
<input type=”checkbox” name=”tourpackage” id=”tourpackage” <?php if(get_usermeta($user->ID ,’tourpackage’) == ‘tour’){ echo ‘checked’;} ?> value=”tour”/>
</td>
</tr>

<tr>
<th><label for=”carhires”>Car Hires</label></th>

<td>
<input type=”checkbox” name=”carhires” id=”carhires” <?php if(get_usermeta($user->ID ,’carhires’) == ‘car’){ echo ‘checked’;} ?> value=”car”/>
</td>
</tr>

</table>
<?php }

add_action( ‘user_register’, ‘my_save_extra_profile_fields’ );
add_action( ‘edit_user_profile_update’, ‘my_save_extra_profile_fields’ );

function my_save_extra_profile_fields( $user_id ) {

if ( !current_user_can( ‘edit_user’, $user_id ) )
return false;

update_usermeta( $user_id, ‘tourpackage’, $_POST[‘tourpackage’]);
update_usermeta( $user_id, ‘carhires’, $_POST[‘carhires’] );
}

/*Add Extra fieled for create and edit user and save to database End*/