You are here

function create_image_object in Image javascript crop 5

Same name and namespace in other branches
  1. 6 imagecrop.module \create_image_object()

Helper function to create image

Parameters

$fid file id in files table:

$presetid id of the preset:

$cutoff delete the javascript crop action when user wants to define the offsets:

Return value

$file with file, javascript crop and preset properties

2 calls to create_image_object()
imagecrop_docrop in ./imagecrop.module
Callback with javascript crop.
imagecrop_showcrop in ./imagecrop.module
Show the cropped image.

File

./imagecrop.module, line 457
Provides a javascript toolbox through an imagecache action.

Code

function create_image_object($fid, $presetid, $module = '', $cutoff = FALSE) {
  $file = _imagecrop_file_load($fid, $module);
  if ($file != FALSE) {
    $preset = imagecache_preset($presetid);
    imagecache_image_flush($file->filename);
    if ($cutoff == FALSE) {

      // get the actions from the preset and and throw out the javascript_crop action
      // and every other action which comes after it.
      $break = FALSE;
      while (list($key, $val) = each($preset['actions'])) {
        if ($val['action'] == 'imagecrop_javascript') {
          $crop_width = $preset['actions'][$key]['data']['width'];
          $crop_height = $preset['actions'][$key]['data']['height'];
          $resizable = $preset['actions'][$key]['data']['resizable'];
          $break = TRUE;
        }
        if ($break == TRUE) {
          unset($preset['actions'][$key]);
        }
      }

      // see if we have stored values allready for this file
      $file->xoffset = 0;
      $file->yoffset = 0;
      $file->crop_width = $crop_width;
      $file->crop_height = $crop_height;
      $reference = !empty($module) ? $module : 'files';
      $row = db_fetch_object(db_query("SELECT xoffset,yoffset,width,height,scale FROM {imagecrop} ic where ic.fid = %d AND ic.presetid = %d AND ic.reference = '%s'", $fid, $presetid, $reference));
      $firstscale = FALSE;
      if (!empty($row)) {
        $file->xoffset = $row->xoffset;
        $file->yoffset = $row->yoffset;
        $file->crop_width = $row->width;
        $file->crop_height = $row->height;
        $file->scale = $row->scale;
        $firstscale = TRUE;
      }

      // resizable or not
      $file->resizable = $resizable;

      // add scale action if necessary
      if ($row->scale != 'original' && $firstscale == TRUE) {
        $preset['actions'][] = array(
          'action' => 'imagecache_scale',
          'data' => array(
            'width' => $row->scale,
            'height' => '',
            'upscale' => 'false',
          ),
        );
      }
    }
    $src = $file->filepath;
    $file->presetname = $preset['presetname'];
    $dst = imagecache_create_path($preset['presetname'], $file->filepath);
    $file->dst = $dst;

    // original size
    $orig = getimagesize($file->filepath);
    $file->orig_width = $orig[0];

    // create the file to display for the crop,
    // we also set a global presetid variable, so I can use this in
    // the javascript_crop action
    $GLOBALS['imagecrop_presetid'] = $presetid;
    imagecache_build_derivative($preset['actions'], $src, $dst);
    return $file;
  }
  else {
    return FALSE;
  }
}