This is final look of uploadItemResources function
public function uploadItemResources($aResources, $itemId) {
if(is_array($aResources) && !empty($aResources)) {
$limit_reached = false;
$itemResourceManager = ItemResource::newInstance();
$folder = osc_uploads_path() . floor($itemId / 100) . '/';
$maxImagesPerItem = osc_max_images_per_item();
$totalItemImages = $itemResourceManager->countResources($itemId);
$newUploadImages = 0;
foreach($aResources['error'] as $key => $error) {
if($maxImagesPerItem == 0 || ($maxImagesPerItem > 0 && ($totalItemImages + $newUploadImages) < $maxImagesPerItem)) {
if($error == UPLOAD_ERR_OK) {
$tmpName = $aResources['tmp_name'][$key];
$order = $aResources['order'][$key];
$imgres = ImageProcessing::fromFile($tmpName);
$extension = osc_apply_filter('upload_image_extension', $imgres->getExt());
$mime = osc_apply_filter('upload_image_mime', $imgres->getMime());
// Create normal size
$normal_path = $path = $tmpName . '_normal';
$size = explode('x', osc_normal_dimensions());
$img = $imgres->autoRotate();
$img = $img->resizeTo($size[0], $size[1]);
if (osc_is_watermark_text()) {
$img->doWatermarkText(osc_watermark_text(), osc_watermark_text_color());
} elseif (osc_is_watermark_image()) {
$img->doWatermarkImage();
}
$img->saveToFile($path, $extension);
// Create preview
$path = $tmpName . '_preview';
$size = explode('x', osc_preview_dimensions());
ImageProcessing::fromFile($normal_path)->resizeTo($size[0], $size[1])->saveToFile($path, $extension);
// Create thumbnail
$path = $tmpName . '_thumbnail';
$size = explode('x', osc_thumbnail_dimensions());
ImageProcessing::fromFile($normal_path)->resizeTo($size[0], $size[1])->saveToFile($path, $extension);
$newUploadImages++;
$itemResourceManager->insert(array('fk_i_item_id' => $itemId));
$resourceId = $itemResourceManager->dao->insertedId();
if (!is_dir($folder)) {
if (!@mkdir($folder, 0755, true)) {
return 3; // PATH CAN NOT BE CREATED
}
}
$copy_options = array('folderPermission' => 0755, 'filePermission' => 0644);
osc_copy($tmpName . '_normal', $folder . $resourceId . '.' . $extension, $copy_options);
osc_copy($tmpName . '_preview', $folder . $resourceId . '_preview.' . $extension, $copy_options);
osc_copy($tmpName . '_thumbnail', $folder . $resourceId . '_thumbnail.' . $extension, $copy_options);
if (osc_keep_original_image()) {
$path = $folder . $resourceId . '_original.' . $extension;
osc_copy($tmpName, $path, $copy_options);
}
@unlink($tmpName . '_normal');
@unlink($tmpName . '_preview');
@unlink($tmpName . '_thumbnail');
@unlink($tmpName);
$s_path = str_replace(osc_base_path(), '', $folder);
$itemResourceManager->update(
array(
's_path' => $s_path,
's_name' => osc_genRandomPassword(),
's_extension' => $extension,
's_content_type' => $mime,
'i_order' => $order
),
array(
'pk_i_id' => $resourceId,
'fk_i_item_id' => $itemId
)
);
osc_run_hook('uploaded_file', ItemResource::newInstance()->findByPrimaryKey($resourceId));
}
} else {
// images per item limit reached
$limit_reached = true;
}
}
if($limit_reached) {
osc_add_flash_warning_message(_m('Image limit reached, some images were not uploaded!'));
}
unset($itemResourceManager);
}
return 0; // NO PROBLEMS
}