function resize () { $key = (defined('SECRET_AUTH_KEY') && SECRET_AUTH_KEY != '')?SECRET_AUTH_KEY:DB_PASSWORD; $message = $this->Image->id.','.implode(',',$this->parameters); if ($this->valid != crc32($key.$message)) { header("HTTP/1.1 404 Not Found"); die(''); } require_once(ECART_PATH."/core/model/Image.php"); $Resized = new ImageProcessor($this->Image->retrieve(),$this->Image->width,$this->Image->height); $scaled = $this->Image->scaled($this->width,$this->height,$this->scale); $alpha = ($this->Image->mime == "image/png"); $Resized->scale($scaled['width'],$scaled['height'],$this->scale,$alpha,$this->fill); // Post sharpen if ($this->sharpen !== false) $Resized->UnsharpMask($this->sharpen); $ResizedImage = new ImageAsset(); $ResizedImage->copydata($this->Image,false,array()); $ResizedImage->name = 'cache_'.implode('_',$this->parameters); $ResizedImage->filename = $ResizedImage->name.'_'.$ResizedImage->filename; $ResizedImage->parent = $this->Image->id; $ResizedImage->context = 'image'; $ResizedImage->mime = "image/jpeg"; $ResizedImage->id = false; $ResizedImage->width = $Resized->width; $ResizedImage->height = $Resized->height; foreach ($this->args as $index => $arg) $ResizedImage->settings[$arg] = isset($this->parameters[$index])?intval($this->parameters[$index]):false; $ResizedImage->data = $Resized->imagefile($this->quality); if (empty($ResizedImage->data)) return false; $ResizedImage->size = strlen($ResizedImage->data); $this->Image = $ResizedImage; if ($ResizedImage->store( $ResizedImage->data ) === false) return false; $ResizedImage->save(); }
/** * 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)); }
/** * 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; }
public function resize() { if ($this->valid != ImageAsset::checksum($this->Image->id, $this->parameters)) { header('HTTP/1.1 401 Unauthorized'); die('<h1>Not Authorized</h1>'); } do_action('shopp_imageserver_preprocess', $this->Image, $this->parameters); $Resized = new ImageProcessor($this->Image->retrieve(), $this->Image->width, $this->Image->height); $scaled = $this->Image->scaled($this->width, $this->height, $this->scale); $alpha = 'image/png' == $this->Image->mime; if (-1 == $this->fill) { $alpha = true; } $Resized->scale($scaled['width'], $scaled['height'], $this->scale, $alpha, $this->fill); // Post sharpen if (!$alpha && $this->sharpen > 0) { $Resized->sharpen($this->sharpen); } $ResizedImage = new ImageAsset(); $ResizedImage->copydata($this->Image, false, array()); $ResizedImage->name = 'cache_' . implode('_', $this->parameters); $ResizedImage->filename = $ResizedImage->name . '_' . $ResizedImage->filename; $ResizedImage->parent = $this->Image->id; $ResizedImage->context = 'image'; $ResizedImage->mime = 'image/jpeg'; $ResizedImage->id = false; $ResizedImage->width = $Resized->width(); $ResizedImage->height = $Resized->height(); $ResizedImage->settings = $this->parameters; do_action('shopp_imageserver_processed', $ResizedImage, $this->parameters); $ResizedImage->data = $Resized->imagefile($this->quality); if (empty($ResizedImage->data)) { return false; } $ResizedImage->size = strlen($ResizedImage->data); $this->Image = $ResizedImage; if (false === $ResizedImage->store($ResizedImage->data)) { return false; } $ResizedImage->save(); }