function create_image_object in Image javascript crop 6
Same name and namespace in other branches
- 5 imagecrop.module \create_image_object()
Helper function to create image
Parameters
$fid file id in files table:
$presetname Name of preset beïng cropped:
$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.admin.inc - Callback with javascript crop.
- imagecrop_showcrop in ./
imagecrop.admin.inc - Show the cropped image.
File
- ./
imagecrop.module, line 556 - Provides a javascript toolbox through an imagecache action.
Code
function create_image_object($fid, $presetname, $module = '', $cutoff = FALSE) {
$file = _imagecrop_file_load($fid, $module);
if ($file != FALSE) {
$preset = imagecache_preset_by_name($presetname);
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'];
$initial_xoffset = $preset['actions'][$key]['data']['xoffset'];
$initial_yoffset = $preset['actions'][$key]['data']['yoffset'];
$aspect = $preset['actions'][$key]['data']['aspect'];
$downscaling = $preset['actions'][$key]['data']['downscaling'];
$break = TRUE;
}
if ($break == TRUE) {
unset($preset['actions'][$key]);
}
}
$file->initial_xoffset = $initial_xoffset;
$file->initial_yoffset = $initial_yoffset;
// see if we have stored values already for this file
// $file->xoffset = 0;
// $file->yoffset = 0;
// start with original image size for upcoming actions.
$src = $file->filepath;
$download_method = variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC);
$dst = imagecache_create_path($cutoff || $download_method == FILE_DOWNLOADS_PRIVATE ? $preset['presetname'] : '_imagecrop_temp', $file->filepath);
file_delete($dst);
imagecache_build_derivative($preset['actions'], $src, $dst);
$orig = getimagesize($dst);
$file->orig_width = $orig[0];
$file->orig_height = $orig[1];
if (!empty($initial_xoffset) && is_numeric($file->initial_xoffset)) {
//offset is not empty and is numeric so use #
$file->xoffset = $file->initial_xoffset;
}
else {
if (!is_numeric($file->initial_xoffset)) {
switch ($initial_xoffset) {
case "center":
if (strpos($crop_width, '%')) {
$crop_width_corrected = (int) (str_replace("%", "", $crop_width) / 100 * $file->orig_width);
$file->xoffset = (int) ($file->orig_width / 2 - $crop_width_corrected / 2);
}
else {
$file->xoffset = (int) ($file->orig_width / 2 - $crop_width / 2);
}
break;
case "left":
$file->xoffset = 0;
break;
case "right":
if (strpos($crop_width, '%')) {
$crop_width_corrected = (int) (str_replace("%", "", $crop_width) / 100 * $file->orig_width);
$file->xoffset = (int) $file->orig_width - $crop_width_corrected;
}
else {
$file->xoffset = (int) $file->orig_width - $crop_width;
}
break;
}
}
else {
$file->xoffset = 0;
}
}
if (!empty($initial_yoffset) && is_numeric($file->initial_yoffset)) {
//offset is not empty and is numeric so use #
$file->yoffset = $file->initial_yoffset;
}
else {
if (!is_numeric($file->initial_yoffset)) {
switch ($initial_yoffset) {
case "center":
if (strpos($crop_height, '%')) {
$crop_height_corrected = (int) (str_replace("%", "", $crop_height) / 100 * $file->orig_height);
$file->yoffset = (int) ($file->orig_height / 2 - $crop_height_corrected / 2);
}
else {
$file->yoffset = (int) ($file->orig_height / 2 - $crop_height / 2);
}
break;
case "top":
$file->yoffset = 0;
break;
case "bottom":
if (strpos($crop_height, '%')) {
$crop_height_corrected = (int) (str_replace("%", "", $crop_height) / 100 * $file->orig_height);
$file->yoffset = (int) $file->orig_height - $crop_height_corrected;
}
else {
$file->yoffset = (int) $file->orig_height - $crop_height;
}
break;
}
}
else {
$file->yoffset = 0;
}
}
$file->crop_width = $crop_width;
$file->crop_height = $crop_height;
if ($downscaling) {
$file->min_width = $crop_width;
$file->min_height = $crop_height;
}
else {
$file->min_width = 1;
$file->min_height = 1;
}
$file->preset_width = $crop_width;
$file->preset_height = $crop_height;
$reference = $module == 'node_images' || $module == 'user' ? $module : 'files';
$firstscale = FALSE;
$row = db_fetch_object(db_query("SELECT xoffset, yoffset, width, height, scale\n FROM {imagecrop} ic\n WHERE ic.fid = %d AND ic.presetname = '%s' AND ic.reference = '%s'", $fid, $preset['presetname'], $reference));
// var_dump($row);
if (!empty($row)) {
$file->xoffset = $row->xoffset + $file->initial_xoffset;
$file->yoffset = $row->yoffset + $file->initial_xoffset;
// When not resizable, force the imagecache action settings.
if ($resizable) {
$file->crop_width = $row->width;
$file->crop_height = $row->height;
}
$file->scale = $row->scale;
$firstscale = TRUE;
}
// resizable or not
$file->resizable = $resizable;
/*
// start with original image size for upcoming actions.
$src = $file->filepath;
$download_method = variable_get('file_downloads', FILE_DOWNLOADS_PUBLIC);
$dst = imagecache_create_path(($cutoff || ($download_method == FILE_DOWNLOADS_PRIVATE)) ? $preset['presetname'] : '_imagecrop_temp', $file->filepath);
file_delete($dst);
imagecache_build_derivative($preset['actions'], $src, $dst);
$orig = getimagesize($dst);
$file->orig_width = $orig[0];
$file->orig_height = $orig[1];
*/
// aspect ratio
if ($aspect == 'CROP') {
$aspect = $crop_width / $crop_height;
}
elseif ($aspect == 'KEEP') {
$aspect = $file->orig_width / $file->orig_height;
}
$file->aspect = $aspect;
// 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',
),
);
}
}
$file->presetname = $preset['presetname'];
$file->dst = $dst;
$file->preset_destination = imagecache_create_path($file->presetname, $file->filepath);
// 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_preset'] = $preset;
// if we have a dst, flush the image with all the needed imagecache actions.
if (!empty($dst)) {
file_delete($dst);
imagecache_build_derivative($preset['actions'], $src, $dst);
}
return $file;
}
else {
return FALSE;
}
}