- <?php
-
- $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 "存在图片";
-
-
- 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);
- 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';
- $tempPath = str_replace('http://www.lesohome.com/', '', $imgPath);//替换换行字符
- $name = strrchr($tempPath, "/");
- $path = str_replace($name, '', $tempPath);
-
- 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 ;
- }
- }
- makedir($path);
-
- 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>
posted @
2015-06-09 16:56
拉风的牛
阅读(
867)
评论()
编辑
收藏
举报