PHP缩略图片

//缩略图
    function image($imagefile)
    {
        $star = strrpos($imagefile,'/',-1)+1;
        $end = strrpos($imagefile,'.',-1);
        $imagename = substr($imagefile,$star,$end-$star);
        $url = "http://".$_SERVER['SERVER_NAME']."/projects/webapp/uploads/thumbnail/".$imagename.".jpg";
        $imageurl = "./uploads/thumbnail/".$imagename.".jpg";
        if (file_exists($imageurl))
        {
            return $url;
            die();
        }
        // 指定文件路径和缩放比例
        $filename = $imagefile;//'test.jpg';
        //$percent = 0.05;
        // 指定头文件Content typezhi值
        //header('Content-type: image/jpeg');  //显示在浏览器上是需要这句

        // 获取图片的宽高ob_clean();
        list($width, $height) = getimagesize($filename);
        $newwidth = 60;//$width * $percent;
        $newheight = 60;//$height * $percent;
        // 创建一个图片。接收参数分别为宽高,返回生成的资源句柄
        $thumb = imagecreatetruecolor($newwidth, $newheight);
        //获取源文件资源句柄。接收参数为图片路径,返回句柄
        $source = imagecreatefromjpeg($filename);
        // 将源文件剪切全部域并缩小放到目标图片上。前两个为资源句柄
        imagecopyresampled($thumb, $source, 0, 0, 0, 0, $newwidth,
            $newheight, $width, $height);
        // 输出给浏览器
        $fileurl = "./uploads/thumbnail/{$imagename}.jpg";
        imagejpeg($thumb,$fileurl,80);   //特别注意当imagejpeg要保存图片是,imagejpeg返回的是true或false,不需要输出header('Content-type: image/jpeg');不然浏览器上会一直报图片出错,所以imagejpeg不存在既能保存图片,又能显示在浏览器上。
        imagedestroy($thumb);
        return $url;
    }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值