Linux图片马PHP,php 根据请求生成缩略图片保存到Linux图片服务器的代码

这个功能,有点类似图片站点分离的操作,就是将图片单独架设在一台服务器上,有兴趣的朋友,好好研究下吧。

代码如下:

复制代码 代码示例:

/**

* 缩略图片 Linux图片服务器

* edit www.jbxue.com

*/

$picID=$_GET['imgID'];

$picTypes=".".$_GET['imgType'];

$picWidth=$_GET['ImgWidth'];

if($picID!="")

{

//请求的是小图

if($picWidth>0){

//如果小图存在

if(file_exists($picID."_".$picWidth.$picTypes))

{

outputImg($picID."_".$picWidth.$picTypes);

}else

{

if(file_exists($picID."_0".$picTypes)){

//如果不存在小图直接生成小图

resizeImg($picID."_0".$picTypes,$picWidth,$picWidth,$picID."_".$picWidth.$picTypes);

//输出

outputImg($picID."_".$picWidth.$picTypes);

}else

{

//如果大图不存在

resizeImg('noDefaultImage.gif',$picWidth,$picWidth,noDefaultImage."_".$picWidth.".gif");

//输出

outputImg($picID."_".$picWidth.$picTypes);

}

}

}

//判断文件是否存在大图

if(file_exists($picID."_0".$picTypes))

{

$img_file = $picID."_0".$picTypes;

outputImg($img_file);

}

else

{

//如果不存在图片

$img_file = 'noDefaultImage.gif';

outputImg($img_file);

}

}

//输出图片

function outputImg($img_file)

{

$fp = fopen($img_file, 'rb');

$content = fread($fp, filesize($img_file)); //二进制数据

fclose($fp);

header('Content-Type: image/gif');

echo $content;

}

/**

* 生成缩略图

* $srcName----为原图片路径

* $newWidth,$newHeight----分别缩略图的最大宽,高

* $newName----为缩略图文件名(含路径)

* @param string $srcName

* @param int $newWidth

* @param int $newHeight

* @param string $newName

* return viod

*/

function resizeImg($srcName,$newWidth,$newHeight,$newName="")

{

if($newName=="")

{

$nameArr=explode('.',$srcName);

$expName=array_pop($nameArr);

$expName=$expName;

array_push($nameArr,$expName);

$newName = implode('.',$nameArr);

}

$info = "";

$data = getimagesize($srcName,$info);

switch ($data[2])

{

case 1:

if(!function_exists("imagecreatefromgif")){

echo "你的GD库不能使用GIF格式的图片,请使用Jpeg或PNG格式!返回";

exit();

}

$im = ImageCreateFromGIF($srcName);

break;

case 2:

if(!function_exists("imagecreatefromjpeg")){

echo "你的GD库不能使用jpeg格式的图片,请使用其它格式的图片!返回";

exit();

}

$im = ImageCreateFromJpeg($srcName);

break;

case 3:

$im = ImageCreateFromPNG($srcName);

break;

}

$srcW=ImageSX($im);

$srcH=ImageSY($im);

$newWidthH=$newWidth/$newHeight;

$srcWH=$srcW/$srcH;

if($newWidthH<=$srcWH){

$ftoW=$newWidth;

$ftoH=$ftoW*($srcH/$srcW);

}

else{

$ftoH=$newHeight;

$ftoW=$ftoH*($srcW/$srcH);

}

if($srcW>$newWidth||$srcH>$newHeight)

{

if(function_exists("imagecreatetruecolor"))

{

@$ni = ImageCreateTrueColor($ftoW,$ftoH);

if($ni) ImageCopyResampled($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH);

else

{

$ni=ImageCreate($ftoW,$ftoH);

ImageCopyResized($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH);

}

}

else

{

$ni=ImageCreate($ftoW,$ftoH);

ImageCopyResized($ni,$im,0,0,0,0,$ftoW,$ftoH,$srcW,$srcH);

}

if(function_exists('imagejpeg')) ImageJpeg($ni,$newName);

else ImagePNG($ni,$newName);

ImageDestroy($ni);

}

ImageDestroy($im);

}

?>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值