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