(codeIgniter框架为基础)
function http_get_data($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_URL, $url);
ob_start();
curl_exec($ch);
$return_content = ob_get_contents();
ob_end_clean();
$return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
return $return_content;
}
private function getNetworkImgType($url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 3);
curl_setopt($ch, CURLOPT_TIMEOUT, 3);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_exec($ch);
$http_code = curl_getinfo($ch);
curl_close($ch);
if ($http_code['http_code'] == 200) {
$theImgType = explode('/', $http_code['content_type']);
if ($theImgType[0] == 'image') {
return $theImgType[1];
} else {
return false;
}
} else {
return false;
}
}
function createImageFromFile($file)
{
if (preg_match('/http(s)?:\/\//', $file)) {
$fileSuffix = $this->getNetworkImgType($file);
} else {
$fileSuffix = pathinfo($file, PATHINFO_EXTENSION);
}
if (!$fileSuffix) return false;
switch ($fileSuffix) {
case 'jpeg':
if (!strpos($file, 'qnimg.ruwii.com') && !strpos($file, 'upload')) {
$theImage = @imagecreatefromstring($this->http_get_data($file));
} else {
$theImage = @imagecreatefromjpeg($file);
if (!$theImage) {
$theImage = @imagecreatefromstring($this->http_get_data($file));
}
}
break;
case 'jpg':
if (!strpos($file, 'qnimg.ruwii.com') && !strpos($file, 'upload')) {
$theImage = @imagecreatefromstring($this->http_get_data($file));
} else {
$theImage = @imagecreatefromjpeg($file);
if (!$theImage) {
$theImage = @imagecreatefromstring($this->http_get_data($file));
}
}
break;
case 'png':
if (!strpos($file, 'qnimg.ruwii.com') && !strpos($file, 'upload')) {
$theImage = @imagecreatefromstring($this->http_get_data($file));
} else {
$theImage = @imagecreatefrompng($file);
if (!$theImage) {
$theImage = @imagecreatefromstring($this->http_get_data($file));
}
}
break;
case 'gif':
if (!strpos($file, 'qnimg.ruwii.com') && !strpos($file, 'upload')) {
$theImage = @imagecreatefromstring($this->http_get_data($file));
} else {
$theImage = @imagecreatefromgif($file);
if (!$theImage) {
$theImage = @imagecreatefromstring($this->http_get_data($file));
}
}
break;
default:
$theImage = @imagecreatefromstring($this->http_get_data($file));
break;
}
return $theImage;
}
function changeCircularImg($imgpath)
{
$src_img = null;
$src_img = $this->createImageFromFile($imgpath);
$wh = getimagesize($imgpath);
$w = $wh[0];
$h = $wh[1];
$w = min($w, $h);
$h = $w;
$img = imagecreatetruecolor($w, $h);
imagesavealpha($img, true);
$bg = imagecolorallocatealpha($img, 255, 255, 255, 127);
imagefill($img, 0, 0, $bg);
imagesavealpha($img , true);
$r = $w / 2;
$y_x = $r;
$y_y = $r;
for ($x = 0; $x < $w;
$x++) {
for ($y = 0; $y < $h; $y++) {
$rgbColor = imagecolorat($src_img, $x,
$y);
if (((($x - $r) * ($x - $r) + ($y - $r) * ($y - $r)) < ($r * $r))) {
imagesetpixel($img, $x, $y, $rgbColor);
}
}
}
return $img;
}
|