/** * unique - returns true if the the filename is unique, or can be made unique reasonably * * @author John Dillick * @since 1.2 * * @return bool true on success, false on fail **/ public function unique() { $Existing = new ImageAsset(); $Existing->uri = $this->filename; $limit = 100; while ($Existing->found()) { // Rename the filename of the image if it already exists list($name, $ext) = explode(".", $Existing->uri); $_ = explode("-", $name); $last = count($_) - 1; $suffix = $last > 0 ? intval($_[$last]) + 1 : 1; if ($suffix == 1) { $_[] = $suffix; } else { $_[$last] = $suffix; } $Existing->uri = join("-", $_) . '.' . $ext; if (!$limit--) { return false; } } if ($Existing->uri !== $this->filename) { $this->filename = $Existing->uri; } return true; }
/** * AJAX behavior to process uploaded images * * TODO: Find a better place for this code so products & categories can both use it * * @return string JSON encoded result with thumbnail id and src **/ function images () { $context = false; $error = false; if (isset($_FILES['Filedata']['error'])) $error = $_FILES['Filedata']['error']; if ($error) die(json_encode(array("error" => $this->uploadErrors[$error]))); require(ECART_PATH."/core/model/Image.php"); if (isset($_REQUEST['type'])) { $parent = $_REQUEST['parent']; switch (strtolower($_REQUEST['type'])) { case "product": $context = "product"; break; case "category": $context = "category"; break; } } if (!$context) die(json_encode(array("error" => __('The file could not be saved because the server cannot tell whether to attach the asset to a product or a category.','Ecart')))); if (!is_uploaded_file($_FILES['Filedata']['tmp_name'])) die(json_encode(array("error" => __('The file could not be saved because the upload was not found on the server.','Ecart')))); if (!is_readable($_FILES['Filedata']['tmp_name'])) die(json_encode(array("error" => __('The file could not be saved because the web server does not have permission to read the upload from the server\'s temporary directory.','Ecart')))); if ($_FILES['Filedata']['size'] == 0) die(json_encode(array("error" => __('The file could not be saved because the uploaded file is empty.','Ecart')))); // Save the source image if ($context == "category") $Image = new CategoryImage(); else $Image = new ProductImage(); $Image->parent = $parent; $Image->type = "image"; $Image->name = "original"; $Image->filename = $_FILES['Filedata']['name']; list($Image->width, $Image->height, $Image->mime, $Image->attr) = getimagesize($_FILES['Filedata']['tmp_name']); $Image->mime = image_type_to_mime_type($Image->mime); $Image->size = filesize($_FILES['Filedata']['tmp_name']); $Existing = new ImageAsset(); $Existing->uri = $Image->filename; $limit = 100; while ($Existing->found()) { // Rename the filename of the image if it already exists list($name,$ext) = explode(".",$Existing->uri); $_ = explode("-",$name); $last = count($_)-1; $suffix = $last > 0?intval($_[$last])+1:1; if ($suffix == 1) $_[] = $suffix; else $_[$last] = $suffix; $Existing->uri = join("-",$_).'.'.$ext; if (!$limit--) die(json_encode(array("error" => __('The image already exists, but a new filename could not be generated.','Ecart')))); } if ($Existing->uri !== $Image->filename) $Image->filename = $Existing->uri; $Image->store($_FILES['Filedata']['tmp_name'],'upload'); $Image->save(); if (empty($Image->id)) die(json_encode(array("error" => __('The image reference was not saved to the database.','Ecart')))); echo json_encode(array("id"=>$Image->id)); }