148 lines
3.3 KiB
PHP
148 lines
3.3 KiB
PHP
|
<?
|
||
|
|
||
|
/*
|
||
|
$imgPath 源图片路径
|
||
|
$maxWidth 缩小后最大宽度
|
||
|
$maxHeight 缩小后最大高度
|
||
|
$destPath 目标路径,留空则直接输出
|
||
|
*/
|
||
|
function resizeImg($imgPath, $maxWidth, $maxHeight, $destPath = '', $isdel = false) {
|
||
|
if(stristr($imgPath, '.png')) {
|
||
|
$img = @imagecreatefrompng($imgPath);
|
||
|
} else {
|
||
|
$img = @imagecreatefromjpeg($imgPath);
|
||
|
}
|
||
|
if(!$img) {
|
||
|
if($isdel) {
|
||
|
@unlink($imgPath);
|
||
|
}
|
||
|
exit;
|
||
|
}
|
||
|
$w = $w_o = imagesx($img);
|
||
|
$h = $h_o = imagesy($img);
|
||
|
|
||
|
if($w > $maxWidth) {
|
||
|
$h = intval($h*$maxWidth/$w);
|
||
|
$w = $maxWidth;
|
||
|
}
|
||
|
if($h > $maxHeight) {
|
||
|
$w = intval($w*$maxHeight/$h);
|
||
|
$h = $maxHeight;
|
||
|
}
|
||
|
if($w>$w_o || $h>$h_o ) {
|
||
|
$w = $w_o;
|
||
|
$h = $h_o;
|
||
|
}
|
||
|
//echo $w.",".$h;exit;
|
||
|
$img2 = imagecreatetruecolor($w, $h);
|
||
|
imagecopyresampled($img2, $img, 0, 0, 0, 0, $w, $h, $w_o, $h_o);
|
||
|
if($destPath) {
|
||
|
imagejpeg($img2, $destPath, 80);
|
||
|
} else {
|
||
|
header("Content-Type:image/jpeg");
|
||
|
imagejpeg($img2, '', 80);
|
||
|
}
|
||
|
|
||
|
imagedestroy($img2);
|
||
|
imagedestroy($img);
|
||
|
}
|
||
|
|
||
|
function check_domain($url, $check_domain) {
|
||
|
$pos1 = strpos($url, "//");
|
||
|
if($pos1 === false) return false;
|
||
|
$pos2 = strpos($url, "/", $pos1+2);
|
||
|
if($pos2 === false) return false;
|
||
|
$domain = substr($url, $pos1+2, $pos2-$pos1-2);
|
||
|
$len1 = strlen($domain);
|
||
|
$len2 = strlen($check_domain);
|
||
|
$pos = stripos($domain, $check_domain);
|
||
|
if($pos === false) return false;
|
||
|
if($len1 == $len2+$pos) return true;
|
||
|
return false;
|
||
|
}
|
||
|
|
||
|
$width = intval($_REQUEST['w']);
|
||
|
$height = intval($_REQUEST['h']);
|
||
|
if($width <= 0 || $height <= 0) exit;
|
||
|
|
||
|
$pic = htmlentities($_REQUEST['p']);
|
||
|
if(!$pic) exit;
|
||
|
|
||
|
$root = dirname(__FILE__);
|
||
|
$destPath = $root."/images/thumb/".md5($pic).".jpg";
|
||
|
|
||
|
if(is_file($destPath)) {
|
||
|
header("Content-Type:image/jpeg");
|
||
|
echo file_get_contents($destPath);
|
||
|
exit;
|
||
|
}
|
||
|
function httpget($url) {
|
||
|
$c = curl_init($url);
|
||
|
curl_setopt($c, CURLOPT_RETURNTRANSFER, 1);
|
||
|
curl_setopt($c, CURLOPT_TIMEOUT, 10);
|
||
|
curl_setopt($c, CURLOPT_SSL_VERIFYPEER, 0);
|
||
|
curl_setopt($c, CURLOPT_SSL_VERIFYHOST, 0);
|
||
|
|
||
|
$s = curl_exec($c);
|
||
|
curl_close($c);
|
||
|
return $s;
|
||
|
}
|
||
|
$ishttp = false;
|
||
|
if(substr($pic, 0, 4) == 'http') {
|
||
|
$domains = array('360buyimg.com', '127.net', 'duifuli.com', 'weibo.com');
|
||
|
$suc = false;
|
||
|
foreach($domains as $domain) {
|
||
|
if(check_domain($pic, $domain)) {
|
||
|
$suc = true;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
if(!$suc) die("domain error!");
|
||
|
if(strstr($pic,'360buyimg') || strstr($pic,'yanxuan')) {
|
||
|
$pic = str_replace('http://', 'https://', $pic);
|
||
|
header("Location: ".$pic);
|
||
|
exit;
|
||
|
}
|
||
|
$ext = end(explode('.', $pic));
|
||
|
if(strlen($ext) > 4) $ext = 'data';
|
||
|
$filepath = $root."/img_".md5(microtime()."_".$pic).".".$ext;
|
||
|
|
||
|
|
||
|
$s = httpget($pic);
|
||
|
//if(!$s && substr($pic, 0, 5) == 'https') {
|
||
|
// $s = httpget(str_replace('https://', 'http://', $pic));
|
||
|
//}
|
||
|
if(!$s) exit;
|
||
|
file_put_contents($filepath, $s);
|
||
|
$ishttp = true;
|
||
|
} else {
|
||
|
$filepath = $root."/../".$pic;
|
||
|
if(!is_file($filepath)) {
|
||
|
$filepath = $root."/../backstage/".$pic;
|
||
|
if(!is_file($filepath)) exit;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
//echo "vvvvvvvvv";
|
||
|
//echo $destPath;
|
||
|
//exit;
|
||
|
if($width > 5000 && $ishttp) {
|
||
|
if(stristr($pic, '.png')) {
|
||
|
header("Content-Type:image/png");
|
||
|
} else {
|
||
|
header("Content-Type:image/jpeg");
|
||
|
}
|
||
|
rename($filepath, $destPath);
|
||
|
echo file_get_contents($destPath);
|
||
|
} else {
|
||
|
resizeImg($filepath, $width, $height, $destPath, $ishttp?true:false);
|
||
|
if($ishttp) @unlink($filepath);
|
||
|
header("Content-Type:image/jpeg");
|
||
|
echo file_get_contents($destPath);
|
||
|
}
|
||
|
|
||
|
|
||
|
|