View file imageresize/htx.php

File size: 2.65Kb
<?
error_reporting(0);
function resizeimg($filename, $smallimage, $w, $h, $angle, $color)
{
if($angle<>0) {$rotate=TRUE;} else {$rotate=FALSE;}
list($width, $height, $type, $attr) = @getimagesize( $smallimage );
if (!$rotate and file_exists($smallimage) and ($w==$width or $h==$height)) {
return true;
} else {
@unlink($smallimage);
}
$ratio = $w/$h;
list($width, $height, $type, $attr) = @getimagesize( $filename );
if (($width<$w) && ($height<$h)) return true;
$src_ratio=$width/$height;
if ($ratio<$src_ratio)
{
$h = $w/$src_ratio;
}
else
{
$w = $h*$src_ratio;
}
$dest_img = imagecreatetruecolor($w, $h);
$white = imagecolorallocate($dest_img, 255, 255, 255);
if ($type==2)  $src_img = imagecreatefromjpeg($filename);
else if ($type==1) $src_img = imagecreatefromgif($filename);
else if ($type==3) $src_img = imagecreatefrompng($filename);
imagecopyresampled($dest_img, $src_img, 0, 0, 0, 0, $w, $h, $width, $height);
if($rotate) {
$src_img=$dest_img;
$dest_img = imagerotate($src_img, $angle, $color);
}
if($smallimage=="_screen") {
if ($type==1) {header('Content-type: image/gif');  imagegif ($dest_img);}
else if ($type==2) {header('Content-type: image/jpeg'); imagejpeg($dest_img);}
else if ($type==3) {header('Content-type: image/png');  imagepng ($dest_img);}
} else {
if ($type==1) imagegif ($dest_img, $smallimage);
else if ($type==2) imagejpeg($dest_img, $smallimage);
else if ($type==3) imagepng ($dest_img, $smallimage);
}
imagedestroy($dest_img);
imagedestroy($src_img);
return true;
}

$url = $_POST['url'];
$imw = $_POST['imw'];
$imh = $_POST['imh'];
if(!preg_match("#.+\.[jpg|jpeg|gif|png]+$#i", $url)) { echo 'Недопустимый формат файла'; exit; }
$rnd = rand(10000, 99999);
$file = file_get_contents($url);
$fp = fopen("$rnd.gif", "w");
fputs($fp, $file);
$inf = getimagesize("$rnd.gif");
if($inf[2] == 1) { $rn = "gif"; }
if($inf[2] == 2) { $rn = "jpg"; }
if($inf[2] == 3) { $rn = "png"; }
rename("$rnd.gif", "$rnd.$rn");
fclose($fp);
if(filesize("$rnd.$rn") != 0) { $rand = rand(1000000000, 9999999999);
resizeimg("$rnd.$rn", "files/$rand.$rn", $imw, $imh, $ugol, $color);
unlink("$rnd.$rn");

header("Content-type: text/html; charset=utf-8");
echo '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> <head> <title>HTX</title></head> <body bgcolor="#eeeeee" link="#2c75b1" vlink="#2c75b1">';
echo '<img src="http://localhost/htx/files/'.$rand.'.'.$rn.'" alt=" "><br>';
echo '<a href="http://localhost/htx/files/'.$rand.'.'.$rn.'">DownLoad</a><br>
&#169; <a href="http://phptime.org">PhpTime</a>, 2006.</body></html>'; } else { echo 'File Not Found'; unlink("$rnd.$rn"); }
?>