php生成小图片
很多地方,一张大的图片很占用服务器资源,然后需要生成小图片
<?php
/*
*Author:伴侣哟-Blyoo
*Author Blog:www.blyoo.com
*/
error_reporting(0);
?>
<?php
if(!empty($_GET['url'])){
if(substr($_GET['url'], 0,2)=='//'){ //这里是判断图片地址有没有加http
$img_url='http:'.$_GET['url'];
}elseif(substr($_GET['url'], 0,2)!='//' && substr($_GET['url'], 0,4)!='http'){
$img_url='http://'.$_GET['url'];
}else{
$img_url=$_GET['url'];
}
$img_size=getimagesize($img_url);
$img_width=$img_size[0];
$img_height=$img_size[1];
$img_mime=$img_size['mime'];
if(!empty($_GET['p'])){ //这是按比例生成的,如果要计算长宽然后生成指定长宽,自行写,
if($_GET['p']>1000){ //比例参数不写,默认0.2
$img_percent=0.5;
}else{
if($_GET['p']>10){
$img_percent=$_GET['p']/1000;
}else{
$img_percent=$_GET['p']/10;
}
}
}else{
$img_percent=0.2;
}
if(!empty($_GET['width']) && !empty($_GET['height'])){ //这里可以指定长宽 ,不写默认按照比例
$new_img_width=$_GET['width'];
$new_img_heigth=$_GET['height'];
}else{
$new_img_width=$img_width*$img_percent;
$new_img_heigth=$img_height*$img_percent;
}
//print_r($img_size);
switch ($img_mime) {
case 'image/jpeg':
$img_creat=imagecreatefromjpeg($img_url);
break;
case 'image/png':
$img_creat=imagecreatefrompng($img_url);
break;
case 'image/gif':
$img_creat=imagecreatefromgif($img_url);
break;
default:
return false;
break;
}
$img_thumb=imagecreatetruecolor($new_img_width, $new_img_heigth);
imagecopyresampled($img_thumb, $img_creat, 0, 0, 0, 0, $new_img_width, $new_img_heigth, $img_width, $img_height);
header('Content-Type: image/png');
imagejpeg($img_thumb);
}else{
exit('error');
}
?>
可以使用我的 ,http://tu.blyoo.com/image.php
参数一:url 图片地址 必须写
参数二:p 比例大小 可选 无此参数 默认0.2比例 比例为0-10
参数三:width 宽度 height 高度 可选 无此参数 默认0.2比例
1.比例参数
http://tu.blyoo.com/image.php?url=http://bg.blyoo.com/bing/tu/20161207.jpg //不写比例默认 0.2
2.指定比例参数
http://tu.blyoo.com/image.php?url=http://bg.blyoo.com/bing/tu/20161207.jpg&p=5 //0.5比例
2.指定w宽h高,p比例参数无效
http://tu.blyoo.com/image.php?url=http://bg.blyoo.com/bing/tu/20161207.jpg&p=5&width=500&height=500 //宽500 高500 比例p参数无效