/** * Attempts to upload a file, adds it to the database and removes other database entries for that file type if they are asked to be removed * @param string $field The name of the $_FILES field you want to upload * @param boolean $upload_type The type of upload ('news', 'gallery', 'section') etc * @param boolean $type_id The ID of the item (above) that the upload is linked to * @param boolean $remove_existing Setting this to true will remove all existing uploads of the passed in type (useful for replacing news article images when a new one is uploaded for example) * @return object Returns the upload object so we can work with the uploaded file and details */ public static function upload($field = 'image', $upload_type = false, $type_id = false, $remove_existing = false) { if (!$field || !$upload_type || !$type_id) { return false; } $input = Input::file($field); if ($input && $input['error'] == UPLOAD_ERR_OK) { if ($remove_existing) { static::remove($upload_type, $type_id); } $ext = File::extension($input['name']); $filename = Str::slug(Input::get('title'), '-'); Input::upload('image', './uploads/' . $filename . '.' . $ext); $upload = new Upload(); $upload->link_type = $upload_type; $upload->link_id = $type_id; $upload->filename = $filename . '.' . $ext; if (Koki::is_image('./uploads/' . $filename . '.' . $ext)) { $upload->small_filename = $filename . '_small' . '.' . $ext; $upload->thumb_filename = $filename . '_thumb' . '.' . $ext; $upload->image = 1; } $upload->extension = $ext; $upload->user_id = Auth::user()->id; $upload->save(); return $upload; } }
/** * Attempts to upload a file, adds it to the database and removes other database entries for that file type if they are asked to be removed * @param string $field The name of the $_FILES field you want to upload * @param boolean $upload_type The type of upload ('news', 'gallery', 'section') etc * @param boolean $type_id The ID of the item (above) that the upload is linked to * @param boolean $remove_existing Setting this to true will remove all existing uploads of the passed in type (useful for replacing news article images when a new on * @param boolean $title Sets the title of the upload * @param boolean $path_to_store Sets the path to the upload (where it should go) * @return object Returns the upload object so we can work with the uploaded file and details */ public static function upload($details = array()) { $upload_details = array('remove_existing_for_link' => true, 'path_to_store' => path('public') . 'uploads/', 'resizing' => array('small' => array('w' => 200, 'h' => 200), 'thumb' => array('w' => 200, 'h' => 200))); if (!empty($details)) { $required_keys = array('upload_field_name', 'upload_type', 'upload_link_id', 'title'); $continue = true; foreach ($required_keys as $key) { if (!isset($details[$key]) || empty($details[$key])) { Messages::add('error', 'Your upload array details are not complete... please fill this in properly.'); $continue = false; } } if ($continue) { $configuration = $details + $upload_details; $input = Input::file($configuration['upload_field_name']); if ($input && $input['error'] == UPLOAD_ERR_OK) { if ($configuration['remove_existing_for_link']) { static::remove($configuration['upload_type'], $configuration['upload_link_id']); } $ext = File::extension($input['name']); $filename = Str::slug($configuration['upload_type'] . '-' . $configuration['upload_link_id'] . '-' . Str::limit(md5($input['name']), 10, false) . '-' . $configuration['title'], '-'); Input::upload($configuration['upload_field_name'], $configuration['path_to_store'], $filename . '.' . $ext); $upload = new Upload(); $upload->link_type = $configuration['upload_type']; $upload->link_id = $configuration['upload_link_id']; $upload->filename = $filename . '.' . $ext; if (Koki::is_image($configuration['path_to_store'] . $filename . '.' . $ext)) { $upload->small_filename = $filename . '_small' . '.' . $ext; $upload->thumb_filename = $filename . '_thumb' . '.' . $ext; $upload->image = 1; } $upload->extension = $ext; $upload->user_id = Auth::user()->id; $upload->save(); if (Koki::is_image($configuration['path_to_store'] . $filename . '.' . $ext)) { WideImage::load($configuration['path_to_store'] . $upload->filename)->resize($configuration['resizing']['small']['w'], $configuration['resizing']['small']['h'])->saveToFile($configuration['path_to_store'] . $upload->small_filename); WideImage::load($configuration['path_to_store'] . $upload->small_filename)->crop('center', 'center', $configuration['resizing']['thumb']['w'], $configuration['resizing']['thumb']['h'])->saveToFile($configuration['path_to_store'] . $upload->thumb_filename); } return true; } } } else { Messages::add('error', 'Your upload array details are empty... please fill this in properly.'); } return false; }
public function upload($field = 'image', $upload_type = false, $type_id = false) { if (!$field || !$upload_type || !$type_id) { return false; } if (Input::file($field)) { $input = Input::file('image'); $ext = File::extension($input['name']); $filename = Str::slug(Input::get('title'), '-'); Input::upload('image', './uploads/' . $filename . '.' . $ext); $upload = new Upload(); $upload->link_type = $upload_type; $upload->link_id = $type_id; $upload->filename = $filename . '.' . $ext; if (Koki::is_image('./uploads/' . $filename . '.' . $ext)) { $upload->small_filename = $filename . '_small' . '.' . $ext; $upload->thumb_filename = $filename . '_thumb' . '.' . $ext; $upload->image = 1; } $upload->extension = $ext; $upload->save(); return $upload; } }