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; }
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 makeThumb2($target, $fileUpload = null, $overwrite = true) { $gd = new gd($fileUpload); // Drop files which are not GD handled images if ($gd->init_error) { return true; } $mpid = strtok($target, '/'); $thumb = str_replace($mpid, "{$mpid}/{$this->config['thumbsDir']}", $target); $thumb = path::normalize($thumb); //if (!$overwrite && is_file($thumb)) // return true; // Resize image if (!$gd->resize_fit($this->config['thumbWidth'], $this->config['thumbHeight'])) { return false; } // Save thumbnail $temp = tempnam(sys_get_temp_dir(), uniqid()); if (!$gd->imagejpeg($temp, $this->config['jpegQuality'])) { @unlink($temp); return false; } $bucket = 'xinxintong'; $alioss = $this->get_alioss(); $rsp = $alioss->upload_file_by_file($bucket, $thumb, $temp); @unlink($temp); return true; }