| 1 |
<?php |
<?php |
| 2 |
// $Id: img_assist.module,v 1.68.2.50.2.22 2009/07/04 11:58:12 sun Exp $ |
// $Id: img_assist.module,v 1.68.2.50.2.23 2009/07/15 22:58:20 sun Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file |
* @file |
| 465 |
|
|
| 466 |
case 'description': |
case 'description': |
| 467 |
return t('Add images to your posts with Image assist.'); |
return t('Add images to your posts with Image assist.'); |
| 468 |
|
|
| 469 |
|
// case 'no cache': |
| 470 |
|
// return TRUE; |
| 471 |
|
|
| 472 |
case 'process': |
case 'process': |
| 473 |
$processed = FALSE; |
$processed = FALSE; |
| 474 |
foreach (img_assist_get_macros($text) as $unexpanded_macro => $macro) { |
foreach (img_assist_get_macros($text) as $unexpanded_macro => $macro) { |
| 1359 |
|
|
| 1360 |
if ($attributes['nid']) { |
if ($attributes['nid']) { |
| 1361 |
$node = node_load($attributes['nid']); |
$node = node_load($attributes['nid']); |
| 1362 |
|
|
| 1363 |
// Get size. |
// Get image size. |
| 1364 |
$width = $attributes['width']; |
$width = (int) $attributes['width']; |
| 1365 |
$height = $attributes['height']; |
$height = (int) $attributes['height']; |
| 1366 |
|
$default_sizes = image_get_sizes(); |
| 1367 |
if ($width || $height) { |
if ($width || $height) { |
| 1368 |
// 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 |
| 1369 |
// img_assist settings. |
// img_assist settings. |
| 1390 |
|
|
| 1391 |
// Get new width and height for this inline image. |
// Get new width and height for this inline image. |
| 1392 |
// If height is either left out or larger than width then |
// If height is either left out or larger than width then |
| 1393 |
// width is the controlling factor. |
// Width is controlling factor. |
| 1394 |
if (!$height || round($width / $aspect_ratio) <= $height) { |
if (!$height || ($width && round($width / $aspect_ratio) <= $height)) { |
| 1395 |
$height = round($width / $aspect_ratio); |
$height = round($width / $aspect_ratio); |
| 1396 |
} |
} |
| 1397 |
// Else, width is either left out or larger than height so |
// Else, width is either left out or larger than height so |
| 1398 |
// height is controlling factor. |
// Height is controlling factor. |
| 1399 |
else { |
else { |
| 1400 |
$width = round($height * $aspect_ratio); |
$width = round($height * $aspect_ratio); |
| 1401 |
} |
} |
| 1402 |
|
|
| 1403 |
// Compare new width and height to existing image derivative sizes. |
// Find out whether the given width/height is the same as (or extremely |
| 1404 |
|
// close to) a default image derivative size; if so, we will use the |
| 1405 |
|
// default size instead of generating a custom image. |
| 1406 |
$diag_size_new = sqrt(pow($width, 2) + pow($height, 2)); |
$diag_size_new = sqrt(pow($width, 2) + pow($height, 2)); |
| 1407 |
$closest_difference = 9999; |
$closest_difference = 9999; |
| 1408 |
|
|
| 1409 |
foreach (image_get_sizes() as $key => $stdsize) { |
foreach ($default_sizes as $key => $stdsize) { |
| 1410 |
$width_std = $stdsize['width']; |
$width_std = $stdsize['width']; |
| 1411 |
$height_std = $stdsize['height']; |
$height_std = $stdsize['height']; |
| 1412 |
// Get default width and height, taking the aspect ratio into account. |
// Diagonal size calculations require a width or height. |
| 1413 |
if (round($width_std / $aspect_ratio) <= $height_std) { |
if (!$height_std && !$width_std) { |
| 1414 |
// Width is controlling factor. |
// For the original image, we can fall back to actual dimensions |
| 1415 |
|
// though. In fact, IMAGE_ORIGINAL can either have maximum |
| 1416 |
|
// dimensions (so aspect ratio based calculations below will apply) |
| 1417 |
|
// or the real dimensions (which must be considered as valid |
| 1418 |
|
// "derivative size"). |
| 1419 |
|
// Note that this annuls the 'use original size' user permission. |
| 1420 |
|
if ($key !== IMAGE_ORIGINAL) { |
| 1421 |
|
continue; |
| 1422 |
|
} |
| 1423 |
|
else { |
| 1424 |
|
$width_std = $original_size['width']; |
| 1425 |
|
$height_std = $original_size['height']; |
| 1426 |
|
} |
| 1427 |
|
} |
| 1428 |
|
// Calculate default width and height based on aspect ratio. |
| 1429 |
|
// Width is controlling factor. |
| 1430 |
|
if (!$height_std && ($width_std && round($width_std / $aspect_ratio) <= $height_std)) { |
| 1431 |
$height_std = round($width_std / $aspect_ratio); |
$height_std = round($width_std / $aspect_ratio); |
| 1432 |
} |
} |
| 1433 |
|
// Height is controlling factor. |
| 1434 |
else { |
else { |
|
// Height is controlling factor. |
|
| 1435 |
$width_std = round($height_std * $aspect_ratio); |
$width_std = round($height_std * $aspect_ratio); |
| 1436 |
} |
} |
| 1437 |
// Get the diagonal size of this standard image. |
// Calculate diagonal size of this default size. |
| 1438 |
$diag_size_std = sqrt(pow($width_std, 2) + pow($height_std, 2)); |
$diag_size_std = sqrt(pow($width_std, 2) + pow($height_std, 2)); |
| 1439 |
$difference = abs($diag_size_new - $diag_size_std); |
$difference = abs($diag_size_new - $diag_size_std); |
| 1440 |
if ($difference < $closest_difference) { |
if ($difference < $closest_difference) { |
| 1442 |
$closest_difference = $difference; |
$closest_difference = $difference; |
| 1443 |
} |
} |
| 1444 |
} |
} |
| 1445 |
|
// If, for any reason, no default image derivative size has a width or |
| 1446 |
// Find out if desired width/height is the same (or extremely close) |
// height, fall back to IMAGE_THUMBNAIL. |
| 1447 |
// to the size of a default image derivative; if so, we will use the |
if (!isset($closest_std_size)) { |
| 1448 |
// default size image instead of generating our own image. |
$closest_std_size = IMAGE_THUMBNAIL; |
| 1449 |
|
} |
| 1450 |
|
|
| 1451 |
if ($closest_difference < 3) { |
if ($closest_difference < 3) { |
| 1452 |
$create_custom = FALSE; |
$create_custom = FALSE; |
| 1453 |
} |
} |
| 1493 |
$size['key'] = $closest_std_size; |
$size['key'] = $closest_std_size; |
| 1494 |
} |
} |
| 1495 |
} |
} |
| 1496 |
// Default to thumbnail if width and/or height is missing. |
// Default to thumbnail if width and height is missing. |
| 1497 |
else { |
else { |
| 1498 |
$size = image_get_sizes(); |
$size = $default_sizes[IMAGE_THUMBNAIL]; |
|
$size = $size[IMAGE_THUMBNAIL]; |
|
| 1499 |
$size['key'] = IMAGE_THUMBNAIL; |
$size['key'] = IMAGE_THUMBNAIL; |
| 1500 |
} |
} |
| 1501 |
return theme('img_assist_inline', $node, $size, $attributes); |
return theme('img_assist_inline', $node, $size, $attributes); |