Index: branches/RC/core/units/general/helpers/image_helper.php =================================================================== diff -u -N -r9598 -r9642 --- branches/RC/core/units/general/helpers/image_helper.php (.../image_helper.php) (revision 9598) +++ branches/RC/core/units/general/helpers/image_helper.php (.../image_helper.php) (revision 9642) @@ -2,6 +2,22 @@ class ImageHelper extends kHelper { + function parseFormat($format) + { + // resize:300x500;wm:inc/wm.png|c|-20 + // Array('max_width' => 300, 'max_height' => 500, 'wm_filename' => 'inc/wm.png', 'h_margin' => 'c', 'v_margin' => -20) + $format_parts = explode(';', $format); + foreach ($format_parts as $format_part) { + if (preg_match('/resize:(\d*)x(\d*)/', $format_part, $regs)) { + $res['max_width'] = $regs[1]; + $res['max_height'] = $regs[2]; + } + elseif (preg_match('/wm:([^\|]*)\|([^\|]*)\|([^\|]*)/', $format_part, $regs)) { + $res['wm_filename'] = FULL_PATH.THEMES_PATH.'/'.$regs[1]; + $res['h_margin'] = $regs[2]; + $res['v_margin'] = $regs[3]; + } + } /** * Resized given image to required dimensions & saves resized image to "resized" subfolder in source image folder * @@ -10,27 +26,34 @@ * @param mixed $max_height maximal allowed resized image height or false if no limit * @return string direct url to resized image */ - function ResizeImage($src_image, $max_width, $max_height) + function ResizeImage($src_image, $max_width, $max_height = null) { - if ($max_width > 0 || $max_height > 0) { - list ($max_width, $max_height, $needs_resize) = $this->GetImageDimensions($src_image, $max_width, $max_height); - - if ($needs_resize) { + if(isset($max_height)) { + $params['max_height'] = $max_height; + $params['max_width'] = $max_width; + } + else { + $params = $this->parseFormat($max_width); + } + if ($params['max_width'] > 0 || $params['max_height'] > 0) { + list ($params['max_width'], $params['max_height'], $needs_resize) = $this->GetImageDimensions($src_image, $params['max_width'], $params['max_height']); $src_path = dirname($src_image); - $dst_image = preg_replace('/^'.preg_quote($src_path, '/').'(.*)\.(.*)$/', $src_path.'/resized\\1_'.$max_width.'x'.$max_height.'.\\2', $src_image); + $dst_image = preg_replace('/^'.preg_quote($src_path, '/').'(.*)\.(.*)$/', $src_path.'/resized\\1_'.crc32(serialize($params)).'.\\2', $src_image); + if ($needs_resize || array_key_exists('wm_filename', $params) && $params['wm_filename']) { 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 = $this->ScaleImage($src_image, $dst_image, $max_width, $max_height); + $params['dst_image'] = $dst_image; + $image_resized = $this->ScaleImage($src_image, $params); if (!$image_resized) { // resize failed, because of server error $dst_image = $src_image; } - } // resize ok + } + } $src_image = $dst_image; } - } $base_url = rtrim($this->Application->BaseURL(), '/'); return preg_replace('/^'.preg_quote(FULL_PATH, '/').'(.*)/', $base_url.'\\1', $src_image); @@ -44,18 +67,18 @@ * @param int $dst_width destination image width (in pixels) * @param int $dst_height destination image height (in pixels) */ - function ScaleImage($src_image, $dst_image, $dst_width, $dst_height) + function ScaleImage($src_image, $params) { $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); +/* list ($params['max_width'], $params['max_height'], $resized) = $this->GetImageDimensions($src_image, $params['max_width'], $params['max_height']); if (!$resized) { // image dimensions are smaller or equals to required dimensions return false; - } + }*/ if (function_exists('imagecreatefromjpeg')) { // try to resize using GD @@ -74,21 +97,51 @@ $src_image_rs = @$read_function($src_image); if ($src_image_rs) { - $dst_image_rs = imagecreatetruecolor($dst_width, $dst_height); + $dst_image_rs = imagecreatetruecolor($params['max_width'], $params['max_height']); if ($file_extension == 'png') { // preserve transparency of PNG images $transparent_color = imagecolorallocate($dst_image_rs, 0, 0, 0); imagecolortransparent($dst_image_rs, $transparent_color); } - 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); + imagecopyresampled($dst_image_rs, $src_image_rs, 0, 0, 0, 0, $params['max_width'], $params['max_height'], $image_info[0], $image_info[1]); + + if(array_key_exists('wm_filename', $params) && $params['wm_filename'] && file_exists($params['wm_filename'])) + { + $logo_img = imagecreatefrompng($params['wm_filename']); + list($logo_width, $logo_height) = getimagesize($params['wm_filename']); + //if(($params['max_width'] > $logo_width * 2) && ($params['max_height'] > $logo_height * 2)) + //{ + imagealphablending($dst_image_rs, true); + //imagecopy($dst_image_rs, $logo_img, $params['max_width'] - $logo_width - $right_margin, $params['max_height'] - $logo_height - $bottom_margin, 0, 0, $logo_width, $logo_height); + if ($params['h_margin'] == 'c') { + $x_position = round($params['max_width']/2 - $logo_width/2); + } + elseif ($params['h_margin'] >= 0) { + $x_position = $params['h_margin']; + } + else { + $x_position = $params['max_width'] - (-$params['h_margin'] + $logo_width); + } + if ($params['v_margin'] == 'c') { + $y_position = round($params['max_height']/2 - $logo_height/2); + } + elseif ($params['v_margin'] >= 0) { + $y_position = $params['v_margin']; + } + else { + $y_position = $params['max_height'] - (-$params['v_margin'] + $logo_height); + } + imagecopy($dst_image_rs, $logo_img, $x_position, $y_position, 0, 0, $logo_width, $logo_height); + //} + } + return @$write_function($dst_image_rs, $params['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); + exec('/usr/bin/convert '.$src_image.' -resize '.$params['max_width'].'x'.$params['max_height'].' '.$params['dst_image'], $shell_output, $exec_status); return $exec_status == 0; } @@ -197,6 +250,72 @@ } /** + * 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; + $virtual_fields['Delete'.$field_name] = Array ('type' => 'int', 'not_null' => 1, 'default' => 0); + $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'); + $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); + } + /** * Puts existing item images (from subitem) to virtual fields (in main item) * * @param kCatDBItem $object Index: branches/RC/core/kernel/db/db_tag_processor.php =================================================================== diff -u -N -r9639 -r9642 --- branches/RC/core/kernel/db/db_tag_processor.php (.../db_tag_processor.php) (revision 9639) +++ branches/RC/core/kernel/db/db_tag_processor.php (.../db_tag_processor.php) (revision 9642) @@ -2059,16 +2059,19 @@ * @param Array $params * @return string */ - /*function ImageSrc($params) + function ImageSrc($params) { $max_width = isset($params['MaxWidth']) ? $params['MaxWidth'] : false; $max_height = isset($params['MaxHeight']) ? $params['MaxHeight'] : false; + $logo_filename = isset($params['LogoFilename']) ? $params['LogoFilename'] : false; + $logo_h_margin = isset($params['LogoHMargin']) ? $params['LogoHMargin'] : false; + $logo_v_margin = isset($params['LogoVMargin']) ? $params['LogoVMargin'] : false; $object =& $this->getObject($params); $field = $this->SelectParam($params, 'name,field'); - return $object->GetField($field, 'resize:'.$max_width.'x'.$max_height); - }*/ + return $object->GetField($field, 'resize:'.$max_width.'x'.$max_height.';wm:'.$logo_filename.'|'.$logo_h_margin.'|'.$logo_v_margin); + } } ?> \ No newline at end of file Index: branches/RC/core/kernel/utility/formatters/upload_formatter.php =================================================================== diff -u -N -r9560 -r9642 --- branches/RC/core/kernel/utility/formatters/upload_formatter.php (.../upload_formatter.php) (revision 9560) +++ branches/RC/core/kernel/utility/formatters/upload_formatter.php (.../upload_formatter.php) (revision 9642) @@ -185,7 +185,7 @@ $format = isset($options['format']) ? $options['format'] : false; } - if ($format && preg_match('/(file_urls|file_names|file_sizes|files_resized)(.*)/', $format, $regs)) { + if ($format && preg_match('/(file_urls|file_names|file_sizes|files_resized|wms)(.*)/', $format, $regs)) { if (!$value || $format == 'file_names') { // storage format matches display format OR no value return $value; @@ -231,7 +231,7 @@ $image_helper =& $this->Application->recallObject('ImageHelper'); /* @var $image_helper ImageHelper */ - return $image_helper->ResizeImage(FULL_PATH.$upload_dir.$value, $regs[1], $regs[2]); + return $image_helper->ResizeImage(FULL_PATH.$upload_dir.$value, $format); } switch ($format) {