<?php
/**
* 图片缩放
* @param string $url
* @param int $maxWidth
* @param int $maxHeight
* @return string
*/
function thumb($url, $maxWidth, $maxHeight, &$info) {
$info = $imgInfo = getimagesize($url);
$width = $imgInfo[0];//获取图片宽度
$height = $imgInfo[1];//获取图片高度
$r = min($maxHeight/$height, $maxWidth/$width);
if($r >= 1) { // 不用缩放
$maxHeight = $height;
$maxWidth = $width;
} elseif($r < 1) { // 缩放
$maxHeight = $height * $r;
$maxWidth = $width * $r;
}
$temp_img = imagecreatetruecolor($maxWidth,$maxHeight); //创建画布
$fun = str_replace('/', 'createfrom', $imgInfo['mime']);
$im = $fun($url);
imagecopyresized($temp_img,$im,0,0,0,0,$maxWidth,$maxHeight,$width,$height);
ob_start();
$fun = str_replace('/', '', $imgInfo['mime']);
$fun($temp_img);
$imgstr = ob_get_contents();
ob_end_clean();
imagedestroy($im);
return $imgstr;
}
$imgUrl = $_GET['url'];
$info = array();
$string = thumb($imgUrl, 500, 500, $info);
$mimeArray = explode("/", $info['mime']);
header("Content-Type:image/{$mimeArray[1]}");
echo $string;
以上代码存为thumb.php,调用效果: