public function render() { // Let's make sure they set an upload destination if (array_get($this->field_config, 'destination', false) === false) { throw new Exception("You need to set a destination for your File field."); } if ($this->field_data) { $file = Path::toAsset($this->field_data); $html = "<div class='file-field-container file-exists'>"; $html .= "<div>"; if (File::isImage(Path::fromAsset($this->field_data, true))) { $html .= "<img src='{$file}' height='58'>"; } $html .= "<p>" . basename($this->field_data) . "</p>"; $html .= "<a class='btn btn-small btn-remove-file' href='#'>" . Localization::fetch('remove') . "</a>"; $html .= "</div>"; $html .= $this->makeFileSelect($file); } else { $html = "<div class='file-field-container'>"; $html .= "<div class='upload-file'>"; $html .= "<p><input type='file' name='{$this->fieldname}' tabindex='{$this->tabindex}' value='' /></p>"; $html .= $this->makeFileSelect(); $html .= "</div>"; } $html .= "</div>"; return $html; }
public function render() { $html = "<div class='file-field-container'>"; if ($this->field_data) { $html .= "<div class='file-exists'>"; if (File::isImage(Path::fromAsset($this->field_data, true))) { $html .= "<img src='" . Path::toAsset($this->field_data) . "' height='58'>"; } $html .= "<p>" . basename($this->field_data) . "</p>"; $html .= "<a class='btn btn-small btn-remove-file' href='#'>" . Localization::fetch('remove') . "</a>"; $html .= "<input type='hidden' name='{$this->fieldname}' value='{$this->field_data}' />"; $html .= "</div>"; } else { $html .= "<div class='upload-file'>"; $html .= "<p><input type='file' name='{$this->fieldname}' tabindex='{$this->tabindex}' value='' /></p>"; $html .= "</div>"; } $html .= "</div>"; return $html; }
public function render() { $html = "<div class='file-field-container'>"; if ($this->field_data) { $html .= "<div class='file-exists'>"; if (File::isImage(BASE_PATH . $this->field_data)) { $html .= "<img src='{$this->field_data}' height='58'>"; } $html .= "<p>" . basename($this->field_data) . "</p>"; $html .= "<a class='btn btn-small btn-remove-file' href='#'>Remove</a>"; $html .= "<input type='hidden' name='{$this->fieldname}' value='{$this->field_data}' />"; $html .= "</div>"; } else { $html .= "<div class='upload-file'>"; $html .= "<p><input type='file' name='{$this->fieldname}' tabindex='{$this->tabindex}' value='' /></p>"; $html .= "</div>"; } $html .= "</div>"; return $html; }
public function index() { /* |-------------------------------------------------------------------------- | Check for image |-------------------------------------------------------------------------- | | Transform just needs the path to an image to get started. If it exists, | the fun begins. | */ $image_src = $this->fetchParam('src', null, false, false, false); // Set full system path $image_path = Path::tidy(BASE_PATH . '/' . $image_src); // Check if image exists before doing anything. if (!File::isImage($image_path)) { Log::error("Could not find requested image to transform: " . $image_path, "core", "Transform"); return; } /* |-------------------------------------------------------------------------- | Resizing and cropping options |-------------------------------------------------------------------------- | | The first transformations we want to run is for size to reduce the | memory usage for future effects. | */ $width = $this->fetchParam('width', null, 'is_numeric'); $height = $this->fetchParam('height', null, 'is_numeric'); // resize specific $ratio = $this->fetchParam('ratio', true, false, true); $upsize = $this->fetchParam('upsize', true, false, true); // crop specific $pos_x = $this->fetchParam('pos_x', 0, 'is_numeric'); $pos_y = $this->fetchParam('pos_y', 0, 'is_numeric'); $quality = $this->fetchParam('quality', '75', 'is_numeric'); /* |-------------------------------------------------------------------------- | Action |-------------------------------------------------------------------------- | | Available actions: resize, crop, and guess. | | "Guess" will find the best fitting aspect ratio of your given width and | height on the current image automatically, cut it out and resize it to | the given dimension. | */ $action = $this->fetchParam('action', 'resize'); /* |-------------------------------------------------------------------------- | Extra bits |-------------------------------------------------------------------------- | | Delicious and probably rarely used options. | */ $angle = $this->fetchParam('rotate', false); $flip_side = $this->fetchParam('flip', false); $blur = $this->fetchParam('blur', false, 'is_numeric'); $pixelate = $this->fetchParam('pixelate', false, 'is_numeric'); $grayscale = $this->fetchParam('grayscale', false, false, true); // Silly brits $greyscale = $this->fetchParam('greyscale', $grayscale, false, true); /* |-------------------------------------------------------------------------- | Assemble filename and check for duplicate |-------------------------------------------------------------------------- | | We need to make sure we don't already have this image created, so we | defer any action until we've processed the parameters, which create | a unique filename. | */ // Find .jpg, .png, etc $extension = File::getExtension($image_path); // Filename with the extension removed so we can append our unique filename flags $stripped_image_path = str_replace('.' . $extension, '', $image_path); // The possible filename flags $parameter_flags = array('width' => $width, 'height' => $height, 'quality' => $quality, 'rotate' => $angle, 'flip' => $flip_side, 'pos_x' => $pos_x, 'pos_y' => $pos_y, 'blur' => $blur, 'pixelate' => $pixelate, 'greyscale' => $greyscale); // Start with a 1 character action flag $file_breadcrumbs = '-' . $action[0]; foreach ($parameter_flags as $param => $value) { if ($value) { $flag = is_bool($value) ? '' : $value; // don't show boolean flags $file_breadcrumbs .= '-' . $param[0] . $flag; } } // Allow converting filetypes (jpg, png, gif) $extension = $this->fetchParam('type', $extension); // Allow saving in a different directory $destination = $this->fetchParam('destination', Config::get('transform_destination', false), false, false, false); if ($destination) { $destination = Path::tidy(BASE_PATH . '/' . $destination); // Method checks to see if folder exists before creating it Folder::make($destination); $stripped_image_path = Path::tidy($destination . '/' . basename($stripped_image_path)); } // Reassembled filename with all flags filtered and delimited $new_image_path = $stripped_image_path . $file_breadcrumbs . '.' . $extension; // Check if we've already built this image before if (File::exists($new_image_path)) { return File::cleanURL($new_image_path); } /* |-------------------------------------------------------------------------- | Create Image |-------------------------------------------------------------------------- | | Transform just needs the path to an image to get started. The image is | created in memory so we can start manipulating it. | */ $image = Image::make($image_path); /* |-------------------------------------------------------------------------- | Perform Actions |-------------------------------------------------------------------------- | | This is fresh transformation. Time to work the magic! | */ if ($action === 'resize' && ($width || $height)) { $image->resize($width, $height, $ratio, $upsize); } if ($action === 'crop' && $width && $height) { $image->crop($width, $height, $pos_x, $pos_y); } if ($action === 'smart') { $image->grab($width, $height); } if ($angle) { $image->rotate($angle); } if ($flip_side === 'h' || $flip_side === 'v') { $image->flip($flip_side); } if ($greyscale) { $image->greyscale(); } if ($blur) { $image->blur($blur); } if ($pixelate) { $image->pixelate($pixelate); } /* |-------------------------------------------------------------------------- | Save |-------------------------------------------------------------------------- | | Get out of dodge! | */ $image->save($new_image_path, $quality); return File::cleanURL($new_image_path); }
public function index() { /* |-------------------------------------------------------------------------- | Check for image |-------------------------------------------------------------------------- | | Transform just needs the path to an image to get started. If it exists, | the fun begins. | | The way to do this changes depending on whether its an internal or | external file. | */ $image_src = $this->fetchParam('src', null, false, false, false); // External URL if ($is_external = URL::isExternalUrl($image_src)) { $image_path = $image_src; // Check if file is an image before doing anything. // @TODO: Maybe check that the file exists. $img_info = pathinfo($image_src); $is_image = in_array($img_info['extension'], array('jpg', 'jpeg', 'png', 'gif')); if ( ! $is_image) { Log::error("Requested file is not an image: " . $image_path, "core", "Transform"); return; } } // Internal URL else { // Set full system path $image_path = Path::standardize(Path::fromAsset($image_src)); // Check if image exists before doing anything. if ( ! File::isImage($image_path)) { Log::error("Could not find requested image to transform: " . $image_path, "core", "Transform"); return; } } /* |-------------------------------------------------------------------------- | Resizing and cropping options |-------------------------------------------------------------------------- | | The first transformations we want to run is for size to reduce the | memory usage for future effects. | */ $width = $this->fetchParam('width', null, 'is_numeric'); $height = $this->fetchParam('height', null, 'is_numeric'); // resize specific $ratio = $this->fetchParam('ratio', true, false, true); $upsize = $this->fetchParam('upsize', true, false, true); // crop specific $pos_x = $this->fetchParam('pos_x', 0, 'is_numeric'); $pos_y = $this->fetchParam('pos_y', 0, 'is_numeric'); $quality = $this->fetchParam('quality', '75', 'is_numeric'); /* |-------------------------------------------------------------------------- | Action |-------------------------------------------------------------------------- | | Available actions: resize, crop, and guess. | | "Guess" will find the best fitting aspect ratio of your given width and | height on the current image automatically, cut it out and resize it to | the given dimension. | */ $action = $this->fetchParam('action', 'resize'); /* |-------------------------------------------------------------------------- | Extra bits |-------------------------------------------------------------------------- | | Delicious and probably rarely used options. | */ $angle = $this->fetchParam('rotate', false); $flip_side = $this->fetchParam('flip' , false); $blur = $this->fetchParam('blur', false, 'is_numeric'); $pixelate = $this->fetchParam('pixelate', false, 'is_numeric'); $greyscale = $this->fetchParam(array('greyscale', 'grayscale'), false, false, true); $watermark = $this->fetchParam('watermark', false, false, false, false); /* |-------------------------------------------------------------------------- | Assemble filename and check for duplicate |-------------------------------------------------------------------------- | | We need to make sure we don't already have this image created, so we | defer any action until we've processed the parameters, which create | a unique filename. | */ // Late modified time of original image $last_modified = ($is_external) ? false : File::getLastModified($image_path); // Find .jpg, .png, etc $extension = File::getExtension($image_path); // Filename with the extension removed so we can append our unique filename flags $stripped_image_path = str_replace('.' . $extension, '', $image_path); // The possible filename flags $parameter_flags = array( 'width' => $width, 'height' => $height, 'quality' => $quality, 'rotate' => $angle, 'flip' => $flip_side, 'pos_x' => $pos_x, 'pos_y' => $pos_y, 'blur' => $blur, 'pixelate' => $pixelate, 'greyscale' => $greyscale, 'modified' => $last_modified ); // Start with a 1 character action flag $file_breadcrumbs = '-'.$action[0]; foreach ($parameter_flags as $param => $value) { if ($value) { $flag = is_bool($value) ? '' : $value; // don't show boolean flags $file_breadcrumbs .= '-' . $param[0] . $flag; } } // Allow converting filetypes (jpg, png, gif) $extension = $this->fetchParam('type', $extension); // Allow saving in a different directory $destination = $this->fetchParam('destination', Config::get('transform_destination', false), false, false, false); if ($destination) { $destination = Path::tidy(BASE_PATH . '/' . $destination); // Method checks to see if folder exists before creating it Folder::make($destination); $stripped_image_path = Path::tidy($destination . '/' . basename($stripped_image_path)); } // Reassembled filename with all flags filtered and delimited $new_image_path = $stripped_image_path . $file_breadcrumbs . '.' . $extension; // Check if we've already built this image before if (File::exists($new_image_path)) { return Path::toAsset($new_image_path); } /* |-------------------------------------------------------------------------- | Create Image |-------------------------------------------------------------------------- | | Transform just needs the path to an image to get started. The image is | created in memory so we can start manipulating it. | */ $image = Image::make($image_path); /* |-------------------------------------------------------------------------- | Perform Actions |-------------------------------------------------------------------------- | | This is fresh transformation. Time to work the magic! | */ if ($action === 'resize' && ($width || $height) ) { $image->resize($width, $height, $ratio, $upsize); } if ($action === 'crop' && $width && $height) { $image->crop($width, $height, $pos_x, $pos_y); } if ($action === 'smart') { $image->grab($width, $height); } $resize = $this->fetchParam('resize', null); if ($resize) { $resize_options = Helper::explodeOptions($resize, true); $image->resize( array_get($resize_options, 'width'), array_get($resize_options, 'height'), array_get($resize_options, 'ratio', true), array_get($resize_options, 'upsize', true) ); } $crop = $this->fetchParam('crop', null); if ($crop) { $crop_options = Helper::explodeOptions($crop, true); $image->crop( array_get($crop_options, 'width'), array_get($crop_options, 'height'), array_get($crop_options, 'x'), array_get($crop_options, 'y') ); } if ($angle) { $image->rotate($angle); } if ($flip_side === 'h' || $flip_side === 'v') { $image->flip($flip_side); } if ($greyscale) { $image->greyscale(); } if ($blur) { $image->blur($blur); } if ($pixelate) { $image->pixelate($pixelate); } // Positioning options via ordered pipe settings: // source|position|x offset|y offset if ($watermark) { $watermark_options = Helper::explodeOptions($watermark); $source = Path::tidy(BASE_PATH . '/' . array_get($watermark_options, 0, null)); $anchor = array_get($watermark_options, 1, null); $pos_x = array_get($watermark_options, 2, 0); $pos_y = array_get($watermark_options, 3, 0); $image->insert($source, $pos_x, $pos_y, $anchor); } /* |-------------------------------------------------------------------------- | Save |-------------------------------------------------------------------------- | | Get out of dodge! | */ try { $image->save($new_image_path, $quality); } catch(Exception $e) { Log::fatal('Could not write new images. Try checking your file permissions.', 'core', 'Transform'); throw new Exception('Could not write new images. Try checking your file permissions.'); } return File::cleanURL($new_image_path); }
public function index() { /* |-------------------------------------------------------------------------- | Paramers |-------------------------------------------------------------------------- | | Match overrides Extension. Exclusion applies in both cases. | */ $match = $this->fetchParam('match', false); $exclude = $this->fetchParam('exclude', false); $extension = $this->fetchParam(array('extension', 'type'), false); $in = $this->fetchParam(array('in', 'folder', 'from'), false); $not_in = $this->fetchParam('not_in', false); $file_size = $this->fetchParam('file_size', false); $file_date = $this->fetchParam('file_date', false); $depth = $this->fetchParam('depth', false); $sort_by = $this->fetchParam(array('sort_by', 'order_by'), false); $sort_dir = $this->fetchParam(array('sort_dir', 'sort_direction'), 'asc'); $limit = $this->fetchParam('limit', false); if ($in) { $in = Helper::explodeOptions($in); } if ($not_in) { $not_in = Helper::explodeOptions($not_in); } if ($file_size) { $file_size = Helper::explodeOptions($file_size); } if ($extension) { $extension = Helper::explodeOptions($extension); } /* |-------------------------------------------------------------------------- | Finder |-------------------------------------------------------------------------- | | Get_Files implements most of the Symfony Finder component as a clean | tag wrapper mapped to matched filenames. | */ $finder = new Finder(); if ($in) { foreach ($in as $location) { $finder->in(Path::fromAsset($location)); } } /* |-------------------------------------------------------------------------- | Name |-------------------------------------------------------------------------- | | Match is the "native" Finder name() method, which is supposed to | implement string, glob, and regex. The glob support is only partial, | so "extension" is a looped *single* glob rule iterator. | */ if ($match) { $finder->name($match); } elseif ($extension) { foreach ($extension as $ext) { $finder->name("*.{$ext}"); } } /* |-------------------------------------------------------------------------- | Exclude |-------------------------------------------------------------------------- | | Exclude directories from matching. Remapped to "not in" to allow more | intuitive differentiation between filename and directory matching. | */ if ($not_in) { foreach ($not_in as $location) { $finder->exclude($location); } } /* |-------------------------------------------------------------------------- | Not Name |-------------------------------------------------------------------------- | | Exclude files matching a given pattern: string, regex, or glob. | By default we don't allow looking for PHP files. Be smart. | */ if ($this->fetchParam('allow_php', false) !== TRUE) { $finder->notName("*.php"); } if ($exclude) { $finder->notName($exclude); } /* |-------------------------------------------------------------------------- | File Size |-------------------------------------------------------------------------- | | Restrict files by size. Can be chained and allows comparison operators. | */ if ($file_size) { foreach ($file_size as $size) { $finder->size($size); } } /* |-------------------------------------------------------------------------- | File Date |-------------------------------------------------------------------------- | | Restrict files by last modified date. Can use comparison operators, and | since/after is aliased to >, and until/before to <. | */ if ($file_date) { $finder->date($file_date); } /* |-------------------------------------------------------------------------- | Depth |-------------------------------------------------------------------------- | | Recursively traverse directories, starting at 0. | */ if ($depth) { $finder->depth($depth); } /* |-------------------------------------------------------------------------- | Sort By |-------------------------------------------------------------------------- | | Sort by name, file, or type | */ if ($sort_by) { if ($sort_by === 'file' || $sort_by === 'name') { $finder->sortByName(); } elseif ($sort_by === 'type') { $finder->sortByType(); } } /* |-------------------------------------------------------------------------- | Assemble File Array |-------------------------------------------------------------------------- | | Select the important bits of data on the list of files. | */ $matches = $finder->files()->followLinks(); $files = array(); foreach ($matches as $file) { $files[] = array('extension' => $file->getExtension(), 'filename' => $file->getFilename(), 'file' => Path::toAsset($file->getPathname()), 'name' => Path::toAsset($file->getPathname()), 'size' => File::getHumanSize($file->getSize()), 'size_bytes' => $file->getSize(), 'size_kilobytes' => number_format($file->getSize() / 1024, 2), 'size_megabytes' => number_format($file->getSize() / 1048576, 2), 'size_gigabytes' => number_format($file->getSize() / 1073741824, 2), 'is_image' => File::isImage($file->getPathname())); } /* |-------------------------------------------------------------------------- | Sort Direction |-------------------------------------------------------------------------- | | Set the sort direction, defaulting to "asc" (ascending) | */ if ($sort_dir === 'desc') { $files = array_reverse($files); } /* |-------------------------------------------------------------------------- | Randomizing |-------------------------------------------------------------------------- | | You can't sort randomly using Symfony finder, so we'll do it manually. | */ if ($sort_by === 'random') { shuffle($files); } /* |-------------------------------------------------------------------------- | Limit Files |-------------------------------------------------------------------------- | | Limit the number of files returned. Needs to be run after sort_dir to | ensure consistency. | */ if ($limit) { $files = array_slice($files, 0, $limit); } return Parse::tagLoop($this->content, $files, true, $this->context); }
/** * @param string $fileId id from $_FILES * @param bool $validateImage makes sure, it's an image * @return int attachment id of the uploaded item */ public static function uploadAttachment($fileId, $validateImage) { if (!function_exists('wp_generate_attachment_metadata')) { require_once ABSPATH . 'wp-admin/includes/image.php'; require_once ABSPATH . 'wp-admin/includes/file.php'; require_once ABSPATH . 'wp-admin/includes/media.php'; } // Check for images, if needed, and return 0 if no image if ($validateImage) { $file = $_FILES[$fileId]; if (!File::isImage($file['name']) || !File::isImageMime($file['type'])) { return 0; } } // Run the update $result = media_handle_upload($fileId, 0); // Check for errors if ($result instanceof WP_Error) { return 0; } return intval($result); }
public function testImageNegativeDetermination() { $file = new File(); $file->setTypeFromPath(__DIR__ . '/../fixtures/fake-image.jpg'); $this->assertFalse($file->isImage(), 'Detect fake image'); }