Index: trunk/core/units/images/image_tag_processor.php =================================================================== diff -u -N -r8474 -r8686 --- trunk/core/units/images/image_tag_processor.php (.../image_tag_processor.php) (revision 8474) +++ trunk/core/units/images/image_tag_processor.php (.../image_tag_processor.php) (revision 8686) @@ -23,26 +23,24 @@ } /** - * [AGGREGATED TAG] works as + * [AGGREGATED TAGS] works as * * @param Array $params * @return string */ - function ItemImage($params) + function ItemImageTag($params) { $this->LoadItemImage($params); - return $this->Image($params); + return $this->$params['original_tag']($params); } function LargeImageExists($params) { $object =& $this->getObject($params); - if ( $object->GetDBField('SameImages') == null || $object->GetDBField('SameImages')=='1' ) - { + if ($object->GetDBField('SameImages') == null || $object->GetDBField('SameImages') == 1) { return false; } - else - { + else { return true; } } @@ -56,25 +54,19 @@ $object->Clear(); - // if we need primary thumbnail which is preloaded with products list + // if we need primary thumbnail which is preloaded with category item's list + $is_primary = $this->SelectParam($params, 'primary,Primary'); + if ( - $this->SelectParam($params, 'thumbnail,Thumbnail') && - ( - ( - $this->SelectParam($params, 'primary,Primary') - || - !isset($params['name']) - ) - && - !$this->Application->GetVar('img_id') - && - isset($parent_item->Fields['LocalThumb']) - ) - ) - { + ($is_primary || !isset($params['name'])) + && + !$this->Application->GetVar('img_id') + && + isset($parent_item->Fields['LocalThumb']) + ) { $object->SetDefaultValues(); $object->SetDBField('Url', $parent_item->GetDBField('FullUrl')); - $object->SetDBFieldsFromHash($parent_item->GetFieldValues(), Array('SameImages', 'LocalThumb', 'ThumbPath', 'ThumbUrl', 'LocalImage', 'LocalPath') ); + $object->SetDBFieldsFromHash($parent_item->GetFieldValues(), Array('SameImages', 'LocalThumb', 'ThumbPath', 'ThumbUrl', 'LocalImage', 'LocalPath')); $object->Loaded = true; } else { // if requested image is not primary thumbnail - load it directly @@ -90,6 +82,10 @@ elseif ( getArrayValue($params, 'name') ) { //load by name $keys['Name'] = $params['name']; } + elseif ( getArrayValue($params, 'field') ) { //load by virtual field name in main object + $field_options = $parent_item->GetFieldOptions($params['field']); + $keys['Name'] = $field_options['original_field']; + } elseif ( $image_id = $this->Application->GetVar($this->Prefix.'_id') ) { $keys['ImageId'] = $image_id; } @@ -101,16 +97,68 @@ } } + function getImageDimension($type, $params) + { + $ret = isset($params['Max'.$type]) ? $params['Max'.$type] : false; + if (!$ret) { + return $ret; + } + $parent_prefix = $this->Application->getUnitOption($this->Prefix, 'ParentPrefix'); + + if ($ret == 'thumbnail') { + $ret = $this->Application->ConfigValue($parent_prefix.'_ThumbnailImage'.$type); + } + if ($ret == 'fullsize') { + $ret = $this->Application->ConfigValue($parent_prefix.'_FullImage'.$type); + } + + return $ret; + } + function ImageSrc($params) { $object =& $this->getObject($params); - $base_url = rtrim($this->Application->BaseURL(), '/'); + if ($object->GetDBField('SameImages') && $object->GetDBField('ThumbPath')) { + // we can auto-resize image, when source image available & we use same image for thumbnail & full image in admin + + $image_helper =& $this->Application->recallObject('ImageHelper'); + /* @var $image_helper ImageHelper */ + + $max_width = $this->getImageDimension('Width', $params); + $max_height = $this->getImageDimension('Height', $params); + + if ($max_width > 0 || $max_height > 0) { + // image auto-resize is required + $src_image = FULL_PATH.$object->GetDBField('ThumbPath'); + list ($max_width, $max_height, $resized) = $image_helper->GetImageDimensions($src_image, $max_width, $max_height); + + if ($resized) { + $src_path = FULL_PATH.($this->Application->IsAdmin() ? IMAGES_PENDING_PATH : IMAGES_PATH); + $dst_image = preg_replace('/^'.preg_quote($src_path, '/').'(.*)\.(.*)$/', $src_path.'resized/\\1_'.$max_width.'x'.$max_height.'.\\2', $src_image); + if (!file_exists($dst_image) || filemtime($src_image) > filemtime($dst_image)) { + // resized image not available OR should be recreated due source image change + $image_resized = $image_helper->ScaleImage($src_image, $dst_image, $max_width, $max_height); + if (!$image_resized) { + // resize failed, because of server error + $dst_image = $src_image; + } + } + + return preg_replace('/^'.preg_quote(FULL_PATH, '/').'(.*)/', $base_url.'\\1', $dst_image); + } + } + } + $ret = ''; - // if we need thumb, or full image is same as thumb - if ( $this->SelectParam($params, 'thumbnail,Thumbnail') || $object->GetDBField('SameImages') ) - { + + // if we need thumbnail, or full image is same as thumbnail + $show_thumbnail = $this->SelectParam($params, 'thumbnail,Thumbnail') || // old style + (isset($params['MaxWidth']) && $params['MaxWidth'] == 'thumbnail') || // new style + (isset($params['MaxHeight']) && $params['MaxHeight'] == 'thumbnail'); + + if ($show_thumbnail || $object->GetDBField('SameImages')) { // return local image or url $ret = $object->GetDBField('LocalThumb') ? $base_url.$object->GetDBField('ThumbPath') : $object->GetDBField('ThumbUrl'); if ($object->GetDBField('LocalThumb') && !file_exists(FULL_PATH.$object->GetDBField('ThumbPath')) && !constOn('DBG_IMAGE_RECOVERY')) { @@ -160,7 +208,10 @@ $image_helper =& $this->Application->recallObject('ImageHelper'); /* @var $image_helper ImageHelper */ - $image_dimensions = $image_helper->GetImageDimensions($img_path, getArrayValue($params, 'MaxWidth'), getArrayValue($params, 'MaxHeight')); + $max_width = $this->getImageDimension('Width', $params); + $max_height = $this->getImageDimension('Height', $params); + + $image_dimensions = $image_helper->GetImageDimensions($img_path, $max_width, $max_height); if (!$image_dimensions) { return false; } Index: trunk/core/units/images/images_config.php =================================================================== diff -u -N -r8388 -r8686 --- trunk/core/units/images/images_config.php (.../images_config.php) (revision 8388) +++ trunk/core/units/images/images_config.php (.../images_config.php) (revision 8686) @@ -1,5 +1,5 @@ 'img', 'Clones' => Array( @@ -9,35 +9,49 @@ /*'p-img' => Array('ParentPrefix' => 'p'),*/ 'c-img' => Array('ParentPrefix' => 'c'), ), - + 'ItemClass' => Array('class'=>'kDBItem','file'=>'','build_event'=>'OnItemBuild'), 'ListClass' => Array('class'=>'kDBList','file'=>'','build_event'=>'OnListBuild'), 'EventHandlerClass' => Array('class'=>'ImageEventHandler','file'=>'image_event_handler.php','build_event'=>'OnBuild'), 'TagProcessorClass' => Array('class'=>'ImageTagProcessor','file'=>'image_tag_processor.php','build_event'=>'OnBuild'), 'AutoLoad' => true, - + 'AggregateTags' => Array ( Array ( 'AggregateTo' => '#PARENT#', 'AggregatedTagName' => 'Image', - 'LocalTagName' => 'ItemImage', + 'LocalTagName' => 'ItemImageTag', 'LocalSpecial' => '-item', ), - + Array ( 'AggregateTo' => '#PARENT#', + 'AggregatedTagName' => 'ImageSrc', + 'LocalTagName' => 'ItemImageTag', + 'LocalSpecial' => '-item', + ), + + Array ( + 'AggregateTo' => '#PARENT#', + 'AggregatedTagName' => 'ImageSize', + 'LocalTagName' => 'ItemImageTag', + 'LocalSpecial' => '-item', + ), + + Array ( + 'AggregateTo' => '#PARENT#', 'AggregatedTagName' => 'ListImages', 'LocalTagName' => 'PrintList2', 'LocalSpecial' => 'list', ), - + Array ( 'AggregateTo' => '#PARENT#', 'AggregatedTagName' => 'LargeImageExists', 'LocalTagName' => 'LargeImageExists', - ), + ), ), - + 'QueryString' => Array( 1 => 'id', 2 => 'page', @@ -62,23 +76,23 @@ 'show_disabled' => Array('label' => 'la_Disabled', 'on_sql' => '', 'off_sql' => 'Enabled != 0' ), ) ), - + 'CalculatedFields' => Array( '' => Array( 'Preview' => '0', ), ), - + 'ListSQLs' => Array( ''=>'SELECT * FROM %s', ), // key - special, value - list select sql 'ItemSQLs' => Array( ''=>'SELECT * FROM %s', ), - 'ListSortings' => Array( + 'ListSortings' => Array( '' => Array( 'ForcedSorting' => Array('Priority' => 'desc'), 'Sorting' => Array('Name' => 'asc'), ) - ), + ), 'Fields' => Array( 'ImageId' => Array('type' => 'int', 'not_null' => 1, 'default' => 0), 'ResourceId' => Array('type'=>'int', 'not_null'=>1, 'default' => 0), @@ -124,7 +138,7 @@ 'Preview' => Array(), 'ImageUrl' => Array(), ), - + 'Grids' => Array( 'Default' => Array( 'Icons' => Array('default'=>'icon17_custom.gif','1_0'=>'icon16_image.gif','0_0'=>'icon16_image_disabled.gif','1_1'=>'icon16_image_primary.gif'), @@ -135,7 +149,7 @@ 'Enabled' => Array( 'title'=>'la_col_ImageEnabled' ), 'Preview' => Array( 'title'=>'la_col_Preview', 'data_block' => 'image_preview_td' ), ), - + ), ), ); Index: trunk/kernel/units/images/image_tag_processor.php =================================================================== diff -u -N -r8474 -r8686 --- trunk/kernel/units/images/image_tag_processor.php (.../image_tag_processor.php) (revision 8474) +++ trunk/kernel/units/images/image_tag_processor.php (.../image_tag_processor.php) (revision 8686) @@ -23,26 +23,24 @@ } /** - * [AGGREGATED TAG] works as + * [AGGREGATED TAGS] works as * * @param Array $params * @return string */ - function ItemImage($params) + function ItemImageTag($params) { $this->LoadItemImage($params); - return $this->Image($params); + return $this->$params['original_tag']($params); } function LargeImageExists($params) { $object =& $this->getObject($params); - if ( $object->GetDBField('SameImages') == null || $object->GetDBField('SameImages')=='1' ) - { + if ($object->GetDBField('SameImages') == null || $object->GetDBField('SameImages') == 1) { return false; } - else - { + else { return true; } } @@ -56,25 +54,19 @@ $object->Clear(); - // if we need primary thumbnail which is preloaded with products list + // if we need primary thumbnail which is preloaded with category item's list + $is_primary = $this->SelectParam($params, 'primary,Primary'); + if ( - $this->SelectParam($params, 'thumbnail,Thumbnail') && - ( - ( - $this->SelectParam($params, 'primary,Primary') - || - !isset($params['name']) - ) - && - !$this->Application->GetVar('img_id') - && - isset($parent_item->Fields['LocalThumb']) - ) - ) - { + ($is_primary || !isset($params['name'])) + && + !$this->Application->GetVar('img_id') + && + isset($parent_item->Fields['LocalThumb']) + ) { $object->SetDefaultValues(); $object->SetDBField('Url', $parent_item->GetDBField('FullUrl')); - $object->SetDBFieldsFromHash($parent_item->GetFieldValues(), Array('SameImages', 'LocalThumb', 'ThumbPath', 'ThumbUrl', 'LocalImage', 'LocalPath') ); + $object->SetDBFieldsFromHash($parent_item->GetFieldValues(), Array('SameImages', 'LocalThumb', 'ThumbPath', 'ThumbUrl', 'LocalImage', 'LocalPath')); $object->Loaded = true; } else { // if requested image is not primary thumbnail - load it directly @@ -90,6 +82,10 @@ elseif ( getArrayValue($params, 'name') ) { //load by name $keys['Name'] = $params['name']; } + elseif ( getArrayValue($params, 'field') ) { //load by virtual field name in main object + $field_options = $parent_item->GetFieldOptions($params['field']); + $keys['Name'] = $field_options['original_field']; + } elseif ( $image_id = $this->Application->GetVar($this->Prefix.'_id') ) { $keys['ImageId'] = $image_id; } @@ -101,16 +97,68 @@ } } + function getImageDimension($type, $params) + { + $ret = isset($params['Max'.$type]) ? $params['Max'.$type] : false; + if (!$ret) { + return $ret; + } + $parent_prefix = $this->Application->getUnitOption($this->Prefix, 'ParentPrefix'); + + if ($ret == 'thumbnail') { + $ret = $this->Application->ConfigValue($parent_prefix.'_ThumbnailImage'.$type); + } + if ($ret == 'fullsize') { + $ret = $this->Application->ConfigValue($parent_prefix.'_FullImage'.$type); + } + + return $ret; + } + function ImageSrc($params) { $object =& $this->getObject($params); - $base_url = rtrim($this->Application->BaseURL(), '/'); + if ($object->GetDBField('SameImages') && $object->GetDBField('ThumbPath')) { + // we can auto-resize image, when source image available & we use same image for thumbnail & full image in admin + + $image_helper =& $this->Application->recallObject('ImageHelper'); + /* @var $image_helper ImageHelper */ + + $max_width = $this->getImageDimension('Width', $params); + $max_height = $this->getImageDimension('Height', $params); + + if ($max_width > 0 || $max_height > 0) { + // image auto-resize is required + $src_image = FULL_PATH.$object->GetDBField('ThumbPath'); + list ($max_width, $max_height, $resized) = $image_helper->GetImageDimensions($src_image, $max_width, $max_height); + + if ($resized) { + $src_path = FULL_PATH.($this->Application->IsAdmin() ? IMAGES_PENDING_PATH : IMAGES_PATH); + $dst_image = preg_replace('/^'.preg_quote($src_path, '/').'(.*)\.(.*)$/', $src_path.'resized/\\1_'.$max_width.'x'.$max_height.'.\\2', $src_image); + if (!file_exists($dst_image) || filemtime($src_image) > filemtime($dst_image)) { + // resized image not available OR should be recreated due source image change + $image_resized = $image_helper->ScaleImage($src_image, $dst_image, $max_width, $max_height); + if (!$image_resized) { + // resize failed, because of server error + $dst_image = $src_image; + } + } + + return preg_replace('/^'.preg_quote(FULL_PATH, '/').'(.*)/', $base_url.'\\1', $dst_image); + } + } + } + $ret = ''; - // if we need thumb, or full image is same as thumb - if ( $this->SelectParam($params, 'thumbnail,Thumbnail') || $object->GetDBField('SameImages') ) - { + + // if we need thumbnail, or full image is same as thumbnail + $show_thumbnail = $this->SelectParam($params, 'thumbnail,Thumbnail') || // old style + (isset($params['MaxWidth']) && $params['MaxWidth'] == 'thumbnail') || // new style + (isset($params['MaxHeight']) && $params['MaxHeight'] == 'thumbnail'); + + if ($show_thumbnail || $object->GetDBField('SameImages')) { // return local image or url $ret = $object->GetDBField('LocalThumb') ? $base_url.$object->GetDBField('ThumbPath') : $object->GetDBField('ThumbUrl'); if ($object->GetDBField('LocalThumb') && !file_exists(FULL_PATH.$object->GetDBField('ThumbPath')) && !constOn('DBG_IMAGE_RECOVERY')) { @@ -160,7 +208,10 @@ $image_helper =& $this->Application->recallObject('ImageHelper'); /* @var $image_helper ImageHelper */ - $image_dimensions = $image_helper->GetImageDimensions($img_path, getArrayValue($params, 'MaxWidth'), getArrayValue($params, 'MaxHeight')); + $max_width = $this->getImageDimension('Width', $params); + $max_height = $this->getImageDimension('Height', $params); + + $image_dimensions = $image_helper->GetImageDimensions($img_path, $max_width, $max_height); if (!$image_dimensions) { return false; } Index: trunk/kernel/units/images/images_config.php =================================================================== diff -u -N -r8388 -r8686 --- trunk/kernel/units/images/images_config.php (.../images_config.php) (revision 8388) +++ trunk/kernel/units/images/images_config.php (.../images_config.php) (revision 8686) @@ -1,5 +1,5 @@ 'img', 'Clones' => Array( @@ -9,35 +9,49 @@ /*'p-img' => Array('ParentPrefix' => 'p'),*/ 'c-img' => Array('ParentPrefix' => 'c'), ), - + 'ItemClass' => Array('class'=>'kDBItem','file'=>'','build_event'=>'OnItemBuild'), 'ListClass' => Array('class'=>'kDBList','file'=>'','build_event'=>'OnListBuild'), 'EventHandlerClass' => Array('class'=>'ImageEventHandler','file'=>'image_event_handler.php','build_event'=>'OnBuild'), 'TagProcessorClass' => Array('class'=>'ImageTagProcessor','file'=>'image_tag_processor.php','build_event'=>'OnBuild'), 'AutoLoad' => true, - + 'AggregateTags' => Array ( Array ( 'AggregateTo' => '#PARENT#', 'AggregatedTagName' => 'Image', - 'LocalTagName' => 'ItemImage', + 'LocalTagName' => 'ItemImageTag', 'LocalSpecial' => '-item', ), - + Array ( 'AggregateTo' => '#PARENT#', + 'AggregatedTagName' => 'ImageSrc', + 'LocalTagName' => 'ItemImageTag', + 'LocalSpecial' => '-item', + ), + + Array ( + 'AggregateTo' => '#PARENT#', + 'AggregatedTagName' => 'ImageSize', + 'LocalTagName' => 'ItemImageTag', + 'LocalSpecial' => '-item', + ), + + Array ( + 'AggregateTo' => '#PARENT#', 'AggregatedTagName' => 'ListImages', 'LocalTagName' => 'PrintList2', 'LocalSpecial' => 'list', ), - + Array ( 'AggregateTo' => '#PARENT#', 'AggregatedTagName' => 'LargeImageExists', 'LocalTagName' => 'LargeImageExists', - ), + ), ), - + 'QueryString' => Array( 1 => 'id', 2 => 'page', @@ -62,23 +76,23 @@ 'show_disabled' => Array('label' => 'la_Disabled', 'on_sql' => '', 'off_sql' => 'Enabled != 0' ), ) ), - + 'CalculatedFields' => Array( '' => Array( 'Preview' => '0', ), ), - + 'ListSQLs' => Array( ''=>'SELECT * FROM %s', ), // key - special, value - list select sql 'ItemSQLs' => Array( ''=>'SELECT * FROM %s', ), - 'ListSortings' => Array( + 'ListSortings' => Array( '' => Array( 'ForcedSorting' => Array('Priority' => 'desc'), 'Sorting' => Array('Name' => 'asc'), ) - ), + ), 'Fields' => Array( 'ImageId' => Array('type' => 'int', 'not_null' => 1, 'default' => 0), 'ResourceId' => Array('type'=>'int', 'not_null'=>1, 'default' => 0), @@ -124,7 +138,7 @@ 'Preview' => Array(), 'ImageUrl' => Array(), ), - + 'Grids' => Array( 'Default' => Array( 'Icons' => Array('default'=>'icon17_custom.gif','1_0'=>'icon16_image.gif','0_0'=>'icon16_image_disabled.gif','1_1'=>'icon16_image_primary.gif'), @@ -135,7 +149,7 @@ 'Enabled' => Array( 'title'=>'la_col_ImageEnabled' ), 'Preview' => Array( 'title'=>'la_col_Preview', 'data_block' => 'image_preview_td' ), ), - + ), ), ); Index: trunk/themes/default2007/platform/elements/forms.tpl =================================================================== diff -u -N -r8674 -r8686 --- trunk/themes/default2007/platform/elements/forms.tpl (.../forms.tpl) (revision 8674) +++ trunk/themes/default2007/platform/elements/forms.tpl (.../forms.tpl) (revision 8686) @@ -96,7 +96,10 @@
- " alt=""/> + " target="_blank"> + + +
" tabindex="" style=""> [upload]" value="" /> Index: trunk/core/kernel/processors/tag_processor.php =================================================================== diff -u -N -r8402 -r8686 --- trunk/core/kernel/processors/tag_processor.php (.../tag_processor.php) (revision 8402) +++ trunk/core/kernel/processors/tag_processor.php (.../tag_processor.php) (revision 8686) @@ -105,7 +105,8 @@ $processor =& $this->Application->recallObject($__tag_processor); $processor->Prefix = $tmp['prefix']; $processor->Special = getArrayValue($tag_mapping, 2) ? $tag_mapping[2] : $tmp['special']; - + + $params['original_tag'] = $Method; // allows to define same method for different aggregated tags in same tag processor $params['PrefixSpecial'] = $this->getPrefixSpecial(); // $prefix; $ret = $processor->ProcessParsedTag($tag_mapping[1], $params, $prefix); if (isset($params['result_to_var'])) { Index: trunk/core/kernel/utility/formatters/upload_formatter.php =================================================================== diff -u -N -r8397 -r8686 --- trunk/core/kernel/utility/formatters/upload_formatter.php (.../upload_formatter.php) (revision 8397) +++ trunk/core/kernel/utility/formatters/upload_formatter.php (.../upload_formatter.php) (revision 8686) @@ -7,11 +7,11 @@ function kUploadFormatter() { - if ($this->DestinationPath) - { + parent::kBase(); + + if ($this->DestinationPath) { $this->FullPath = FULL_PATH.$this->DestinationPath; } - parent::kBase(); } @@ -235,8 +235,9 @@ function kPictureFormatter() { - $this->NakeLookupPath = IMAGES_PATH; - $this->DestinationPath = IMAGES_PENDING_PATH; + $this->NakeLookupPath = IMAGES_PATH; // used ? + $this->DestinationPath = constOn('ADMIN') ? IMAGES_PENDING_PATH : IMAGES_PATH; + parent::kUploadFormatter(); } Index: trunk/kernel/admin_templates/incs/image_blocks.tpl =================================================================== diff -u -N -r8413 -r8686 --- trunk/kernel/admin_templates/incs/image_blocks.tpl (.../image_blocks.tpl) (revision 8413) +++ trunk/kernel/admin_templates/incs/image_blocks.tpl (.../image_blocks.tpl) (revision 8686) @@ -2,7 +2,7 @@ " border="0" />
- + ">
@@ -45,7 +45,7 @@
- + ">
Index: trunk/core/units/general/cat_event_handler.php =================================================================== diff -u -N -r8562 -r8686 --- trunk/core/units/general/cat_event_handler.php (.../cat_event_handler.php) (revision 8562) +++ trunk/core/units/general/cat_event_handler.php (.../cat_event_handler.php) (revision 8686) @@ -670,7 +670,10 @@ function OnAfterItemLoad(&$event) { $special = substr($event->Special, -6); + $object =& $event->getObject(); + /* @var $object kCatDBItem */ + if ($special == 'import' || $special == 'export') { $image_data = $object->getPrimaryImageData(); @@ -688,7 +691,7 @@ } } - //substituiting pending status value for pending editing + // substituiting pending status value for pending editing if ($object->HasField('OrgId') && $object->GetDBField('OrgId') > 0 && $object->GetDBField('Status') == -2) { $options = $object->Fields['Status']['options']; foreach ($options as $key => $val) { @@ -697,14 +700,28 @@ } $object->Fields['Status']['options'] = $new_options; } - /*elseif (!$this->Application->IsAdmin() && $object->GetDBField('Status') != 1 && $object->Prefix != 'cms') { - header('HTTP/1.0 404 Not Found'); - while (ob_get_level()) { ob_end_clean(); } - $this->Application->HTML = $this->Application->ParseBlock(array('name'=>$this->Application->ConfigValue('ErrorTemplate'))); - $this->Application->Done(); - exit(); - }*/ + // linking existing images for item with virtual fields + $sql = 'SELECT * + FROM '.TABLE_PREFIX.'Images + WHERE ResourceId = '.$object->GetDBField('ResourceId').' + ORDER BY ImageId ASC'; + $item_images = $this->Conn->Query($sql); + + $image_counter = 1; + foreach ($item_images as $item_image) { + $image_path = preg_replace('/^'.preg_quote(IMAGES_PATH, '/').'(.*)/', '\\1', $item_image['ThumbPath']); + if ($item_image['DefaultImg'] == 1 || $item_image['Name'] == 'main') { + // process primary image separately + $object->SetDBField('PrimaryImage', $image_path); + $object->Fields['PrimaryImage']['original_field'] = $item_image['Name']; + continue; + } + + $object->SetDBField('Image'.$image_counter, $image_path); + $object->Fields['Image'.$image_counter]['original_field'] = $item_image['Name']; + $image_counter++; + } } function OnAfterItemUpdate(&$event) @@ -2133,6 +2150,19 @@ return $owner_field; } + /** + * Creates virtual image fields for item + * + * @param kEvent $event + */ + function OnAfterConfigRead(&$event) + { + $image_helper =& $this->Application->recallObject('ImageHelper'); + /* @var $image_helper ImageHelper */ + + $image_helper->createItemImages($event->Prefix); + } + } ?> \ No newline at end of file Index: trunk/kernel/images/pending/.cvs =================================================================== diff -u -N --- trunk/kernel/images/pending/.cvs (revision 13) +++ trunk/kernel/images/pending/.cvs (revision 0) @@ -1 +0,0 @@ \ No newline at end of file Index: trunk/core/admin_templates/incs/image_blocks.tpl =================================================================== diff -u -N -r8413 -r8686 --- trunk/core/admin_templates/incs/image_blocks.tpl (.../image_blocks.tpl) (revision 8413) +++ trunk/core/admin_templates/incs/image_blocks.tpl (.../image_blocks.tpl) (revision 8686) @@ -2,7 +2,7 @@ " border="0" />
- + ">
@@ -45,7 +45,7 @@
- + ">
Index: trunk/core/units/general/helpers/image_helper.php =================================================================== diff -u -N -r8474 -r8686 --- trunk/core/units/general/helpers/image_helper.php (.../image_helper.php) (revision 8474) +++ trunk/core/units/general/helpers/image_helper.php (.../image_helper.php) (revision 8686) @@ -12,7 +12,46 @@ */ function ScaleImage($src_image, $dst_image, $dst_width, $dst_height) { + $image_info = $this->getImageInfo($src_image); + if (!$image_info) { + return false; + } + list ($dst_width, $dst_height, $resized) = $this->GetImageDimensions($src_image, $dst_width, $dst_height); + if (!$resized) { + // image dimensions are smaller or equals to required dimensions + return false; + } + + if (function_exists('imagecreatefromjpeg')) { + // try to resize using GD + $resize_map = Array ( + 'image/jpeg' => 'imagecreatefromjpeg:imagejpeg', + 'image/gif' => 'imagecreatefromgif:imagegif', + 'image/png' => 'imagecreatefrompng:imagepng', + ); + + $mime_type = $image_info['mime']; + if (!isset($resize_map[$mime_type])) { + return false; + } + + list ($read_function, $write_function) = explode(':', $resize_map[$mime_type]); + + $src_image_rs = @$read_function($src_image); + if ($src_image_rs) { + $dst_image_rs = imagecreatetruecolor($dst_width, $dst_height); + imagecopyresampled($dst_image_rs, $src_image_rs, 0, 0, 0, 0, $dst_width, $dst_height, $image_info[0], $image_info[1]); + return @$write_function($dst_image_rs, $dst_image, 100); + } + } + else { + // try to resize using ImageMagick + exec('/usr/bin/convert '.$src_image.' -resize '.$dst_width.'x'.$dst_height.' '.$dst_image, $shell_output, $exec_status); + return $exec_status == 0; + } + + return false; } /** @@ -49,7 +88,7 @@ $height = $orig_height; } - return Array ($width, $height); + return Array ($width, $height, $too_large); } /** @@ -72,6 +111,82 @@ return $image_info; } + + /** + * Determines what image fields should be created (from post or just dummy fields for 1st upload) + * + * @param string $prefix + */ + function createItemImages($prefix) + { + $items_info = $this->Application->GetVar($prefix); + if ($items_info) { + list ($id, $fields_values) = each($items_info); + $this->createImageFields($prefix, $fields_values); + } + else { + $this->createImageFields($prefix, Array()); + } + } + + /** + * Dynamically creates virtual fields for item for each image field in submit + * + * @param string $prefix + * @param Array $fields_values + */ + function createImageFields($prefix, $fields_values) + { + $field_options = Array ( + 'type' => 'string', + 'formatter' => 'kPictureFormatter', +// 'skip_empty' => 1, + 'max_len' => 240, + 'default' => '', + 'not_null' => 1, + 'include_path' => 1, + 'allowed_types' => Array ('image/jpeg', 'image/pjpeg', 'image/png', 'image/gif', 'image/bmp'), + 'error_msgs' => Array ( + 'bad_file_format' => '!la_error_InvalidFileFormat!', + 'bad_file_size' => '!la_error_FileTooLarge!', + 'cant_save_file' => '!la_error_cant_save_file!' + ), + ); + + $fields = $this->Application->getUnitOption($prefix, 'Fields'); + $virtual_fields = $this->Application->getUnitOption($prefix, 'VirtualFields'); + + $image_count = 0; + foreach ($fields_values as $field_name => $field_value) { + if (preg_match('/^(Image[\d]+|PrimaryImage)$/', $field_name)) { + $fields[$field_name] = $field_options; + $virtual_fields[$field_name] = $field_options; + $image_count++; + } + } + + if (!$image_count) { + // no images found in POST -> create default image fields + $image_names = Array ('PrimaryImage' => ''); + $image_count = $this->Application->ConfigValue($prefix.'_MaxImageCount'); + if (!$image_count) { + $image_count = 3; // primary image + 2 additional images + } + + $created_count = 1; + while ($created_count < $image_count) { + $image_names['Image'.$created_count] = ''; + $created_count++; + } + + $this->createImageFields($prefix, $image_names); + return ; + } + + $this->Application->setUnitOption($prefix, 'ImageCount', $image_count); + $this->Application->setUnitOption($prefix, 'Fields', $fields); + $this->Application->setUnitOption($prefix, 'VirtualFields', $virtual_fields); + } } ?> \ No newline at end of file