public function resize($filename, $width, $height) { if (!file_exists(DIR_IMAGE . $filename) || !is_file(DIR_IMAGE . $filename)) { return; } $info = pathinfo($filename); $extension = $info['extension']; $old_image = $filename; $new_image = 'cache/' . utf8_substr($filename, 0, utf8_strrpos($filename, '.')) . '-' . $width . 'x' . $height . '.' . $extension; if (!file_exists(DIR_IMAGE . $new_image) || filemtime(DIR_IMAGE . $old_image) > filemtime(DIR_IMAGE . $new_image)) { $path = ''; $directories = explode('/', dirname(str_replace('../', '', $new_image))); foreach ($directories as $directory) { $path = $path . '/' . $directory; if (!file_exists(DIR_IMAGE . $path)) { @mkdir(DIR_IMAGE . $path, 0777); } } $image = new Image(DIR_IMAGE . $old_image); $image->resize($width, $height); $image->save(DIR_IMAGE . $new_image); } if (isset($this->request->server['HTTPS']) && ($this->request->server['HTTPS'] == 'on' || $this->request->server['HTTPS'] == '1')) { return HTTPS_CATALOG . 'image/' . $new_image; } else { return HTTP_CATALOG . 'image/' . $new_image; } }
function admin_save() { if (isset($this->data) && !empty($this->data)) { if (isset($this->data['Text']['id'])) { $screenshot = $this->Screenshot->find('first', array('conditions' => array('Screenshot.id' => $this->data['Screenshot']['id']))); } // resize App::import('Vendor', 'Image', array('file' => 'image.php')); $Image = new Image(); if (empty($this->data['Screenshot']['image']['name'])) { unset($this->data['Screenshot']['image']); } else { $ext = preg_replace('/(.*)(\\.(.*))$/', '\\3', $this->data['Screenshot']['image']['name']); $imageName = md5(uniqid(mt_rand(), true)) . '.' . $ext; move_uploaded_file($this->data['Screenshot']['image']['tmp_name'], WWW_ROOT . 'img' . DS . 'website' . DS . 'screenshots' . DS . 'original' . DS . $imageName); $Image->resize(WWW_ROOT . 'img' . DS . 'website' . DS . 'screenshots' . DS . 'original' . DS . $imageName, WWW_ROOT . 'img' . DS . 'website' . DS . 'screenshots' . DS . 'big' . DS . $imageName, 640, 480); $Image->resize(WWW_ROOT . 'img' . DS . 'website' . DS . 'screenshots' . DS . 'original' . DS . $imageName, WWW_ROOT . 'img' . DS . 'website' . DS . 'screenshots' . DS . 'medium' . DS . $imageName, 350, 350); $Image->resize(WWW_ROOT . 'img' . DS . 'website' . DS . 'screenshots' . DS . 'original' . DS . $imageName, WWW_ROOT . 'img' . DS . 'website' . DS . 'screenshots' . DS . 'small' . DS . $imageName, 100, 100); $this->data['Screenshot']['image'] = $imageName; if (isset($screenshot['Screenshot']['image']) && !empty($screenshot['Screenshot']['image'])) { @unlink(WWW_ROOT . 'img' . DS . 'website' . DS . 'screenshots' . DS . 'original' . DS . $screenshot['Screenshot']['image']); @unlink(WWW_ROOT . 'img' . DS . 'website' . DS . 'screenshots' . DS . 'small' . DS . $screenshot['Screenshot']['image']); @unlink(WWW_ROOT . 'img' . DS . 'website' . DS . 'screenshots' . DS . 'medium' . DS . $screenshot['Screenshot']['image']); @unlink(WWW_ROOT . 'img' . DS . 'website' . DS . 'screenshots' . DS . 'big' . DS . $screenshot['Screenshot']['image']); } } $this->Screenshot->save($this->data); $this->redirect('/admin/screenshots'); } else { $this->redirect('/admin/screenshots'); } }
public function edit($album_url, $photo) { $photo = new Photo_Model($album_url . '/' . $photo); if (!$photo->id) { Event::run('system.404'); } $this->template->content = new View('admin/photo/form'); $this->template->content->errors = ''; $this->template->content->action = 'Edit'; if ($_POST) { try { $photo->set_fields($_POST); if (!$_FILES['photo']['error']) { // Delete the old photo before we change it $photo->delete_file(); $photo->photo_filename = $_FILES['photo']['name']; // Create a thumbnail and resized version $image = new Image($_FILES['photo']['tmp_name']); $image->resize(Kohana::config('photo_gallery.image_width'), Kohana::config('photo_gallery.image_height'), Image::AUTO); $image->save(APPPATH . 'views/media/photos/' . $album_url . '/' . $_FILES['photo']['name']); $image->resize(Kohana::config('photo_gallery.thumbnail_width'), Kohana::config('photo_gallery.thumbnail_height'), Image::AUTO); $image->save(APPPATH . 'views/media/photos/' . $album_url . '/thumb_' . $_FILES['photo']['name']); } $photo->save(); url::redirect('album/view/' . $photo->album->url_name); } catch (Kohana_User_Exception $e) { $this->template->content->errors = $e; } } $this->template->content->photo = $photo; }
function resizeImage() { //lay duong dan file duoc request $request = $_SERVER['REQUEST_URI']; //lay ten file $tmp_request = explode('/', $request); $case = count($tmp_request); $image_name = $tmp_request[$case - 1]; //type resize $resize_type = $tmp_request[$case - 2]; //echo $image_name; $resource_img = get_picture_dir($image_name) . '/' . $image_name; $resource_img = '..' . $resource_img; if (!file_exists($resource_img)) { error_404_document(); } //echo file_get_contents($resource_img);exit(); $images = new Image($resource_img); $imageinfo = $images->getImageInfo(); if ($imageinfo['height'] == null || $imageinfo['height'] == 0) { error_404_document(); } //kich thuoc resize $array_resize = array('large' => array(480, 360), 'medium' => array(240, 180), 'medium2' => array(145, 95), 'small' => array(106, 80), 'thumb' => array(50, 50), 'mobile' => array(640, 400), 'mobile_medium' => array(320, 200), 'mobile_small' => array(120, 75), 'mobile_low' => array(640, 400, 30), 'mobile_medium_low' => array(320, 200, 30), 'mobile_small_low' => array(120, 75, 30)); if (!isset($array_resize[$resize_type])) { error_404_document(); } //image name file no extension $filesavename = explode('.', $image_name); $count3 = count($filesavename); if ($count3 > 1) { unset($filesavename[$count3 - 1]); } $filesavename = implode('.', $filesavename); $pathsave = '..' . get_picture_dir($image_name, $resize_type); //nếu thư mục ảnh không tồn tại thì tạo mới if (!file_exists($pathsave)) { mkdir($pathsave, 0755, 1); } //echo $pathsave;die(); //kich thuoc resize ok -> tao file voi kich thuoc phu hop $r_width = $array_resize[$resize_type][0]; $r_height = $array_resize[$resize_type][1]; if (isset($array_resize[$resize_type][2])) { $r_quality = $array_resize[$resize_type][2]; } else { $r_quality = 100; } if ($resize_type == 'organic') { $images->resize($r_width, $r_height, 'fit', 'c', 'c', $r_quality); } else { $images->resize($r_width, $r_height, 'crop', 'c', 'c', $r_quality); } $images->save($filesavename, $pathsave); header("HTTP/1.0 200 OK"); $images->display(); }
/** * Test * * @return void */ public function testSaveInBmp() { $savingPath = $this->directory . 'saving-test.bmp'; $this->object->open($this->directory . 'test.png'); $this->object->resize(50, 50); $this->assertFalse($this->object->save($savingPath)); }
public function resize($filename, $width, $height) { if (!file_exists(DIR_IMAGE . $filename) || !is_file(DIR_IMAGE . $filename)) { return; } $info = pathinfo($filename); $extension = $info['extension']; $old_image = $filename; $new_image = 'cache/' . utf8_substr($filename, 0, strrpos($filename, '.')) . '-' . $width . 'x' . $height . '.' . $extension; if (!file_exists(DIR_IMAGE . $new_image) || filemtime(DIR_IMAGE . $old_image) > filemtime(DIR_IMAGE . $new_image)) { $path = ''; $directories = explode('/', dirname(str_replace('../', '', $new_image))); foreach ($directories as $directory) { $path = $path . '/' . $directory; if (!file_exists(DIR_IMAGE . $path)) { @mkdir(DIR_IMAGE . $path, 0777); } } try { $image = new Image(DIR_IMAGE . $old_image); } catch (Exception $exc) { $this->log->write("The file {$old_image} has wrong format and can not be handled."); $image = new Image(DIR_IMAGE . 'no_image.jpg'); } $image->resize($width, $height); $image->save(DIR_IMAGE . $new_image); } if (isset($this->request->server['HTTPS']) && ($this->request->server['HTTPS'] == 'on' || $this->request->server['HTTPS'] == '1')) { return HTTPS_IMAGE . $new_image; } else { return HTTP_IMAGE . $new_image; } }
public function resize($filename, $width, $height) { if (!is_file(DIR_IMAGE . $filename) || substr(str_replace('\\', '/', realpath(DIR_IMAGE . $filename)), 0, strlen(DIR_IMAGE)) != DIR_IMAGE) { return; } $extension = pathinfo($filename, PATHINFO_EXTENSION); $image_old = $filename; $image_new = 'cache/' . utf8_substr($filename, 0, utf8_strrpos($filename, '.')) . '-' . $width . 'x' . $height . '.' . $extension; if (!is_file(DIR_IMAGE . $image_new) || filectime(DIR_IMAGE . $image_old) > filectime(DIR_IMAGE . $image_new)) { list($width_orig, $height_orig, $image_type) = getimagesize(DIR_IMAGE . $image_old); if (!in_array($image_type, array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF))) { return DIR_IMAGE . $image_old; } $path = ''; $directories = explode('/', dirname($image_new)); foreach ($directories as $directory) { $path = $path . '/' . $directory; if (!is_dir(DIR_IMAGE . $path)) { @mkdir(DIR_IMAGE . $path, 0777); } } if ($width_orig != $width || $height_orig != $height) { $image = new Image(DIR_IMAGE . $image_old); $image->resize($width, $height); $image->save(DIR_IMAGE . $image_new); } else { copy(DIR_IMAGE . $image_old, DIR_IMAGE . $image_new); } } if ($this->request->server['HTTPS']) { return HTTPS_CATALOG . 'image/' . $image_new; } else { return HTTP_CATALOG . 'image/' . $image_new; } }
function resize($path, $opt) { $f3 = \Base::instance(); $hash = $f3->hash($path . $f3->serialize($opt)); $new_file_name = $hash . '.jpg'; $dst_path = $f3->get('UPLOADS') . 'img/'; $path = explode('/', $path); $file = array_pop($path); if (!is_dir($dst_path)) { mkdir($dst_path, 0775); } if (!file_exists($dst_path . $new_file_name)) { $imgObj = new \Image($file, false, implode('/', $path) . '/'); $ow = $imgObj->width(); $oh = $imgObj->height(); if (!$opt['width']) { $opt['width'] = round($opt['height'] / $oh * $ow); } if (!$opt['height']) { $opt['height'] = round($opt['width'] / $ow * $oh); } $imgObj->resize((int) $opt['width'], (int) $opt['height'], $opt['crop'], $opt['enlarge']); $file_data = $imgObj->dump('jpeg', null, $opt['quality']); $f3->write($dst_path . $new_file_name, $file_data); } return $dst_path . $new_file_name; }
/** * * @param filename string * @param width * @param height * @param type char [default, w, h] * default = scale with white space, * w = fill according to width, * h = fill according to height * */ public function resize($filename, $width, $height, $type = "") { if (!file_exists(DIR_IMAGE . $filename) || !is_file(DIR_IMAGE . $filename)) { return; } $info = pathinfo($filename); $extension = $info['extension']; $old_image = $filename; $new_image = 'cache/' . utf8_substr($filename, 0, utf8_strrpos($filename, '.')) . '-' . $width . 'x' . $height . $type . '.' . $extension; if (!file_exists(DIR_IMAGE . $new_image) || filemtime(DIR_IMAGE . $old_image) > filemtime(DIR_IMAGE . $new_image)) { $path = ''; $directories = explode('/', dirname(str_replace('../', '', $new_image))); foreach ($directories as $directory) { $path = $path . '/' . $directory; if (!file_exists(DIR_IMAGE . $path)) { @mkdir(DIR_IMAGE . $path, 0777); } } list($width_orig, $height_orig) = getimagesize(DIR_IMAGE . $old_image); if ($width_orig != $width || $height_orig != $height) { $image = new Image(DIR_IMAGE . $old_image); $image->resize($width, $height, $type); $image->save(DIR_IMAGE . $new_image); } else { copy(DIR_IMAGE . $old_image, DIR_IMAGE . $new_image); } } if (isset($this->request->server['HTTPS']) && ($this->request->server['HTTPS'] == 'on' || $this->request->server['HTTPS'] == '1')) { return (defined('HTTPS_STATIC_CDN') ? HTTPS_STATIC_CDN : $this->config->get('config_ssl')) . 'image/' . $new_image; } else { return (defined('HTTP_STATIC_CDN') ? HTTP_STATIC_CDN : $this->config->get('config_url')) . 'image/' . $new_image; } }
static function savePhoto($files, $thumbnail = [], $column = 'photo') { $root = f3()->get('ROOT') . f3()->get('BASE'); $path = "/upload/img/" . date("Y/m") . "/"; if ($files[$column]['size'] >= f3()->get('maxsize') || $files[$column]['size'] == 0) { Reaction::_return("2002", "File too large. File must be less than 2 megabytes."); } if (!in_array($files[$column]['type'], f3()->get('photo_acceptable')) && !empty($files["photo"]["type"])) { Reaction::_return("2003", 'Invalid file type. Only JPG, GIF and PNG types are accepted.'); } if ($files[$column]['error'] != 0) { Reaction::_return("2004", 'other error.'); } if (!file_exists($root . $path)) { mkdir($root . $path, 0777, true); } $path_parts = pathinfo($files[$column]['name']); $old_fn = $path_parts['filename']; $ext = $path_parts['extension']; $filename = $path . substr(md5(uniqid(microtime(), 1)), 0, 15); if (move_uploaded_file($files[$column]['tmp_name'], $root . $filename . "." . $ext)) { list($width, $height, $type, $attr) = getimagesize($root . $filename . "." . $ext); $img = new \Image($filename . "." . $ext, false, $root); foreach ($thumbnail as $ns) { $img->resize($ns[0], $ns[1], true); file_put_contents($root . $filename . "_" . $ns[0] . "x" . $ns[1] . "." . $ext, $img->dump()); } $new_fn = $filename . "." . $ext; } else { $new_fn = ''; $width = 0; $height = 0; } return array($new_fn, $width, $height, $old_fn); }
/** * Image caching * * Resize & cache image into the file system. Returns the template image if product image is not exists * At this time supports JPG images only * * @param mixed $name * @param int $user_id * @param int $width Resizing width * @param int $height Resizing height * @param bool $watermarked * @param bool $overwrite * @return string Cached Image URL */ public function image($name, $user_id, $width, $height, $watermarked = false, $overwrite = false) { $storage = DIR_STORAGE . $user_id . DIR_SEPARATOR . $name . '.' . ALLOWED_IMAGE_EXTENSION; $cache = DIR_IMAGE . 'cache' . DIR_SEPARATOR . $user_id . DIR_SEPARATOR . $name . '-' . $width . '-' . $height . '.' . ALLOWED_IMAGE_EXTENSION; $watermark = DIR_IMAGE . 'common' . DIR_SEPARATOR . 'watermark.png'; $cached_url = ($this->_request->getHttps() ? HTTPS_IMAGE_SERVER : HTTP_IMAGE_SERVER) . 'cache' . DIR_SEPARATOR . $user_id . DIR_SEPARATOR . $name . '-' . $width . '-' . $height . '.' . ALLOWED_IMAGE_EXTENSION; // Force reset if ($overwrite) { unlink($cache); } // If image is cached if (file_exists($cache)) { return $cached_url; // If image not cached } else { // Create directories by path if not exists $directories = explode(DIR_SEPARATOR, $cache); $path = ''; foreach ($directories as $directory) { $path .= DIR_SEPARATOR . $directory; if (!is_dir($path) && false === strpos($directory, '.')) { mkdir($path, 0755); } } // Prepare new image $image = new Image($storage); $image->resize($width, $height); if ($watermarked) { $image->watermark($watermark); } $image->save($cache); } return $cached_url; }
public function resize($filename, $width, $height) { if (!is_file(DIR_IMAGE . $filename) || substr(str_replace('\\', '/', realpath(DIR_IMAGE . $filename)), 0, strlen(DIR_IMAGE)) != DIR_IMAGE) { return; } $extension = pathinfo($filename, PATHINFO_EXTENSION); $old_image = $filename; $new_image = 'cache/' . utf8_substr($filename, 0, utf8_strrpos($filename, '.')) . '-' . (int) $width . 'x' . (int) $height . '.' . $extension; if (!is_file(DIR_IMAGE . $new_image) || filectime(DIR_IMAGE . $old_image) > filectime(DIR_IMAGE . $new_image)) { $path = ''; $directories = explode('/', dirname($new_image)); foreach ($directories as $directory) { $path = $path . '/' . $directory; if (!is_dir(DIR_IMAGE . $path)) { @mkdir(DIR_IMAGE . $path, 0777); } } list($width_orig, $height_orig) = getimagesize(DIR_IMAGE . $old_image); if ($width_orig != $width || $height_orig != $height) { $image = new Image(DIR_IMAGE . $old_image); $image->resize($width, $height); $image->save(DIR_IMAGE . $new_image); } else { copy(DIR_IMAGE . $old_image, DIR_IMAGE . $new_image); } } $new_image = str_replace(' ', '%20', $new_image); if (isset($this->request->server['HTTPS']) && ($this->request->server['HTTPS'] == 'on' || $this->request->server['HTTPS'] == '1') || $this->request->server['HTTPS'] == '443') { return $this->config->get('config_ssl') . 'image/' . $new_image; } elseif (isset($this->request->server['HTTP_X_FORWARDED_PROTO']) && $this->request->server['HTTP_X_FORWARDED_PROTO'] == 'https') { return $this->config->get('config_ssl') . 'image/' . $new_image; } else { return $this->config->get('config_url') . 'image/' . $new_image; } }
public static function resize($filename, $width = 0, $height = 0) { if (!file_exists(Yii::app()->params['imagePath'] . $filename) || !is_file(Yii::app()->params['imagePath'] . $filename)) { return; } $info = pathinfo($filename); $extension = $info['extension']; $old_image = $filename; $new_image = substr($filename, 0, strrpos($filename, '.')) . '-' . $width . 'x' . $height . '.' . $extension; $cache_dir = Yii::app()->assetManager->getBasePath() . "/cache/"; if (!file_exists($cache_dir . $new_image) || filemtime(Yii::app()->params['imagePath'] . $old_image) > filemtime($cache_dir . $new_image)) { $path = ''; $directories = explode('/', dirname(str_replace('../', '', $new_image))); foreach ($directories as $directory) { $path = $path . '/' . $directory; if (!file_exists($cache_dir . $path)) { @mkdir($cache_dir . $path, 0777); } } $image = new Image(Yii::app()->params['imagePath'] . $old_image); $image->resize($width, $height); $image->save($cache_dir . $new_image); } return Yii::app()->assetManager->getBaseUrl() . '/cache/' . $new_image; }
/** * Upload image */ public function uploadImage() { $upload_path = Filesystem::makeDir(UPLOADS . DS . 'images' . DS . 'pages' . DS . date('Y/m/d/')); $filter = new Form_Filter_MachineName(); if (!empty($_POST['editor_file_link'])) { $file = $upload_path . DS . $filter->value(basename($_POST['editor_file_link'])); $info = Image::getInfo($file); copy($_POST['editor_file_link'], $file); } else { $image = new Upload_Image('file', array('path' => $upload_path)); if ($image->upload()) { $info = $image->getInfo(); $file = $image->file->path; //Ajax::json(array('succes'=>TRUE,'data'=>HTML::img(Url::toUri($image->file->path),$_POST['editor_file_alt'],array('width'=>$info->width,'height'=>$info->height)))); } //Ajax::json(array('success'=>FALSE,'data'=>implode("\n",$image->errors))); } if (isset($file)) { if ($max = config('pages.image.max', '600x600')) { $size = explode('x', $max); sizeof($size) == 1 && ($size[1] = $size[0]); if ($info->width > $size[0] or $info->height > $size[1]) { $image = new Image($file); $image->resize($max); $image->save(); } } exit(Url::toUri($file)); } exit; }
public function generate_thumb() { $image = new Image($this->get_image_path()); $image->resize(200, 200); $image->save($this->get_thumb_path()); return TRUE; }
function resize($filename, $width, $height) { $filename = trim($filename, '/'); if (!is_file(IMAGEPATH . $filename)) { return; } $extension = pathinfo($filename, PATHINFO_EXTENSION); $old_image = $filename; $new_image = 'cache/' . substr($filename, 0, strrpos($filename, '.')) . '-' . $width . 'x' . $height . '.' . $extension; if (!is_file(IMAGEPATH . $new_image) || filectime(IMAGEPATH . $old_image) > filectime(IMAGEPATH . $new_image)) { $path = ''; $directories = explode('/', dirname(str_replace('../', '', $new_image))); foreach ($directories as $directory) { $path = $path . '/' . $directory; if (!is_dir(IMAGEPATH . $path)) { @mkdir(IMAGEPATH . $path, 0777); } } list($width_orig, $height_orig) = getimagesize(IMAGEPATH . $old_image); if ($width_orig != $width || $height_orig != $height) { $image = new Image(IMAGEPATH . $old_image); $image->resize($width, $height); $image->save(IMAGEPATH . $new_image); } else { copy(IMAGEPATH . $old_image, IMAGEPATH . $new_image); } } return IMAGEPATH . $new_image; }
public function resize($filename, $width, $height) { if (!is_file(DIR_IMAGE . $filename)) { return; } $extension = pathinfo($filename, PATHINFO_EXTENSION); $old_image = $filename; $new_image = 'cache/' . utf8_substr($filename, 0, utf8_strrpos($filename, '.')) . '-' . $width . 'x' . $height . '.' . $extension; if (!is_file(DIR_IMAGE . $new_image) || filectime(DIR_IMAGE . $old_image) > filectime(DIR_IMAGE . $new_image)) { $path = ''; $directories = explode('/', dirname(str_replace('../', '', $new_image))); foreach ($directories as $directory) { $path = $path . '/' . $directory; if (!is_dir(DIR_IMAGE . $path)) { @mkdir(DIR_IMAGE . $path, 0777); } } list($width_orig, $height_orig) = getimagesize(DIR_IMAGE . $old_image); if ($width_orig != $width || $height_orig != $height) { $image = new Image(DIR_IMAGE . $old_image); $image->resize($width, $height); $image->save(DIR_IMAGE . $new_image); } else { copy(DIR_IMAGE . $old_image, DIR_IMAGE . $new_image); } } if ($this->request->server['HTTPS']) { return $this->config->get('config_ssl') . 'image/' . $new_image; } else { return $this->config->get('config_url') . 'image/' . $new_image; } }
/** * * @param filename string * @param width * @param height * @param type char [default, w, h] * default = scale with white space, * w = fill according to width, * h = fill according to height * */ public function resize($filename, $width, $height, $type = "") { if (!file_exists(DIR_IMAGE . $filename) || !is_file(DIR_IMAGE . $filename)) { return; } $info = pathinfo($filename); $extension = $info['extension']; $old_image = $filename; $new_image = 'cache/' . utf8_substr($filename, 0, utf8_strrpos($filename, '.')) . '-' . $width . 'x' . $height . $type . '.' . $extension; if (!file_exists(DIR_IMAGE . $new_image) || filemtime(DIR_IMAGE . $old_image) > filemtime(DIR_IMAGE . $new_image)) { $path = ''; $directories = explode('/', dirname(str_replace('../', '', $new_image))); foreach ($directories as $directory) { $path = $path . '/' . $directory; if (!file_exists(DIR_IMAGE . $path)) { @mkdir(DIR_IMAGE . $path, 0777); } } list($width_orig, $height_orig) = getimagesize(DIR_IMAGE . $old_image); if ($width_orig != $width || $height_orig != $height) { $image = new Image(DIR_IMAGE . $old_image); $image->resize($width, $height, $type); $image->save(DIR_IMAGE . $new_image); } else { copy(DIR_IMAGE . $old_image, DIR_IMAGE . $new_image); } } return $this->getImageUrl($new_image); }
public static function upload($tmp, $inmueble, $foto) { $root = "upload/{$inmueble}"; if (!is_dir($root)) { mkdir($root, 0777); chmod($root, 0777); } $imagen = new Image($tmp); foreach (Inmuebles_Fotos::$sizes as $folder => $size) { if (!is_dir("{$root}/{$folder}")) { mkdir("{$root}/{$folder}", 0777); chmod("{$root}/{$folder}", 0777); } if (true === $size) { $imagen->clear(); $imagen->save("{$root}/{$folder}/{$foto}.jpg", Image::FORMAT_JPG); continue; } $width = array_shift($size); $height = array_shift($size); $imagen->clear(); $imagen->resize($width, $height); $imagen->save("{$root}/{$folder}/{$foto}.jpg", Image::FORMAT_JPG); } return true; }
function admin_save() { if (isset($this->data) && !empty($this->data)) { if (isset($this->data['Promotion']['id'])) { $promotion = $this->Promotion->find('first', array('conditions' => array('Promotion.id' => $this->data['Promotion']['id']))); } // resize App::import('Vendor', 'Image', array('file' => 'image.php')); $Image = new Image(); if (empty($this->data['Promotion']['image']['name'])) { unset($this->data['Promotion']['image']); } else { $ext = preg_replace('/(.*)(\\.(.*))$/', '\\3', $this->data['Promotion']['image']['name']); $imageName = md5(uniqid(mt_rand(), true)) . '.' . $ext; move_uploaded_file($this->data['Promotion']['image']['tmp_name'], WWW_ROOT . 'img' . DS . 'website' . DS . 'promotions' . DS . $imageName); $Image->resize(WWW_ROOT . 'img' . DS . 'website' . DS . 'promotions' . DS . $imageName, WWW_ROOT . 'img' . DS . 'website' . DS . 'promotions' . DS . $imageName, 320, 160); $this->data['Promotion']['image'] = $imageName; if (isset($text['Promotion']['image']) && !empty($text['Promotion']['image'])) { @unlink(WWW_ROOT . 'img' . DS . 'website' . DS . 'promotions' . DS . $text['Promotion']['image']); } } $this->Promotion->save($this->data); } $this->redirect('/admin/promotions'); }
protected function afterSave() { parent::afterSave(); require_once 'Image.php'; // 保存图片 if ($this->serverFile instanceof CUploadedFile && $this->hasErrors('serverFile') == false) { // 保存原文件 $file = $this->serverFile; $fileName = md5($file->tempName . uniqid()) . '.' . $file->extensionName; $filePath = Helper::mediaPath(self::UPLOAD_ORIGINAL_FILE_PATH . $fileName, FRONTEND); $file->saveAs($filePath); // 如果是图片需要进行裁切 if (strtolower($file->extensionName) != 'swf') { $image = new Image($filePath); $image->resize(350, 230)->save(Helper::mediaPath(self::UPLOAD_THUMBNAIL_IMAGE_PATH . $fileName, FRONTEND)); } // 更新数据 $this->updateByPk($this->primaryKey, array('image_path' => $fileName)); } else { if ($this->deleteServerFile) { // 删除图片 @unlink(Helper::mediaPath(self::UPLOAD_ORIGINAL_FILE_PATH . $this->image_path, FRONTEND)); // 更新数据 $this->updateByPk($this->primaryKey, array('image_path' => '')); } } }
public function makeThumbnail($id, $field, $w, $h, $crop = false, $enlarge = false) { $path = $this->getImagePath($id, $field); // \helpers\Debug::prePrintR($path); if (!empty($path)) { $tn = ($crop ? 'cr' : '') . ($enlarge ? 'en' : ''); $cache = str_replace($this->tableMeta->get('table'), $this->tableMeta->get('table') . '/tmp/' . $tn . $w, $path); if (file_exists($cache)) { $ext = pathinfo($path, PATHINFO_EXTENSION); $img = new \Image($cache); echo $img->render($ext); return true; } elseif (file_exists($path)) { // Создаём и сохраняем миниатюру $img = new \Image($path); $img->resize($w, $h, $crop, $enlarge); $dir = $this->makeDirStructure($id, $tn . $w); if ($dir) { $ext = pathinfo($path, PATHINFO_EXTENSION); $f3 = \Base::instance(); $res = $f3->write($dir . $tn . $field . '.' . $ext, $img->dump()); echo $img->render($ext); return true; } } } return false; }
public function getImage($size, $filename) { //make a new response out of the contents of the file // we refactor this to use the image resize functino. // set the response status code to 200 ok $response = Response::make(Image::resize($filename, $size), 200); $response->header('content-type', Image::getMimeType($filename)); return $response; }
/** * 处理头像上传 * 成功返回 头像文件否则返回false */ public static function uploadAvatar($uid, $fieldName) { $file = CUploadedFile::getInstanceByName($fieldName); if (is_object($file) && get_class($file)) { //保存原图像 //后缀名 $extensionName = $file->getExtensionName(); //文件名 id.jpg if (!in_array($extensionName, Yii::app()->params['image_extension'])) { $this->error('上传的文件名不合法'); } //save /public/upload/avatar/1/1.jpg $fileName = $uid . '_origin.' . $extensionName; $avatarDir = 'public/upload/avatar/' . $uid; if (!is_dir($avatarDir)) { mkdir($avatarDir, 0777); } $avatarName = $avatarDir . '/' . $fileName; if (!$file->saveAs($avatarName)) { $this->error('头像上传失败'); } //将图片进行处理 //载入 扩展 Yii::import('application.extensions.image.Image'); $image = new Image($avatarName); $image->resize(50, 50); $avatar_50 = $avatarDir . '/' . $uid . '_50.' . $extensionName; if (!$image->save($avatar_50)) { $this->error('头像处理失败'); } //保存 width =180 $image->resize(180, 180); $avatar_180 = $avatarDir . '/' . $uid . '_180.' . $extensionName; if (!$image->save($avatar_180)) { $this->error('头像处理失败'); } unset($image); //保存头像 用绝对路径 return '/' . $avatar_50; } else { return false; } }
/** * Returns a correctly sized profile photo if available. * If not, returns a default profile photo. */ public function photo($width = 160, $height = 160) { $files = glob('cache/saasy/accounts/' . $this->id . '.{jpg,png,gif}', GLOB_BRACE); if (count($files) > 0) { $photo = array_shift($files); $ext = strtolower(pathinfo($photo, PATHINFO_EXTENSION)); return '/' . \Image::resize($photo, $width, $height, 'cover', $ext); } return '/apps/saasy/pix/profile.png'; }
public function writeFile($upload) { if ($upload->isSuccess()) { $filename = $this->sanitizeFileName($upload->name); if ($upload->save(ROOT_DIR . '/' . $this->uploadPath(), $filename)) { $this->writeFilename($filename); $img = new Image(ROOT_DIR . '/' . $this->uploadPath() . '/' . $filename); $img->resize(32, ROOT_DIR . '/' . $this->uploadPath() . '/thumbs/' . $filename); } } }
public function getImage($size, $filename) { // Make a new response out of the contents of the file // We refactor this to use the image resize function. // Set the response status code to 200 OK $response = Response::make(Image::resize($filename, $size), 200); // Set the mime type for the response. // We now use the Image class for this also. $response->header('content-type', Image::getMimeType($filename)); // We return our image here. return $response; }
/** * Automatically resizes and/or crops an image and returns formatted IMG tag or URL * * @param string $path Path to the image file * @param array $options * * @return mixed Image tag or URL of the resized/cropped image * * @access public */ public function resize($path, $options = array()) { $options = array_merge(array('width' => null, 'height' => null, 'aspect' => true, 'crop' => false, 'cropvars' => array(), 'autocrop' => false, 'htmlAttributes' => array(), 'quality' => 90, 'urlOnly' => true), $options); foreach ($options as $key => $option) { ${$key} = $option; } $relFile = Image::resize($path, $options); //Return only the URL if ($options['urlOnly']) { return $relFile; } return $this->Html->image($relFile, $options['htmlAttributes']); }
public function process($args) { // get top/left crop offsets from query string (center is default) $top = Input::instance()->get('ctop', 'center'); $left = Input::instance()->get('cleft', 'center'); // ensure the values are valid $top = (is_numeric($top) or $top == 'top' or $top == 'center' or $top == 'bottom') ? $top : 'center'; $left = (is_numeric($left) or $left == 'left' or $left == 'center' or $left == 'right') ? $left : 'center'; // $rotate = Input::instance()->get('rotate'); $flip = Input::instance()->get('flip'); $flips = array('h' => 5, 'v' => 6); if (isset($this->row->preview_id) and $this->row->preview_id > 0) { $this->row = $this->row->preview; } if ($this->row->is_image) { $image = new Image(DATAPATH . 'uploads/' . $this->row->file_name); // if (!is_null($rotate) AND is_numeric($rotate)) { $image->rotate( (int) $rotate ); } if (!is_null($flip) and isset($flips[$flip])) { $image->flip($flips[$flip]); } $a = $image->width; $b = $image->height; if ($a != $args[0] or $b != $args[1]) { $s = $args[0] - $a; $t = $args[1] - $b; $r = max(($a + $s) / $a, ($b + $t) / $b); $x = $a * $r; $y = $b * $r; if (!is_numeric($args[0])) { die('invalid width: ' . $args[0]); } if (!is_numeric($args[1])) { die('invalid height: ' . $args[1]); } // $image->quality(75); $image->resize($x, $y, Image::NONE); $image->crop($args[0], $args[1], $top, $left); } $uniqid = '/tmp/temp_image-' . uniqid() . $this->row->file_ext; // Save to a temporary file $image->save($uniqid); $this->image->clear(); $this->image->readImage($uniqid); unlink($uniqid); $border = Input::instance()->get('border', FALSE); if ($border) { $color = Input::instance()->get('bordercolor', null); $this->addborder($border, $color); } } }
public function resizeme($filename, $width, $height, $type = true, $copy = true) { if (!class_exists('PhpThumbFactory')) { require_once DIR_SYSTEM . 'library/image/ThumbLib.php'; } $ok = false; if (!file_exists(DIR_IMAGE . $filename) || !is_file(DIR_IMAGE . $filename)) { return $ok; } $info = pathinfo($filename); $extension = $info['extension']; $old_image = $filename; $new_image = 'cache/' . utf8_substr($filename, 0, utf8_strrpos($filename, '.')) . '-' . $width . 'x' . $height . '.' . $extension; if (!file_exists(DIR_IMAGE . $new_image) || filemtime(DIR_IMAGE . $old_image) > filemtime(DIR_IMAGE . $new_image)) { $path = ''; $directories = explode('/', dirname(str_replace('../', '', $new_image))); foreach ($directories as $directory) { $path = $path . '/' . $directory; if (!file_exists(DIR_IMAGE . $path)) { @mkdir(DIR_IMAGE . $path, 0777); } } list($width_orig, $height_orig) = getimagesize(DIR_IMAGE . $old_image); if ($width_orig != $width || $height_orig != $height || !$copy) { //********* code ************* $thumb = PhpThumbFactory::create(DIR_IMAGE . $old_image, array('resizeUp' => true)); if ($type) { $ok = $thumb->adaptiveResize($width, $height)->save(DIR_IMAGE . $new_image); } else { //$ok = $thumb->resize($width, $height)->save(DIR_IMAGE . $new_image); // opencart standart $image = new Image(DIR_IMAGE . $old_image); $image->resize($width, $height); $image->save(DIR_IMAGE . $new_image); $ok = true; } //********* code ************* } else { $ok = copy(DIR_IMAGE . $old_image, DIR_IMAGE . $new_image); } if ($ok) { return $this->getHttpImage() . $new_image; } else { return ''; } } else { return $this->getHttpImage() . $new_image; } }