PHP获取远程图片并调整图像大小(转)

简介: 允许上传的文件类型为:  如何联系我:【万里虎】www.bravetiger.cn【QQ】3396726884 (咨询问题100元起,帮助解决问题500元起)【博客】http://www.cnblogs.com/kenshinobiy/
<?php
/**
*
*函数:调整图片尺寸或生成缩略图
*修改:2013-2-15
*返回:True/False
*参数:
*   $Image   需要调整的图片(含路径)
*   $Dw=450   调整时最大宽度;缩略图时的绝对宽度
*   $Dh=450   调整时最大高度;缩略图时的绝对高度
*   $Type=1   1,调整尺寸; 2,生成缩略图
*/

$phtypes=array('img/gif', 'img/jpg', 'img/jpeg', 'img/bmp', 'img/pjpeg', 'img/x-png');

function compressImg($Image,$Dw,$Dh,$Type){
    echo $Image;
    IF(!file_exists($Image)){
        echo "不存在图片";
        return false;
    }
    echo "存在图片";
    // 如果需要生成缩略图,则将原图拷贝一下重新给$Image赋值(生成缩略图操作)
    // 当Type==1的时候,将不拷贝原图像文件,而是在原来的图像文件上重新生成缩小后的图像(调整尺寸操作)
    IF($Type!=1){
        copy($Image,str_replace(".","_x.",$Image));
        $Image=str_replace(".","_x.",$Image);
    }
    // 取得文件的类型,根据不同的类型建立不同的对象
    $ImgInfo=getimagesize($Image);
    Switch($ImgInfo[2]){
        case 1:
            $Img =@imagecreatefromgif($Image);
            break;
        case 2:
            $Img =@imagecreatefromjpeg($Image);
            Break;
        case 3:
            $Img =@imagecreatefrompng($Image);
            break;
    }
    // 如果对象没有创建成功,则说明非图片文件
    IF(Empty($Img)){
        // 如果是生成缩略图的时候出错,则需要删掉已经复制的文件
        IF($Type!=1){
            unlink($Image);
        }
        return false;
    }
    // 如果是执行调整尺寸操作则
    IF($Type==1){
        $w=ImagesX($Img);
        $h=ImagesY($Img);
        $width = $w;
        $height = $h;
        IF($width>$Dw){
            $Par=$Dw/$width;
            $width=$Dw;
            $height=$height*$Par;
            IF($height>$Dh){
                $Par=$Dh/$height;
                $height=$Dh;
                $width=$width*$Par;
            }
        } ElseIF($height>$Dh) {
            $Par=$Dh/$height;
            $height=$Dh;
            $width=$width*$Par;
            IF($width>$Dw){
                $Par=$Dw/$width;
                $width=$Dw;
                $height=$height*$Par;
            }
        } Else {
            $width=$width;
            $height=$height;
        }
        $nImg =ImageCreateTrueColor($width,$height);// 新建一个真彩色画布
        ImageCopyReSampled($nImg,$Img,0,0,0,0,$width,$height,$w,$h);// 重采样拷贝部分图像并调整大小
        ImageJpeg($nImg,$Image);// 以JPEG格式将图像输出到浏览器或文件
        return true;
    } Else {// 如果是执行生成缩略图操作则
        $w=ImagesX($Img);
        $h=ImagesY($Img);
        $width = $w;
        $height = $h;
        $nImg =ImageCreateTrueColor($Dw,$Dh);
        IF($h/$w>$Dh/$Dw){// 高比较大
            $width=$Dw;
            $height=$h*$Dw/$w;
            $IntNH=$height-$Dh;
            ImageCopyReSampled($nImg, $Img, 0, -$IntNH/1.8, 0, 0, $Dw, $height, $w, $h);
        } Else {// 宽比较大
            $height=$Dh;
            $width=$w*$Dh/$h;
            $IntNW=$width-$Dw;
            ImageCopyReSampled($nImg, $Img,-$IntNW/1.8,0,0,0, $width, $Dh, $w, $h);
        }
        ImageJpeg($nImg,$Image);
        return true;
    }
};
?>

<?php
//网络图片路径
$imgPath = 'http://www.lesohome.com/phone/compress-img/251139474ba926db3d7850.jpg';
//$imgPath = "http://www.lesohome.com/userfiles/image/20111125/251139474ba926db3d7850.jpg";
$tempPath = str_replace('http://www.lesohome.com/', '', $imgPath);//替换换行字符
$name = strrchr($tempPath, "/");
$path = str_replace($name, '', $tempPath);//替换换行字符

/**
 *根据路径path建立多级目录
 *$dir目标目录 $mode权限,0700表示最高权限
*/
function  makedir( $dir , $mode = "0700" ) {
    if(strpos($dir , "/" )){
        $dir_path = "" ;
        $dir_info = explode("/" , $dir );
        foreach($dir_info as $key => $value){
            $dir_path .= $value ;
            if (!file_exists($dir_path)){
                @mkdir($dir_path, $mode) or die ("建立文件夹时失败了" );
                @chmod($dir_path, $mode);
            } else {
                $dir_path .= "/" ;
                continue ;
            }
            $dir_path .= "/" ;
        }
        return $dir_path ;
    } else {
        @mkdir($dir, $mode) or die( "建立失败了,请检查权限" );
        @chmod($dir, $mode);
        return $dir ;
    }
} //end makedir
makedir($path);

/**
 *根据url获取服务器上的图片
 *$url服务器上图片路径 $filename文件名
*/
function GrabImage($url,$filename="") {
    if($url=="") return false;
    if($filename=="") {
        $ext=strrchr($url,".");
        if($ext!=".gif" && $ext!=".jpg" && $ext!=".png")
            return false;
        $filename=date("YmdHis").$ext;
    }
    ob_start(); 
    readfile($url); 
    $img = ob_get_contents(); 
    ob_end_clean();
    $size = strlen($img); 

    $fp2=@fopen($filename, "a");
    fwrite($fp2,$img);
    fclose($fp2);
    return $filename;
}
?>

<html>
<body>
允许上传的文件类型为:<?=implode(', ',$phtypes)?><br/>
<input type="button" id="getImg" name="getImg" size="10" value="获取图片并保存" onclick="alert('12333');" />

<?php

echo $path."<br>";
/**/
$bigImg=GrabImage($imgPath, $tempPath);
if($bigImg){
    echo '<img src="'.$bigImg.'"><br>';
} else {
    echo "false";
} 

compressImg($bigImg,100,80,1);
?>
</body>
</html>

 

如何联系我:【万里虎】www.bravetiger.cn 【QQ】3396726884 (咨询问题100元起,帮助解决问题500元起) 【博客】http://www.cnblogs.com/kenshinobiy/
目录
相关文章
|
3月前
|
PHP
使用PHP实现随机调用图片
使用PHP实现随机调用图片
66 0
使用PHP实现随机调用图片
|
4月前
|
小程序 PHP 数据安全/隐私保护
php图片加水印函数
这里分享下php给图片加水印的几个自定义函数 给图片加水印首先需要开启GD库。 用到的php函数是imagecopymerge () 和 imagecopy () imagecopymerge 函数可以支持两个图像叠加时,设置叠加的透明度
45 0
|
6月前
|
PHP
【PHP】读取本地文件夹中所有图片并显示
PHP图片收集系统收集作业后,为了方便老师在线查阅作业,特意写了个读取图片然后显示出来的php 比较粗糙,可以再多美化美化
59 0
|
1月前
|
存储 PHP Apache
使用CFimagehost源码搭建无需数据库支持的PHP免费图片托管私人图床
使用CFimagehost源码搭建无需数据库支持的PHP免费图片托管私人图床
|
9月前
|
PHP
PHP实现自制随机图片API- 调用文件夹和引用网络图片
PHP实现随机图片API- 调用文件夹和引用网络图片
110 0
|
6月前
|
小程序 PHP
[微擎]多系统共用accesstoken修复wifi小程序文本敏感词汇检测+图片检测原生php(可用)
[微擎]多系统共用accesstoken修复wifi小程序文本敏感词汇检测+图片检测原生php(可用)
|
9月前
|
PHP 计算机视觉
PHP快速入门10-图像处理,附图像大小调整、旋转、获取颜色等15个常见示例
PHP快速入门与实战,以上就是关于本篇文章介绍的内容,图像处理,后续更多内容将收录在专栏PHP快速入门与实战。
|
9月前
|
JSON 前端开发 API
layui框架实战案例(8):web图片裁切插件croppers.js组件实现上传图片的自定义截取(含php后端)
layui框架实战案例(8):web图片裁切插件croppers.js组件实现上传图片的自定义截取(含php后端)
382 0
|
9月前
|
PHP
PHP实现图片登录验证码的解决方案
PHP实现图片登录验证码的解决方案
75 0