protected function act_thumb() { $this->getDir($this->get['dir'], true); if (!isset($this->get['file']) || !isset($this->get['dir'])) { $this->sendDefaultThumb(); } $file = $this->get['file']; if (basename($file) != $file) { $this->sendDefaultThumb(); } $file = "{$this->thumbsDir}/{$this->type}/{$this->get['dir']}/{$file}"; if (!is_file($file) || !is_readable($file)) { $file = "{$this->config['uploadDir']}/{$this->type}/{$this->get['dir']}/" . basename($file); if (!is_file($file) || !is_readable($file)) { $this->sendDefaultThumb($file); } $image = new gd($file); if ($image->init_error) { $this->sendDefaultThumb($file); } $browsable = array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG); if (in_array($image->type, $browsable) && $image->get_width() <= $this->config['thumbWidth'] && $image->get_height() <= $this->config['thumbHeight']) { $type = $image->type == IMAGETYPE_GIF ? "gif" : ($image->type == IMAGETYPE_PNG ? "png" : "jpeg"); $type = "image/{$type}"; httpCache::file($file, $type); } else { $this->sendDefaultThumb($file); } } httpCache::file($file, "image/jpeg"); }
/** Neka si predstavim vyobrazhaem pravoygylnik s razmeri $width i $height. * Izobrazhenieto shte se preorazmeri taka che to shte izliza ot tozi pravoygylnik, * no samo po edno (x ili y) izmerenie * @param integer $width * @param integer $height * @return bool */ public function resize_overflow($width, $height) { $big = $this->width / $this->height > $width / $height ? $this->width * $height / $this->height : $this->height * $width / $this->width; $big = intval($big); $return = $img = new gd($this->image, $big); if ($return) { $this->image = $img->get_image(); $this->width = $img->get_width(); $this->height = $img->get_height(); } return $return; }
protected function act_upload() { $baseDir = dirname($this->config['uploadDir']); if (!isset($this->post['dir']) || false === ($dir = "{$baseDir}/{$this->post['dir']}") || !is_dir($dir) || !is_readable($dir) || !is_writable($dir)) { die($this->label("Cannot access or write to upload folder.")); } $message = $this->checkUploadedFile(); if ($message !== true) { if (isset($this->file['tmp_name'])) { @unlink($this->file['tmp_name']); } die($message); } $sufix = ""; $i = 1; $ext = $this->getExtension($this->file['name'], false); $base = strlen($ext) ? substr($this->file['name'], 0, -strlen($ext) - 1) : $this->file['name']; do { $target = "{$dir}/{$base}{$sufix}" . (strlen($ext) ? ".{$ext}" : ""); $sufix = "(" . $i++ . ")"; } while (file_exists($target)); if (!move_uploaded_file($this->file['tmp_name'], $target)) { @unlink($this->file['tmp_name']); die($this->label("Cannot move uploaded file to target folder.")); } elseif (function_exists('chmod')) { chmod($target, $this->config['filePerms']); } echo "/" . basename($target); // THUMBNAIL GENERATION $gd = new gd($target); if ($gd->init_error) { return; } // Images with smaller resolutions than thumbnails if ($gd->get_width() <= $this->config['thumbWidth'] && $gd->get_height() <= $this->config['thumbHeight']) { $browsable = array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_JPEG2000, IMAGETYPE_PNG); // Browsable types does not need a thumbnail if (in_array($gd->type, $browsable)) { return; } // Resize image } elseif (!$gd->resize_fit($this->config['thumbWidth'], $this->config['thumbHeight'])) { return; } // Check thumbnail directory $thumb = "{$baseDir}/{$this->config['thumbsDir']}/{$this->post['dir']}"; if (!is_dir($thumb) && !helper::rmkdir($thumb, $this->config['dirPerms'])) { return; } $thumb .= "/" . basename($target); // Save thumbnail $gd->imagejpeg($thumb, $this->config['jpegQuality']); }
protected function makeThumb($file, $overwrite = true) { $gd = new gd($file); // Drop files which are not GD handled images if ($gd->init_error) { return true; } $thumb = substr($file, strlen($this->config['uploadDir'])); $thumb = $this->config['uploadDir'] . "/" . $this->config['thumbsDir'] . "/" . $thumb; $thumb = path::normalize($thumb); $thumbDir = dirname($thumb); if (!is_dir($thumbDir) && !@mkdir($thumbDir, $this->config['dirPerms'], true)) { return false; } if (!$overwrite && is_file($thumb)) { return true; } // Images with smaller resolutions than thumbnails if ($gd->get_width() <= $this->config['thumbWidth'] && $gd->get_height() <= $this->config['thumbHeight']) { $browsable = array(IMAGETYPE_GIF, IMAGETYPE_JPEG, IMAGETYPE_PNG); // Drop only browsable types if (in_array($gd->type, $browsable)) { return true; } // Resize image } elseif (!$gd->resize_fit($this->config['thumbWidth'], $this->config['thumbHeight'])) { return false; } // Save thumbnail return $gd->imagejpeg($thumb, $this->config['jpegQuality']); }
protected function checkUploadedFile() { $config =& $this->config; $file =& $this->file; if (!is_array($file) || !isset($file['name'])) { return $this->label("Unknown error"); } $extension = preg_match('/^.*\\.([^\\.]*)$/s', $file['name'], $patt) ? strtolower($patt[1]) : ""; $extensions = strtolower(helper::clear_whitespaces($this->types[$this->type])); // CHECK FOR UPLOAD ERRORS if ($file['error']) { return $file['error'] == UPLOAD_ERR_INI_SIZE ? $this->label("The uploaded file exceeds {size} bytes.", array('size' => ini_get('upload_max_filesize'))) : ($file['error'] == UPLOAD_ERR_FORM_SIZE ? $this->label("The uploaded file exceeds {size} bytes.", array('size' => $_GET['MAX_FILE_SIZE'])) : ($file['error'] == UPLOAD_ERR_PARTIAL ? $this->label("The uploaded file was only partially uploaded.") : ($file['error'] == UPLOAD_ERR_NO_FILE ? $this->label("No file was uploaded.") : ($file['error'] == UPLOAD_ERR_NO_TMP_DIR ? $this->label("Missing a temporary folder.") : ($file['error'] == UPLOAD_ERR_CANT_WRITE ? $this->label("Failed to write file.") : $this->label("Unknown error.")))))); } elseif (substr($file['name'], 0, 1) == ".") { return $this->label("File name shouldn't begins with '.'"); } elseif (!$this->validateExtension($extension, $this->type)) { return $this->label("Denied file extension."); } elseif (preg_match('/^\\*([^ ]+)(.*)?$/s', $extensions, $patt)) { list($extensions, $type, $params) = $patt; if (class_exists("type_{$type}")) { $class = "type_{$type}"; $type = new $class(); $cfg = $config; if (strlen($params)) { $cfg['params'] = trim($params); } $response = $type->checkFile($file['tmp_name'], $cfg); if ($response !== true) { return $this->label($response); } } else { return $this->label("Unexisting directory type."); } } // IMAGE RESIZE $gd = new gd($file['tmp_name']); if (!$gd->init_error && ($config['maxImageWidth'] || $config['maxImageHeight']) && ($gd->get_width() > $config['maxImageWidth'] || $gd->get_height() > $config['maxImageHeight']) && (!$gd->resize_fit($config['maxImageWidth'], $config['maxImageHeight']) || !$gd->imagejpeg($file['tmp_name'], $config['jpegQuality']))) { return $this->label("The image is too big and/or cannot be resized."); } return true; }