Index: trunk/core/units/general/helpers/image_helper.php
===================================================================
diff -u -N
--- trunk/core/units/general/helpers/image_helper.php (revision 8719)
+++ trunk/core/units/general/helpers/image_helper.php (revision 0)
@@ -1,300 +0,0 @@
-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;
- }
-
- /**
- * Returns destination image size without actual resizing (useful for HTML tag)
- *
- * @param string $src_image full path to source image (already existing)
- * @param int $dst_width destination image width (in pixels)
- * @param int $dst_height destination image height (in pixels)
- * @return Array resized image dimensions (0 - width, 1 - height)
- */
- function GetImageDimensions($src_image, $dst_width, $dst_height)
- {
- $image_info = $this->getImageInfo($src_image);
- if (!$image_info) {
- return false;
- }
-
- $orig_width = $image_info[0];
- $orig_height = $image_info[1];
-
- $too_large = is_numeric($dst_width) ? ($orig_width > $dst_width) : false;
- $too_large = $too_large || (is_numeric($dst_height) ? ($orig_height > $dst_height) : false);
-
- if ($too_large) {
- $width_ratio = $dst_width ? $dst_width / $orig_width : 1;
- $height_ratio = $dst_height ? $dst_height / $orig_height : 1;
- $ratio = min($width_ratio, $height_ratio);
-
- $width = ceil($orig_width * $ratio);
- $height = ceil($orig_height * $ratio);
- }
- else {
- $width = $orig_width;
- $height = $orig_height;
- }
-
- return Array ($width, $height, $too_large);
- }
-
- /**
- * Returns image dimensions + checks if given file is existing image
- *
- * @param string $src_image full path to source image (already existing)
- * @return mixed
- */
- function getImageInfo($src_image)
- {
- if (!file_exists($src_image)) {
- return false;
- }
-
- $image_info = @getimagesize($src_image);
- if (!$image_info) {
- trigger_error('Image '.$src_image.' missing or invalid', E_USER_WARNING);
- return false;
- }
-
- 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;
- $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
- */
- function LoadItemImages(&$object)
- {
- $max_image_count = $this->Application->ConfigValue($object->Prefix.'_MaxImageCount');
-
- $sql = 'SELECT *
- FROM '.TABLE_PREFIX.'Images
- WHERE ResourceId = '.$object->GetDBField('ResourceId').'
- ORDER BY ImageId ASC
- LIMIT 0, '.(int)$max_image_count;
- $item_images = $this->Conn->Query($sql);
-
- $image_counter = 1;
- foreach ($item_images as $item_image) {
- $image_path = $item_image['ThumbPath'];
- if ($item_image['DefaultImg'] == 1 || $item_image['Name'] == 'main') {
- // process primary image separately
- $object->SetDBField('PrimaryImage', $image_path);
- $object->SetOriginalField('PrimaryImage', $image_path);
- $object->Fields['PrimaryImage']['original_field'] = $item_image['Name'];
- continue;
- }
-
- $object->SetDBField('Image'.$image_counter, $image_path);
- $object->SetOriginalField('Image'.$image_counter, $image_path);
- $object->Fields['Image'.$image_counter]['original_field'] = $item_image['Name'];
- $image_counter++;
- }
- }
-
- /**
- * Saves newly uploaded images to external image table
- *
- * @param kCatDBItem $object
- */
- function SaveItemImages(&$object)
- {
- $table_name = $this->Application->getUnitOption('img', 'TableName');
- $max_image_count = $this->Application->getUnitOption($object->Prefix, 'ImageCount'); // $this->Application->ConfigValue($object->Prefix.'_MaxImageCount');
-
- $i = 0;
- while ($i < $max_image_count) {
- $field = $i ? 'Image'.$i : 'PrimaryImage';
- $field_options = $object->GetFieldOptions($field);
-
- $image_src = $object->GetDBField($field);
- if ($image_src) {
- if (isset($field_options['original_field'])) {
- $key_clause = 'Name = '.$this->Conn->qstr($field_options['original_field']).' AND ResourceId = '.$object->GetDBField('ResourceId');
-
- if ($object->GetDBField('Delete'.$field)) {
- // if item was cloned, then new filename is in db (not in $image_src)
- $sql = 'SELECT ThumbPath
- FROM '.$table_name.'
- WHERE '.$key_clause;
- $image_src = $this->Conn->GetOne($sql);
- if (@unlink(FULL_PATH.$image_src)) {
- $sql = 'DELETE FROM '.$table_name.'
- WHERE '.$key_clause;
- $this->Conn->Query($sql);
- }
- }
- else {
- // image record found -> update
- $fields_hash = Array (
- 'ThumbPath' => $image_src,
- );
-
- $this->Conn->doUpdate($fields_hash, $table_name, $key_clause);
- }
- }
- else {
- // image record not found -> create
- $fields_hash = Array (
- 'ResourceId' => $object->GetDBField('ResourceId'),
- 'Name' => $field,
- 'AltName' => $field,
- 'Enabled' => STATUS_ACTIVE,
- 'DefaultImg' => $i ? 0 : 1, // first image is primary, others not primary
- 'ThumbPath' => $image_src,
- );
-
- $this->Conn->doInsert($fields_hash, $table_name);
- }
- }
- $i++;
- }
- }
-
- /**
- * Preserves cloned item images to be rewrited with original item images
- *
- * @param Array $field_values
- */
- function PreserveItemImages(&$field_values)
- {
- foreach ($field_values as $field_name => $field_value) {
- if (!is_array($field_value)) continue;
-
- if (isset($field_value['upload']) && ($field_value['error'] == UPLOAD_ERR_NO_FILE)) {
- // this is upload field, but nothing was uploaded this time
- unset($field_values[$field_name]);
- }
- }
- }
- }
-
-?>
\ No newline at end of file