| 1 |
<?php |
<?php |
| 2 |
// $Id: img_assist.module,v 1.75.2.30 2009/07/04 11:58:04 sun Exp $ |
// $Id: img_assist.module,v 1.75.2.31 2009/07/15 22:58:07 sun Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 493 |
|
|
| 494 |
case 'description': |
case 'description': |
| 495 |
return t('Add images to your posts with Image assist.'); |
return t('Add images to your posts with Image assist.'); |
| 496 |
|
|
| 497 |
|
// case 'no cache': |
| 498 |
|
// return TRUE; |
| 499 |
|
|
| 500 |
case 'process': |
case 'process': |
| 501 |
$processed = FALSE; |
$processed = FALSE; |
| 502 |
foreach (img_assist_get_macros($text) as $unexpanded_macro => $macro) { |
foreach (img_assist_get_macros($text) as $unexpanded_macro => $macro) { |
| 1392 |
|
|
| 1393 |
if ($attributes['nid']) { |
if ($attributes['nid']) { |
| 1394 |
$node = node_load($attributes['nid']); |
$node = node_load($attributes['nid']); |
| 1395 |
|
|
| 1396 |
// Get size. |
// Get image size. |
| 1397 |
$width = $attributes['width']; |
$width = (int) $attributes['width']; |
| 1398 |
$height = $attributes['height']; |
$height = (int) $attributes['height']; |
| 1399 |
|
$default_sizes = image_get_sizes(); |
| 1400 |
if ($width || $height) { |
if ($width || $height) { |
| 1401 |
// Check to ensure that the dimensions don't exceed the max set in the |
// Check to ensure that the dimensions don't exceed the max set in the |
| 1402 |
// img_assist settings. |
// img_assist settings. |
| 1423 |
|
|
| 1424 |
// Get new width and height for this inline image. |
// Get new width and height for this inline image. |
| 1425 |
// If height is either left out or larger than width then |
// If height is either left out or larger than width then |
| 1426 |
// width is the controlling factor. |
// Width is controlling factor. |
| 1427 |
if (!$height || round($width / $aspect_ratio) <= $height) { |
if (!$height || ($width && round($width / $aspect_ratio) <= $height)) { |
| 1428 |
$height = round($width / $aspect_ratio); |
$height = round($width / $aspect_ratio); |
| 1429 |
} |
} |
| 1430 |
// Else, width is either left out or larger than height so |
// Else, width is either left out or larger than height so |
| 1431 |
// height is controlling factor. |
// Height is controlling factor. |
| 1432 |
else { |
else { |
| 1433 |
$width = round($height * $aspect_ratio); |
$width = round($height * $aspect_ratio); |
| 1434 |
} |
} |
| 1435 |
|
|
| 1436 |
// Compare new width and height to existing image derivative sizes. |
// Find out whether the given width/height is the same as (or extremely |
| 1437 |
|
// close to) a default image derivative size; if so, we will use the |
| 1438 |
|
// default size instead of generating a custom image. |
| 1439 |
$diag_size_new = sqrt(pow($width, 2) + pow($height, 2)); |
$diag_size_new = sqrt(pow($width, 2) + pow($height, 2)); |
| 1440 |
$closest_difference = 9999; |
$closest_difference = 9999; |
| 1441 |
|
|
| 1442 |
foreach (image_get_sizes() as $key => $stdsize) { |
foreach ($default_sizes as $key => $stdsize) { |
| 1443 |
$width_std = $stdsize['width']; |
$width_std = $stdsize['width']; |
| 1444 |
$height_std = $stdsize['height']; |
$height_std = $stdsize['height']; |
| 1445 |
// Get default width and height, taking the aspect ratio into account. |
// Diagonal size calculations require a width or height. |
| 1446 |
if (round($width_std / $aspect_ratio) <= $height_std) { |
if (!$height_std && !$width_std) { |
| 1447 |
// Width is controlling factor. |
// For the original image, we can fall back to actual dimensions |
| 1448 |
|
// though. In fact, IMAGE_ORIGINAL can either have maximum |
| 1449 |
|
// dimensions (so aspect ratio based calculations below will apply) |
| 1450 |
|
// or the real dimensions (which must be considered as valid |
| 1451 |
|
// "derivative size"). |
| 1452 |
|
// Note that this annuls the 'use original size' user permission. |
| 1453 |
|
if ($key !== IMAGE_ORIGINAL) { |
| 1454 |
|
continue; |
| 1455 |
|
} |
| 1456 |
|
else { |
| 1457 |
|
$width_std = $original_size['width']; |
| 1458 |
|
$height_std = $original_size['height']; |
| 1459 |
|
} |
| 1460 |
|
} |
| 1461 |
|
// Calculate default width and height based on aspect ratio. |
| 1462 |
|
// Width is controlling factor. |
| 1463 |
|
if (!$height_std && ($width_std && round($width_std / $aspect_ratio) <= $height_std)) { |
| 1464 |
$height_std = round($width_std / $aspect_ratio); |
$height_std = round($width_std / $aspect_ratio); |
| 1465 |
} |
} |
| 1466 |
|
// Height is controlling factor. |
| 1467 |
else { |
else { |
|
// Height is controlling factor. |
|
| 1468 |
$width_std = round($height_std * $aspect_ratio); |
$width_std = round($height_std * $aspect_ratio); |
| 1469 |
} |
} |
| 1470 |
// Get the diagonal size of this standard image. |
// Calculate diagonal size of this default size. |
| 1471 |
$diag_size_std = sqrt(pow($width_std, 2) + pow($height_std, 2)); |
$diag_size_std = sqrt(pow($width_std, 2) + pow($height_std, 2)); |
| 1472 |
$difference = abs($diag_size_new - $diag_size_std); |
$difference = abs($diag_size_new - $diag_size_std); |
| 1473 |
if ($difference < $closest_difference) { |
if ($difference < $closest_difference) { |
| 1475 |
$closest_difference = $difference; |
$closest_difference = $difference; |
| 1476 |
} |
} |
| 1477 |
} |
} |
| 1478 |
|
// If, for any reason, no default image derivative size has a width or |
| 1479 |
// Find out if desired width/height is the same (or extremely close) |
// height, fall back to IMAGE_THUMBNAIL. |
| 1480 |
// to the size of a default image derivative; if so, we will use the |
if (!isset($closest_std_size)) { |
| 1481 |
// default size image instead of generating our own image. |
$closest_std_size = IMAGE_THUMBNAIL; |
| 1482 |
|
} |
| 1483 |
|
|
| 1484 |
if ($closest_difference < 3) { |
if ($closest_difference < 3) { |
| 1485 |
$create_custom = FALSE; |
$create_custom = FALSE; |
| 1486 |
} |
} |
| 1526 |
$size['key'] = $closest_std_size; |
$size['key'] = $closest_std_size; |
| 1527 |
} |
} |
| 1528 |
} |
} |
| 1529 |
// Default to thumbnail if width and/or height is missing. |
// Default to thumbnail if width and height is missing. |
| 1530 |
else { |
else { |
| 1531 |
$size = image_get_sizes(); |
$size = $default_sizes[IMAGE_THUMBNAIL]; |
|
$size = $size[IMAGE_THUMBNAIL]; |
|
| 1532 |
$size['key'] = IMAGE_THUMBNAIL; |
$size['key'] = IMAGE_THUMBNAIL; |
| 1533 |
} |
} |
| 1534 |
return theme('img_assist_inline', $node, $size, $attributes); |
return theme('img_assist_inline', $node, $size, $attributes); |