Scale and crop image programmatically in Drupal 7

// Load image file entity.
$file = file_load($fid);

// Load image by uri.
$image = image_load($file->uri);

// Use Drupal's scale and crop function to do the work for you.
image_scale_and_crop($image, $scale['width'], $scale['height']);

// Save image to file, you don't need to save the file entity.
image_save($image);

Caching in Drupal

I’m talking about the core Drupal caching: page cache and block cache which are available in Drupal 7 core. Drupal 6 is similar but works a but differently because page caching was simplified for Drupal 7 (e.g. aggresive mode is now a available as a settings.php var) and the entire system also has become pluggable.

Page caching:

  • If enables, page caching will only be used for anonymous users.
  • If you enable page caching, the entire HTML of each page will be stored in the database. This significantly reduces the amount of queries needed.
  • This automatically means that blocks will also get cached, disregarded their own cache settings.
  • If Varnish is configured, this will also enable varnish caching
  • The page cache max-age will be used (see below) for each page.
  • You can check the “X-Drupal-Cache” HTTP header to check if this cache is HIT.

Block caching:

  • If you enable block caching, without enabling page caching, the block cache settings will be used (for anonymous and authenticated).
  • The defaults for most blocks is “don’t cache” because that is the default for blocks who don’t have their cache settings specifically declared.
  • You can use block_cache_alter module to set this for each block.
  • When it’s block created by Views, you can set the cache settings for a block in Views.

Minimum cache lifetime:

  • This is the amount of time before the page cache is cleared.
  • Page caches are cleared on each cron run (system_cron).
  • Be warned: incorrect settings of your cron and this setting might kill your website performance. See also this issue.

Expiration of cached pages:

  • This only applies for external caching mechanisms, for example your browser cache or Varnish.
  • It sets the Cache-Control max-age value in the HTTP-HEADERS.
  • This setting only takes effect when page caching is turned on.

Adding custom fields to products in Ubercart

Create a custom module

In mymodule.install, use db_add_field() like so:

mymodule_enable() {
  $ret = array();
  db_add_field($ret, 'uc_orders', 'newfield', array(
    'type'=>'varchar',
    'length'=>50)
  );
}

Simple! So now I’ve added a field to the database. Calling uc_order_load() works fine: the new fields are part of the order object since it just reads from the orders table; however, calling uc_order_save() will not update the new field.

There’s one more thing I needed to do. uc_order_save() calls drupal_write_record(), which depends on the schema. So mymodule needs to add its new field to the schema, using hook_schema_alter():

So what you do is write the following in mymodule.module:

mymodule_schema_alter(&$schema) {
  $schema['uc_orders']['fields']['newfield'] = array(
    'type' => 'varchar',
    'length' => 50,
    'description' => 'custom field',
  );
}

Add to Cart Form in other pages in Ubercart for Drupal 6,7

1)Add to Cart button anywhere using the following PHP code in Drupla 6:Enable 

<?php $node1 =  node_load(1)?> <?php print theme(‘uc_product_add_to_cart’, $node1) ?>

2)Add to Cart button anywhere using the following PHP code in Drupla 7:Enable PHP Text filter Module
In Drupla7 there is little bit of change for getting Data. theme_uc_product_add_to_cart() now actually takes the whole form as a variable instead of the node.Now Use this

<?php
$node = node_load(1);
$add_to_cart = array( '#theme' => 'uc_product_add_to_cart',
'#form' => drupal_get_form('uc_product_add_to_cart_form_' . $node->nid, $node), );
print drupal_render($add_to_cart);
 ?>