Skip to content

Instantly share code, notes, and snippets.

@reinforchu
Created January 24, 2013 16:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save reinforchu/4624273 to your computer and use it in GitHub Desktop.
Save reinforchu/4624273 to your computer and use it in GitHub Desktop.
書きかけてすてすにゃー
<?php
require_once('../../MySQL.php');
error_reporting(-1);
$upload = new upload();
class upload {
public $file;
public $ext;
public $thumb;
private $date;
private $imgid;
private $desc;
private $ac;
public function __construct() {
$handle = fopen($_FILES['file']['tmp_name'], 'rb');
$file = fread($handle, filesize($_FILES['file']['tmp_name']));
fclose($handle);
self::fileChk($_FILES['file']['tmp_name']);
if ($this->ext === FALSE) exit();
$this->date = date('U');
$this->imgid = uniqid(mt_rand(100, 999));
$this->file = $file;
self::createThumbnail($_FILES['file']['tmp_name']);
$this->file = self::convertBase64($this->file);
$this->thumb = self::convertBase64($this->thumb);
self::insertDb();
}
private function fileChk($file) {
switch(exif_imagetype($file)) {
case IMAGETYPE_GIF : $this->ext = 'gif'; break;
case IMAGETYPE_JPEG : $this->ext = 'jpg'; break;
case IMAGETYPE_PNG : $this->ext = 'png'; break;
case IMAGETYPE_BMP : $this->ext = 'bmp'; break;
default : $this->ext = FALSE;
}
}
private function insertDb() {
$MySQL = new MySQL();
$MySQL->insertImg($this->imgid, $this->file, $this->thumb, $this->ext);
}
private function convertBase64($file) {
return base64_encode($file);
}
private function createThumbnail($filePath) {
if (preg_match("/^jpg$/iu", $this->ext) == 1) {
$img = imagecreatefromjpeg($filePath);
} else if (preg_match("/^png$/iu", $this->ext) == 1) {
$img = imagecreatefrompng($filePath);
} else if (preg_match("/^gif$/iu", $this->ext) == 1) {
$img = imagecreatefromgif($filePath);
} else if (preg_match("/^bmp$/iu", $this->ext) == 1) {
$img = imagecreatefromwbmp($filePath);
}
$width = ImageSx($img);
$height = ImageSy($img);
$out = ImageCreateTrueColor(96, 96);
ImageCopyResampled($out, $img, 0,0,0,0, 96, 96, $width, $height);
imagejpeg($out, "R:\\{$this->imgid}.jpg", 70);
imagedestroy($out);
$handle = fopen("R:\\{$this->imgid}.jpg", 'rb');
$file = fread($handle, filesize("R:\\{$this->imgid}.jpg"));
fclose($handle);
unlink("R:\\{$this->imgid}.jpg");
$this->thumb = $file;
}
}
?>
<!DOCTYPE html>
<html>
<body>
<img src="data:image/<?php print($upload->ext); ?>;base64,<?php print($upload->file); ?>">
<img src="data:image/jpg;base64,<?php print($upload->thumb); ?>">
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment