Index: branches/RC/core/units/general/helpers/image_helper.php =================================================================== diff -u -N -r9645 -r9651 --- branches/RC/core/units/general/helpers/image_helper.php (.../image_helper.php) (revision 9645) +++ branches/RC/core/units/general/helpers/image_helper.php (.../image_helper.php) (revision 9651) @@ -2,10 +2,14 @@ class ImageHelper extends kHelper { + /** + * Parses format string into array + * + * @param string $format sample format: "resize:300x500;wm:inc/wm.png|c|-20" + * @return Array sample result: Array('max_width' => 300, 'max_height' => 500, 'wm_filename' => 'inc/wm.png', 'h_margin' => 'c', 'v_margin' => -20) + */ 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)) { @@ -18,7 +22,10 @@ $res['v_margin'] = $regs[3]; } } + + return $res; } + /** * Resized given image to required dimensions & saves resized image to "resized" subfolder in source image folder * @@ -36,11 +43,15 @@ 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); + + $src_path = dirname($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 $params['dst_image'] = $dst_image; @@ -50,11 +61,12 @@ $dst_image = $src_image; } - // resize ok + // resize ok + } } - $src_image = $dst_image; - } + $src_image = $dst_image; + } $base_url = rtrim($this->Application->BaseURL(), '/'); return preg_replace('/^'.preg_quote(FULL_PATH, '/').'(.*)/', $base_url.'\\1', $src_image); @@ -112,10 +124,12 @@ { $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); } @@ -125,6 +139,7 @@ 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); } @@ -134,9 +149,11 @@ 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); } } @@ -251,72 +268,6 @@ } /** - * 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