示例#1
0
 function thumb($board, $filename, $ext, $s = 250)
 {
     $extension = $this->getGraphicsExtension();
     if ($ext == ".webm") {
         $fname = './' . $board . '/src/' . $filename . $ext;
         $thumb_dir = './' . $board . '/src/thumb/';
         require_once dirname(__FILE__) . '/webm.class.php';
         $movie = new \webm($fname);
         if ($movie->thumbnail($thumb_dir . $filename . '.gif', $s, $s)) {
             return array("width" => $s, "height" => $s);
         } else {
             echo "Problem with creating thumbnail\n";
         }
     }
     $filename = $filename . $ext;
     if (!(strpos($filename, "url:") === false)) {
         //dont make thumbnails of links xD
         return 0;
     }
     if ($extension == "imagick") {
         $fname = './' . $board . '/src/' . $filename;
         $thumb_dir = './' . $board . '/src/thumb/';
         //thumbnail directory
         $width = $s;
         //output width
         $height = $s;
         //output height
         $img = new \Imagick($fname);
         $img = $img->coalesceImages();
         foreach ($img as $frame) {
             $frame->thumbnailImage($width, $height, true);
         }
         $img->writeImages($thumb_dir . $filename, true);
         $ig = $img->getImageGeometry();
         $img->destroy();
         return $ig;
     } elseif ($extension == "gd") {
         if (!function_exists("ImageCreate") || !function_exists("ImageCreateFromJPEG")) {
             return;
         }
         $fname = './' . $board . '/src/' . $filename;
         $thumb_dir = './' . $board . '/src/thumb/';
         //thumbnail directory
         $width = $s;
         //output width
         $height = $s;
         //output height
         // width, height, and type are aquired
         $size = GetImageSize($fname);
         $type = "jpg";
         try {
             switch ($size[2]) {
                 case 1:
                     if (!function_exists("ImageCreateFromGIF")) {
                         return;
                     }
                     $im_in = ImageCreateFromGIF($fname);
                     $type = "gif";
                     if (!$im_in) {
                         return -1;
                     }
                     break;
                 case 2:
                     $im_in = ImageCreateFromJPEG($fname);
                     $type = "jpg";
                     if (!$im_in) {
                         return -1;
                     }
                     break;
                 case 3:
                     if (!function_exists("ImageCreateFromPNG")) {
                         return;
                     }
                     $im_in = ImageCreateFromPNG($fname);
                     $type = "png";
                     if (!$im_in) {
                         return -1;
                     }
                     break;
                 default:
                     return -2;
             }
         } catch (Exception $e) {
             return -1;
         }
         // Resizing
         if ($size[0] > $width || $size[1] > $height) {
             $key_w = $width / $size[0];
             $key_h = $height / $size[1];
             $key_w < $key_h ? $keys = $key_w : ($keys = $key_h);
             $out_w = ceil($size[0] * $keys) + 1;
             $out_h = ceil($size[1] * $keys) + 1;
         } else {
             $out_w = $size[0];
             $out_h = $size[1];
         }
         // the thumbnail is created
         if (function_exists("ImageCreateTrueColor")) {
             $im_out = ImageCreateTrueColor($out_w, $out_h);
         } else {
             $im_out = ImageCreate($out_w, $out_h);
         }
         // copy resized original
         ImageCopyResized($im_out, $im_in, 0, 0, 0, 0, $out_w, $out_h, $size[0], $size[1]);
         // thumbnail saved
         switch ($type) {
             case "jpg":
                 ImageJPEG($im_out, $thumb_dir . $filename, 70);
                 break;
             case "png":
                 ImagePNG($im_out, $thumb_dir . $filename, 9);
                 break;
             case "gif":
                 ImageGIF($im_out, $thumb_dir . $filename);
                 break;
         }
         chmod($thumb_dir . $filename, 0666);
         // created image is destroyed
         ImageDestroy($im_in);
         ImageDestroy($im_out);
         return array("width" => $out_w, "height" => $out_h);
     }
 }
示例#2
0
 function process_upload($upload)
 {
     global $min_upload_width, $min_upload_height, $max_upload_width, $max_upload_height;
     if ($upload == "") {
         echo "No data detected.";
         return false;
     }
     $ext = explode('.', $upload['name']);
     $count = count($ext);
     $ext = $ext[$count - 1];
     $ext = strtolower($ext);
     if ($ext != "jpg" && $ext != "jpeg" && $ext != "gif" && $ext != "png" && $ext != "webm") {
         echo "Invalid extension: ." . $ext . ".";
         return false;
     }
     $ext = "." . $ext;
     $fname = hash('sha1', hash_file('md5', $upload['tmp_name']));
     move_uploaded_file($upload['tmp_name'], "./tmp/" . $fname . $ext);
     $f = fopen("./tmp/" . $fname . $ext, "rb");
     if ($f == "") {
         echo "Could not open file for reading.";
         return false;
     }
     $data = '';
     while (!feof($f)) {
         $data .= fread($f, 4096);
     }
     fclose($f);
     if (preg_match("#<(script|html|head|title|body|table|a\\s+href|link|plaintext)#si", $data) == 1) {
         echo "Invalid Data detected.";
         unlink("./tmp/" . $fname . $ext);
         return false;
     }
     if ($ext === ".webm") {
         $vid = new webm("./tmp/" . $fname . $ext);
         if ($vid->valid_webm()) {
             $img = $vid->frame();
             $iinfo = [imagesx($img), imagesy($img)];
             $iinfo['mime'] = 'video/web';
         } else {
             echo "Invalid video file.";
             return false;
         }
     } else {
         $iinfo = getimagesize("./tmp/" . $fname . $ext);
     }
     if (substr($iinfo['mime'], 0, 5) != "image" && substr($iinfo['mime'], 0, 5) != "video" || $iinfo[0] < $min_upload_width && $min_upload_width != 0 || $iinfo[0] > $max_upload_width && $max_upload_width != 0 || $iinfo[1] < $min_upload_height && $min_upload_height != 0 || $iinfo[1] > $max_upload_height && $max_upload_height != 0 || !$this->checksum("./tmp/" . $fname . $ext)) {
         echo "Not a valid image or video file.";
         unlink("./tmp/" . $fname . $ext);
         return false;
     }
     $ffname = $fname;
     $cdir = $this->getcurrentfolder();
     $i = 0;
     if (!is_dir("./images/" . $cdir . "/")) {
         $this->makefolder($cdir);
     }
     while (file_exists("./images/" . $cdir . "/" . $fname . $ext)) {
         $i++;
         $fname = hash('sha1', hash('md5', $fname . $i));
     }
     $f = fopen("./images/" . $cdir . "/" . $fname . $ext, "w");
     if ($f == "") {
         echo "Could not write file to disk.";
         return false;
     }
     fwrite($f, $data);
     fclose($f);
     $this->folder_index_increment($cdir);
     unlink("./tmp/" . $ffname . $ext);
     return $cdir . ":" . $fname . $ext;
 }
示例#3
0
文件: common.php 项目: jasonbb/Haruko
 function thumb($board, $filename, $ext, $s = 250)
 {
     $extension = $this->getGraphicsExtension();
     if ($ext == ".webm") {
         //initialize
         $fname = './' . $board . '/src/' . $filename . $ext;
         $thumb_dir = './' . $board . '/src/thumb/';
         require_once 'webm.class.php';
         $movie = new \webm($fname);
         //actual logic...
         if ($movie->valid_webm()) {
             if ($movie->thumbnail($thumb_dir . $filename . '.gif', $s, $s, "") && $movie->thumbnail($thumb_dir . $filename . '.webm', $s, $s, "h")) {
                 return array("width" => $s, "height" => $s);
             } else {
                 echo "There was a problem in uploading your thumbnail... contact parley.";
             }
         } else {
             //echo "The file you uploaded was <strong>NOT</strong> a valid WebM file.";
         }
     }
     $filename = $filename . $ext;
     if (!(strpos($filename, "url:") === false)) {
         //dont make thumbnails of links
         return 0;
     }
     if ($extension == "imagick") {
         $fname = './' . $board . '/src/' . $filename;
         $thumb_dir = './' . $board . '/src/thumb/';
         //thumbnail directory
         $width = $s;
         //output width
         $height = $s;
         //output height
         $img = new \Imagick($fname);
         $img = $img->coalesceImages();
         foreach ($img as $frame) {
             $frame->thumbnailImage($width, $height, true);
         }
         $img->writeImages($thumb_dir . $filename, true);
         $ig = $img->getImageGeometry();
         $img->destroy();
         return $ig;
     } elseif ($extension == "gd") {
         if (!function_exists("ImageCreate") || !function_exists("ImageCreateFromJPEG")) {
             return;
         }
         $fname = './' . $board . '/src/' . $filename;
         $thumb_dir = './' . $board . '/src/thumb/';
         //thumbnail directory
         $width = $s;
         //output width
         $height = $s;
         //output height
         // width, height, and type are aquired
         $size = GetImageSize($fname);
         $type = "jpg";
         try {
             switch ($size[2]) {
                 case 1:
                     if (!function_exists("ImageCreateFromGIF")) {
                         return;
                     }
                     $im_in = ImageCreateFromGIF($fname);
                     $type = "gif";
                     if (!$im_in) {
                         return -1;
                     }
                     break;
                 case 2:
                     $im_in = ImageCreateFromJPEG($fname);
                     $type = "jpg";
                     if (!$im_in) {
                         return -1;
                     }
                     break;
                 case 3:
                     if (!function_exists("ImageCreateFromPNG")) {
                         return;
                     }
                     $im_in = ImageCreateFromPNG($fname);
                     $type = "png";
                     if (!$im_in) {
                         return -1;
                     }
                     break;
                 default:
                     return -2;
             }
         } catch (Exception $e) {
             return -1;
         }
         // Resizing
         if ($size[0] > $width || $size[1] > $height) {
             $key_w = $width / $size[0];
             $key_h = $height / $size[1];
             $key_w < $key_h ? $keys = $key_w : ($keys = $key_h);
             $out_w = ceil($size[0] * $keys) + 1;
             $out_h = ceil($size[1] * $keys) + 1;
         } else {
             $out_w = $size[0];
             $out_h = $size[1];
         }
         // the thumbnail is created
         if (function_exists("ImageCreateTrueColor")) {
             $im_out = ImageCreateTrueColor($out_w, $out_h);
         } else {
             $im_out = ImageCreate($out_w, $out_h);
         }
         //adds transparency [devnote: this took me f*****g 3-4 days to figure out how to do it... why isn't there better documentation?]
         imagealphablending($im_out, false);
         imagesavealpha($im_out, true);
         $trans_layer_overlay = imagecolorallocatealpha($image, 220, 220, 220, 127);
         imagefill($im_out, 0, 0, $trans_layer_overlay);
         // copy resized original
         imagecopyresampled($im_out, $im_in, 0, 0, 0, 0, $out_w, $out_h, $size[0], $size[1]);
         // thumbnail saved
         switch ($type) {
             case "jpg":
                 ImageJPEG($im_out, $thumb_dir . $filename, 70);
                 break;
             case "png":
                 ImagePNG($im_out, $thumb_dir . $filename, 9);
                 break;
             case "gif":
                 ImageGIF($im_out, $thumb_dir . $filename);
                 break;
         }
         chmod($thumb_dir . $filename, 0666);
         // created image is destroyed
         ImageDestroy($im_in);
         ImageDestroy($im_out);
         return array("width" => $out_w, "height" => $out_h);
     }
 }