public function action_thumb() { if (!preg_match('/^image\\/.*$/i', $this->attachment['mime'])) { $ext = File::ext_by_mime($this->attachment['mime']); if (file_exists(DOCROOT . 'img/icons/' . $ext . '-icon-128x128.png')) { $this->redirect('/img/icons/' . $ext . '-icon-128x128.png'); } else { $this->redirect('http://stdicon.com/' . $this->attachment['mime'] . '?size=96&default=http://stdicon.com/text'); } } if (!file_exists(DOCROOT . 'storage/' . $this->attachment['id'] . '.thumb')) { if (!file_exists(DOCROOT . 'storage/' . $this->attachment['id'])) { $this->redirect('http://stdicon.com/' . $this->attachment['mime'] . '?size=96&default=http://stdicon.com/text'); } $data = file_get_contents(DOCROOT . 'storage/' . $this->attachment['id']); $image = imagecreatefromstring($data); $x = imagesx($image); $y = imagesy($image); $size = max($x, $y); $x = round($x / $size * 96); $y = round($y / $size * 96); $thumb = imagecreatetruecolor($x, $y); imagealphablending($thumb, false); imagesavealpha($thumb, true); imagecopyresampled($thumb, $image, 0, 0, 0, 0, $x, $y, imagesx($image), imagesy($image)); imagepng($thumb, DOCROOT . 'storage/' . $this->attachment['id'] . '.thumb', 9); } header('Content-type: image/png'); header('Content-disposition: filename="thumbnail.png"'); header('Content-length: ' . filesize(DOCROOT . 'storage/' . $this->attachment['id'] . '.thumb')); readfile(DOCROOT . 'storage/' . $this->attachment['id'] . '.thumb'); die; }
/** * Загрузка имаги * @param $company_id * @param $user_id */ private function _upload($company_id, $user) { $validation = Validation::factory($_FILES)->rule('files', 'not_empty'); if ($validation->check()) { $file = (object) array('type' => $validation['files']['type'][0], 'tmp_name' => $validation['files']['tmp_name'][0], 'size' => $validation['files']['size'][0], 'name' => $validation['files']['name'][0], 'error' => $validation['files']['error']); $company = ORM::factory('service', $company_id); $settings = (object) Kohana::$config->load('gallery'); /** * Если файл есть * И папка доступна для записи * И такая компания есть * И ты админ или хозяин компании */ if ($file->size > 0 and is_writable($settings->img_path) and $company->loaded() and ($user->has('roles', 2) or $company->user_id == $user->id)) { // Манипуляция с временным изображением $image = Image::factory($file->tmp_name); // Название изображения $file_name = $company->id . '_' . md5(Date::formatted_time() . $file->name) . '.' . File::ext_by_mime($file->type); if ($image->width > $settings['img_max_width']) { $image->resize($settings['img_max_width'], NULL); } $image->save($settings['img_path'] . $file_name); if ($image->height > $settings['thumb_img_max_width']) { $image->resize(NULL, $settings['thumb_img_max_width']); } $image->save($settings['img_path'] . $settings['thumb_file_name_prefix'] . $file_name); unlink($file->tmp_name); $company_image = ORM::factory('CompanyImage'); $company_image->name = $file->name; $company_image->date_created = Date::formatted_time(); $company_image->img_path = $settings['img_path'] . $file_name; $company_image->thumb_img_path = $settings['img_path'] . $settings['thumb_file_name_prefix'] . $file_name; $company_image->company_id = $company->id; $company_image->title = trim(Arr::path($_POST, 'title')) ? Arr::path($_POST, 'title') : $company_image->name; $company_image->save(); $company->date_edited = Date::formatted_time(); $company->update(); $this->data[] = (object) array('title' => $company_image->title, 'url' => '/' . $company_image->img_path, 'thumbnail_url' => '/' . $company_image->thumb_img_path, 'delete_url' => '/rest/companyimage/index/' . $company_image->id . '?company_id=' . $company->id, 'delete_type' => 'DELETE', 'image_id' => $company_image->id, 'company_id' => $company_image->company->id); $redirect = $this->request->post('redirect') ? stripslashes($this->request->post('redirect')) : null; // Редирект для IE if ($redirect) { $this->request->redirect(sprintf($redirect, rawurlencode(json_encode($this->data)))); } } } }
public function upload_images() { $file_validation = Validation::factory($_FILES)->rule('thumb_img_path', 'Upload::type', array(':value', array('jpg', 'jpeg', 'png', 'gif')))->rule('img_path', 'Upload::type', array(':value', array('jpg', 'jpeg', 'png', 'gif'))); if ($file_validation->check()) { if ($_FILES['img_path']['size'] != 0 and is_writable(self::IMG_PATH)) { $image = Image::factory($_FILES['img_path']['tmp_name']); if ($image->height > 150) { $image->resize(NULL, 150); } if ($image->width > 300) { $image->resize(250, NULL); } if (file_exists($this->img_path)) { unlink($this->img_path); } $file_path = self::IMG_PATH . md5($this->id . '_' . $this->name) . '.' . File::ext_by_mime($image->mime); $image->save($file_path); $this->img_path = $file_path; } if ($_FILES['thumb_img_path']['size'] != 0 and is_writable(self::THUMB_IMG_PATH)) { $image = Image::factory($_FILES['thumb_img_path']['tmp_name']); if ($image->width > 30) { $image->resize(30, NULL); } if ($image->height > 35) { $image->resize(NULL, 35); } if (file_exists($this->thumb_img_path)) { unlink($this->thumb_img_path); } $file_path = self::THUMB_IMG_PATH . md5($this->id . '_' . $this->name) . '_thumb.' . File::ext_by_mime($image->mime); $image->save($file_path); $this->thumb_img_path = $file_path; } $this->update(); } return $file_validation; }
/** * 文件名安全过滤 * * @return Upload */ protected function format_name() { if ($this->filename) { $this->file['name'] = $this->filename; } else { if ($this->config['rename']) { // 重命名文件 $ext = File::ext_by_mime($this->file['type']); // 获取文件后缀 $this->file['name'] = Text::random() . $ext; } else { // 对文件名做特殊字符过滤处理 $this->clean_filename(); if ($this->config['remove_spaces']) { $this->file['name'] = preg_replace('#\\s+#', '_', $this->file['name']); } } } return $this; }
/** * Downloads gravatar to location on server * * [!!] Note: If location is not set, by default use server tmp directory. * * Example: * ~~~ * // get an image specific to a user * $avatar = Gravatar::instance('*****@*****.**'); * * // download gravatar * $result = $avatar->download(); * * // print result * echo __('Gravatar saved to :loc, file size: :len', array( * ':loc' => $result->location, * ':len' => $result->length * )); * ~~~ * * @since 1.4.0 * * @return stdClass * * @throws Gleez_Exception * * @uses File::ext_by_mime */ public function download() { try { $headers = get_headers($this, 1); } catch (ErrorException $e) { if ($e->getCode() === 2) { throw new Gleez_Exception('URL does not seem to exist', array(), 403); } else { throw new Gleez_Exception($e->getMessage(), array(), $e->getCode()); } } // Make sure content type exists if (!isset($headers['Content-Type'])) { throw new Gleez_Exception('Content-Type not found', array(), 300); } // Make sure content type is valid if (!in_array($headers['Content-Type'], $this->getValidTypes())) { throw new Gleez_Exception('Content-Type :type is invalid', array(':type' => $headers['Content-Type']), 305); } // Set file name $filename = $this->getEmailHash() . '.' . File::ext_by_mime($headers['Content-Type']); // Try to download try { file_put_contents($this->getStoreLocation($filename), file_get_contents($this)); } catch (ErrorException $e) { throw new Gleez_Exception('File could not been downloaded: :msg', array(':msg' => $e->getMessage()), 400); } $result = new stdClass(); $result->filename = $filename; $result->extension = File::ext_by_mime($headers['Content-Type']); $result->type = $headers['Content-Type']; $result->length = $headers['Content-Length']; $result->location = $this->getStoreLocation($filename); return $result; }