|
<?php
$r = new StdClass ();
Header (' Content-type:application/json ');
$maxsize = 10; Mb
if ($_files[' xfile '] [' Size '] > ($maxsize * 1048576)) {
$r->error = "picture size" > picture size not exceeding $maxsize MB ";
}
$folder = ' files/';
if (!is_dir ($folder)) {
mkdir ($folder);
}
$folder. = $_post[' folder ']? $_post[' folder ']. '/' : '';
if (!is_dir ($folder)) {
mkdir ($folder);
}
if (Preg_match ('/image/i ', $_files[' xfile '] [' type '])) {
$filename = $_post[' value ']? $_post[' value ': $folder. SHA1 (@microtime (). '-' . $_files[' XFile ' [' name ']]. '. jpg ';
}else{
$tld = Split (', ', $_files[' xfile '] [' name ']);
$tld = $tld [Count ($tld)-1];
$filename = $_post[' value ']? $_post[' value ': $folder. SHA1 (@microtime (). '-' . $_files[' XFile ' [' name ']]. $tld;
}
$types = Array (' image/png ', ' image/gif ', ' image/jpeg ');
if (In_array ($_files[' xfile '] [' type '], $types)) {
$source = file_get_contents ($_files["XFile"] ["tmp_name"]);
Imageresize ($source, $filename, $_post[' width '], $_post[' height '], $_post[' crop ', $_post[' quality ']);
}else{
Move_uploaded_file ($_files["XFile"] ["Tmp_name"], $filename);
}
$path = Str_replace (' test.php ', ', ', $_server[' script_name '));
$r->filename = $filename;
$r->path = $path;
$r->img = '
echo Json_encode ($R);
function Imageresize ($source, $destination, $width = 0, $height = 0, $crop = false, $quality = 80) {
$quality = $quality? $quality: 80;
$image = imagecreatefromstring ($source);
if ($image) {
Get dimensions
$w = Imagesx ($image);
$h = Imagesy ($image);
if ($width && $w > $width) | | ($height && $h > $height)) {
$ratio = $w/$h;
if (($ratio >= 1 | | $height = = 0) && $width &&! $crop) {
$new _height = $width/$ratio;
$new _width = $width;
} elseif ($crop && $ratio <= ($width/$height)) {
$new _height = $width/$ratio;
$new _width = $width;
} else {
$new _width = $height * $ratio;
$new _height = $height;
}
} else {
$new _width = $w;
$new _height = $h;
}
$x _mid = $new _width *. 5; Horizontal Middle
$y _mid = $new _height *. 5; Vertical Middle
Resample
Error_log (' Height: ') $new _height. '-width: '. $new _width);
$new = Imagecreatetruecolor (round ($new _width), round ($new _height));
Imagecopyresampled ($new, $image, 0, 0, 0, 0, $new _width, $new _height, $w, $h);
Crop
if ($crop) {
$crop = Imagecreatetruecolor ($width $width: $new _width, $height? $height: $new _height);
Imagecopyresampled ($crop, $new, 0, 0, ($x _mid-($width *. 5)), 0, $width, $height, $width, $height);
($y _mid-($height *. 5))
}
Output
Enable interlancing [for progressive JPEG]
Imageinterlace ($crop $crop: $new, True);
$dext = Strtolower (PathInfo ($destination, pathinfo_extension));
if ($dext = = ") {
$dext = $ext;
$destination. = '. ' $ext;
}
Switch ($dext) {
Case ' JPEG ':
Case ' jpg ':
Imagejpeg ($crop $crop: $new, $destination, $quality);
Break
Case ' PNG ':
$pngQuality = ($quality-100)/11.111111;
$pngQuality = Round (ABS ($pngQuality));
Imagepng ($crop $crop: $new, $destination, $pngQuality);
Break
Case ' gif ':
Imagegif ($crop $crop: $new, $destination);
Break
}
@imagedestroy ($image);
@imagedestroy ($new);
@imagedestroy ($crop);
}
}
?> |