/** * update_images() * Updates the image details for all cached images */ function update_images ($images) { if (!is_array($images)) return false; foreach ($images as $img) { $Image = new ProductImage($img['id']); $Image->title = $img['title']; $Image->alt = $img['alt']; if (!empty($img['cropping'])) { require_once(ECART_PATH."/core/model/Image.php"); foreach ($img['cropping'] as $id => $cropping) { if (empty($cropping)) continue; $Cropped = new ProductImage($id); list($Cropped->settings['dx'], $Cropped->settings['dy'], $Cropped->settings['cropscale']) = explode(',', $cropping); extract($Cropped->settings); $Resized = new ImageProcessor($Image->retrieve(),$Image->width,$Image->height); $scaled = $Image->scaled($width,$height,$scale); $scale = $Cropped->_scaling[$scale]; $quality = ($quality === false)?$Cropped->_quality:$quality; $Resized->scale($scaled['width'],$scaled['height'],$scale,$alpha,$fill,(int)$dx,(int)$dy,(float)$cropscale); // Post sharpen if ($sharpen !== false) $Resized->UnsharpMask($sharpen); $Cropped->data = $Resized->imagefile($quality); if (empty($Cropped->data)) return false; $Cropped->size = strlen($Cropped->data); if ($Cropped->store( $Cropped->data ) === false) return false; $Cropped->save(); } } $Image->save(); } return true; }
/** * Updates image details for all cached images of the product * * @author Jonathan Davis * @since 1.0 * * @param array image record ids * @return void **/ public function update_images($images) { if (!is_array($images)) { return; } foreach ($images as $img) { $Image = new ProductImage($img['id']); $Image->title = stripslashes($img['title']); $Image->alt = stripslashes($img['alt']); if (!empty($img['cropping'])) { if (!class_exists('ImageProcessor')) { require SHOPP_MODEL_PATH . "/Image.php"; } foreach ($img['cropping'] as $id => $cropping) { if (empty($cropping)) { continue; } $Cropped = new ProductImage($id); list($Cropped->settings['dx'], $Cropped->settings['dy'], $Cropped->settings['cropscale']) = explode(',', $cropping); extract($Cropped->settings); $Resized = new ImageProcessor($Image->retrieve(), $Image->width, $Image->height); $scaled = $Image->scaled($width, $height, $scale); $scale = ImageAsset::$defaults['scaling'][$scale]; $quality = $quality === false ? ImageAsset::$defaults['quality'] : $quality; $Resized->scale($scaled['width'], $scaled['height'], $scale, $alpha, $fill, (int) $dx, (int) $dy, (double) $cropscale); // Post sharpen if ($sharpen !== false) { $Resized->UnsharpMask($sharpen); } $Cropped->data = $Resized->imagefile($quality); if (empty($Cropped->data)) { return false; } $Cropped->size = strlen($Cropped->data); if ($Cropped->store($Cropped->data) === false) { return false; } $Cropped->save(); } } $Image->save(); } }
/** * AJAX behavior to process uploaded images * * @author Jonathan Davis * @return string JSON encoded result with thumbnail id and src **/ public static function images() { $context = false; $error = false; $valid_contexts = array('product', 'category'); if (isset($_FILES['Filedata']['error'])) { $error = $_FILES['Filedata']['error']; } if ($error) { die(json_encode(array('error' => Lookup::errors('uploads', $error)))); } if (isset($_REQUEST['type']) && in_array(strtolower($_REQUEST['type']), $valid_contexts)) { $parent = $_REQUEST['parent']; $context = strtolower($_REQUEST['type']); } if (!$context) { die(json_encode(array('error' => Shopp::__('The file could not be saved because the server cannot tell whether to attach the asset to a product or a category.')))); } if (!@is_uploaded_file($_FILES['Filedata']['tmp_name'])) { die(json_encode(array('error' => Shopp::__('The file could not be saved because the upload was not found on the server.')))); } if (0 == $_FILES['Filedata']['size']) { die(json_encode(array('error' => Shopp::__('The file could not be saved because the uploaded file is empty.')))); } // Save the source image if ('category' == $context) { $Image = new CategoryImage(); } else { $Image = new ProductImage(); } $Image->parent = $parent; $Image->type = 'image'; $Image->name = 'original'; $Image->filename = $_FILES['Filedata']['name']; $context = 'upload'; $tempfile = $_FILES['Filedata']['tmp_name']; if (!@is_readable($tempfile)) { $context = 'file'; $tempfile = get_temp_dir() . $Image->filename; if (!@move_uploaded_file($_FILES['Filedata']['tmp_name'], $tempfile)) { die(json_encode(array('error' => Shopp::__('The file could not be saved because the web server does not have permission to read the upload.')))); } } list($Image->width, $Image->height, $Image->mime, $Image->attr) = getimagesize($tempfile); $Image->mime = image_type_to_mime_type($Image->mime); $Image->size = filesize($tempfile); if (!$Image->unique()) { die(json_encode(array('error' => Shopp::__('The image already exists, but a new filename could not be generated.')))); } $Image->store($tempfile, $context); if ('file' == $context) { unlink($tempfile); } $Error = ShoppErrors()->code('storage_engine_save'); if (!empty($Error)) { die(json_encode(array('error' => $Error->message(true)))); } $Image->save(); if (empty($Image->id)) { die(json_encode(array('error' => Shopp::__('The image reference was not saved to the database.')))); } echo json_encode(array('id' => $Image->id)); exit; }
/** * 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)); }